Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
330 views
in Technique[技术] by (71.8m points)

java - Routing template format for undertow

Is there any documentation about routing template format for undertow. I want to setup handlers like this:

/ or /index.html -> Use handler 1
Anything else -> Use handler 2

I tried this one, bu did not work:

Handlers.routing()
        .add("GET", "/", handler1)
        .add("GET", "/index.html", handler1)
        .add("GET", "/*", handler2)

Any idea?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There are a couple of ways to achieve this:

1) Basic approach: PathHandler

Handlers.path()
    .addExactPath("/path1", handler1)
    .addPrefixPath("/path2", handler2);

The handler1 will match only on /path1 (or /path1/).

The handler2 will match on /path2, /path2/ and everything else that starts with /path2/.

2) Route approach: RoutingHandler

If you use a RoutingHandler, you have the option to easily extract variables from the paths. That's convenient for building REST APIs for example (note the usage of the convenience get method on the RoutingHandler).

Handlers.routing().get("/{test}/*", exchange -> {
    PathTemplateMatch pathMatch = exchange.getAttachment(PathTemplateMatch.ATTACHMENT_KEY);
    String itemId1 = pathMatch.getParameters().get("test"); // or exchange.getQueryParameters().get("test")
    String itemId2 = pathMatch.getParameters().get("*"); // or exchange.getQueryParameters().get("*")
}))

The * parameter can match anything (like a path for instance a/b/c). In order to use the * parameter, you need an actual named parameter defined before in the route template (test in my example).

Note that the parameters defined in your route template will be available together with the query parameters (exchange.getQueryParameters()). This is default behavior. If you do not want it, you can create your routing handler like this: Handlers.routing(false).get(...) and then retrieve the parameters from the exchange's attachments.

For any route that is not matched by your routing handler, you can use the fallbackHandler available in the RoutingHandler.

Handlers.routing()
      .get("/", handler1)
      .get("/index.html", handler1)
      .setFallbackHandler(handler2);

By default the fallbackHandler simply returns an empty response body with a 404 status code. The handler2 will be matching any other requests, not only GET requests.

Comprehensive Example

You can of course combine PathHandler and RoutingHandler to fit your needs.

Here is a small example of a more realistic setup:

Undertow.builder().addHttpListener(8080, "0.0.0.0")
    .setHandler(Handlers.path()

        // REST API path
        .addPrefixPath("/api", Handlers.routing()
            .get("/customers", exchange -> {...})
            .delete("/customers/{customerId}", exchange -> {...})
            .setFallbackHandler(exchange -> {...}))

        // Redirect root path to /static to serve the index.html by default
        .addExactPath("/", Handlers.redirect("/static"))

        // Serve all static files from a folder
        .addPrefixPath("/static", new ResourceHandler(
            new PathResourceManager(Paths.get("/path/to/www/"), 100))
            .setWelcomeFiles("index.html"))

    ).build().start();

This application also serves static files from your file system. This is handy to serve a javascript application or static html files for instance.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...