我非常新于Vert.x,只有几天的经验。我来自JAX-RS、RESTeasy的世界。我可能非常错误,请指正。
所以,我想使用vertx-web和Spring编写REST API。我将Verticles视为REST资源。 我看了一下vertx-web blog和spring-example,但这些示例非常简单,大多数只包含一个资源和Verticle。 我的问题是:如何使Verticle公开其自己的REST接口(子路由),并将其子路由注册到应用程序的主路由中?
我尝试了类似以下的内容,但当我请求/products/all时,我得到404错误:
所以,我想使用vertx-web和Spring编写REST API。我将Verticles视为REST资源。 我看了一下vertx-web blog和spring-example,但这些示例非常简单,大多数只包含一个资源和Verticle。 我的问题是:如何使Verticle公开其自己的REST接口(子路由),并将其子路由注册到应用程序的主路由中?
我尝试了类似以下的内容,但当我请求/products/all时,我得到404错误:
public class ProductsVerticle extends AbstractVerticle {
@Override
public void start(Future<Void> startFuture) throws Exception {
super.start(startFuture);
}
public static Router getRouter() {
Router router = Router.router(Vertx.vertx());
router.get("/all").consumes("application/json").produces("application/json")
.handler(routingContext -> {
routingContext.response()
.putHeader("content-type", "text/html")
.end("<h1>Products</h1>");
});
return router;
}
}
public class ServerVerticle extends AbstractVerticle {
@Override
public void start() throws Exception {
super.start();
Router mainRouter = Router.router(vertx);
ProductsVerticle productsVerticle = new ProductsVerticle();
vertx.deployVerticle(productsVerticle, handler -> {
if (handler.succeeded()) {
LOG.info("Products Verticle deployed successfully");
mainRouter.mountSubRouter("/products", productsVerticle.getRouter());
}
});
mainRouter.get("/static").handler(routingContext -> {
routingContext.response()
.putHeader("content-type", "text/html")
.end("<h1>Hello from my first Vert.x 3 application</h1>");
});
HttpServer server = vertx.createHttpServer();
server.requestHandler(mainRouter::accept);
server.listen(8090);
}
}