在Vert.x Web路由器

时间:2018-06-03 11:10:25

标签: java rest web routing vert.x

情境:

  • 我已将静态数据放在webroot / *文件夹位置和服务器上 根据要求提供这些数据。
  • 我有很多路由API的路由。

路由器代码:

Router router = Router.router(vertx);
router.route().handler(BodyHandler.create());

router.route("/api/login/account").handler((RoutingContext ctx) -> {
  // Handler is here
});
router.route("/api/currentUser").handler(ctx -> {
  // Handler is here
});
router.route().handler(StaticHandler.create());
router.route("/*").hanler(StaticHandler.create("webroot/index.html"));

文件夹webroot包含以下文件:

  • index.css
  • 的index.html
  • 图像/ image.jpg的

问题:

  • 需要在路由不匹配上提供webroot / index.html文件(它不起作用;返回值**'未找到资源' )**:如果我请求/ xyz / abc的数据然后应该提供webroot / index.html。
  • 需要提供其他静态文件,因为它的请求参数(它正在工作):如果我请求/index.css的数据,那么应该提供webroot / index.css。
  • 需要响应API请求的数据(它正在运行):如果我请求/ api / login / account的数据,那么它应该响应。

我错过了哪里?什么是解决方案?

1 个答案:

答案 0 :(得分:2)

您应该添加最后一个处理程序,以便在一切都失败时发送您想要的文件。例如:

ctx.response().sendFile('webroot/index.html');

不要忘记添加您可能需要的标头,例如缓存指令,位置,内容类型......