如文档所示,在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()
。我试图注入VertxRouter
,因为Quarkus似乎在使用它,但我没有找到添加路由的方法。这可行吗?如果可行,怎么做?
您基本上需要执行以下操作:
@ApplicationScoped
public class BeanRegisteringRoute {
void init(@Observes Router router) {
router.route("/my-path").handler(rc -> rc.response().end("Hello, World!"));
}
}
请参阅此了解更多信息