我正在为 Vertx-Web 应用程序构建一个 fat jar。我想提供一些静态文件。我将jar文件打包,带有webroot文件夹。请参阅下面的屏幕截图了解我的 jar 结构:
我能够通过以下方式加载 webroot/static/test.html 文件:
routingContext.response().sendFile("webroot/static/test.html");
但是,我无法让静态处理程序正常工作。下面是我的完整代码:
package com.jdescript;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import io.vertx.core.AbstractVerticle;
import io.vertx.core.http.HttpServer;
import io.vertx.ext.web.Router;
import io.vertx.ext.web.handler.StaticHandler;
public class WebVerticle extends AbstractVerticle {
private HttpServer httpServer;
@Override
public void start() throws IOException {
httpServer = vertx.createHttpServer();
Router router = Router.router(vertx);
router.route("/static/*").handler(StaticHandler.create());
router.route("/test").handler(routingContext -> {
routingContext.response().sendFile("webroot/static/test.html");
});
httpServer.requestHandler(router::accept).listen(9999);
}
}
在上面的示例中,http://localhost:9999/static/test.html会说“未找到”,而 http://localhost:9999/test将渲染 test.html。
任何帮助将不胜感激。
最佳答案
Vert.x 小组在 https://groups.google.com/forum/?fromgroups#!topic/vertx/yKInZuYcqDE 上进行了回答。我的 webroot 应该类似于“webroot/test.html”,而不是“webroot/static/test.html”。
关于java - Fat Jar 中的 Vertx Webroot,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39782818/