As seen in the documentation, the standard way of declaring a route in Quarkus is with the @Path() annotation, like so :
@Path("myPath")
public class Endpoint {
@GET
public String hello() {
return "Hello, World!";
}
}
This will create the route GET /MyPath. However, @Path being an annotation, I have to give it constant expression.
I would like to be able to declare a route with a non constant expression, something like @Path(MyClass.class.getSimpleName())
I tried to implement something like this:
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
}
}
This would create the route GET /MyClass But I have no idea how to implement declareRoute(). I tried to inject the Vertx Router since Quarkus seems to use it, but I did not find a way to add a route. Is this doable, and if so, how ?
Advertisement
Answer
You essentially need to do something like:
@ApplicationScoped
public class BeanRegisteringRoute {
void init(@Observes Router router) {
router.route("/my-path").handler(rc -> rc.response().end("Hello, World!"));
}
}
See this for more information