本文介绍了路径的路由模板格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有关于路径的路由模板格式的文档。我想设置这样的处理程序:

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)

任何想法?

推荐答案

有几种方法可以达到这个目的:

There are a couple of ways to achieve this:

1)基本方法: PathHandler

1) Basic approach: PathHandler

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

handler1 仅匹配 / path1 (或 / path1 / )。

handler2 将匹配 / path2 / path2 / 以及以 / path2 / 开头的所有其他内容。

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

2)路线方法: RoutingHandler

2) Route approach: RoutingHandler

如果您使用a RoutingHandler ,您可以选择从路径中轻松提取变量。例如,这对于构建REST API很方便(注意在 RoutingHandler 上使用方便 get 方法)。

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("*")
}))

* 参数可以匹配任何内容(例如路径,例如 a / b / C )。
为了使用 * 参数,您需要在路径模板中定义之前定义的实际命名参数( test 在我的示例中。

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).

请注意,路径模板中定义的参数将与查询参数一起使用( exchange.getQueryParameters( ))。这是默认行为。如果您不想要它,可以像这样创建路由处理程序: Handlers.routing(false).get(...)然后从交换机中检索参数附件。

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.

对于路由处理程序不匹配的任何路由,您可以使用 fallbackHandler RoutingHandler

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);

默认情况下, fallbackHandler 只返回一个空响应正文,404状态代码。 handler2 将匹配任何其他请求,而不仅仅是 GET 请求。

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.

综合示例

您当然可以合并 PathHandler RoutingHandler 以满足您的需求。

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();

此应用程序还提供文件系统中的静态文件。例如,这对于提供javascript应用程序或静态html文件很方便。

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

这篇关于路径的路由模板格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-27 05:46