繁体   English   中英

如何以编程方式在 Quarkus 中声明路由?

[英]How to programmatically declare a route in Quarkus?

文档中所示,在 Quarkus 中声明路由的标准方法是使用@Path()注释,如下所示:

@Path("myPath")
public class Endpoint {

    @GET
    public String hello() {
        return "Hello, World!";
    }
}

这将创建路线GET /MyPath 但是, @Path是一个注释,我必须给它常量表达式。

我希望能够声明一个带有非常量表达式的路由,比如@Path(MyClass.class.getSimpleName())

我试图实现这样的事情:

public class Endpoint {

    public void initialize(@Observes StartupEvent ev) {
        declareRoute(MyClass.class.getSimpleName(), HttpMethod.GET, this::hello);
    }

    public String hello() {
        return "Hello, World!";
    }

    public void declareRoute(String path, HttpMethod method, Consumer handler) {
        // TODO implement
    }
}

这将创建路线GET /MyClass但我不知道如何实施declareRoute() 我试图注入 Vertx Router ,因为 Quarkus 似乎在使用它,但我没有找到添加路由的方法。 这可行吗?如果可行,怎么做?

您基本上需要执行以下操作:

@ApplicationScoped
public class BeanRegisteringRoute {

    void init(@Observes Router router) {
        router.route("/my-path").handler(rc -> rc.response().end("Hello, World!"));
    }
}

看到这个了解更多信息

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM