[英]Static content in Grizzly jersey
我想在 / 和 /index.html 中提供 static html 页
final HttpServer server = GrizzlyHttpServerFactory.createHttpServer(URI.create(BASE_URI), rc);
BASE_URL = http://locahost:8080/api/
StaticHttpHandler staticHttpHandler = new StaticHttpHandler("/static");
server.getServerConfiguration().addHttpHandler(staticHttpHandler, "/");
我的问题是我应该把 static 文件放在哪里
在您的情况下,静态数据应位于磁盘根目录下的/static
文件夹中。
静态资源可以位于几个选项中:
您可以通过绝对路径(例如/www/static/
)从文件系统提供静态资源:
StaticHttpHandler staticHttpHandler = new StaticHttpHandler("/www/static/"); server.getServerConfiguration().addHttpHandler(staticHttpHandler, "/");
或将您的资源嵌入jar存档(例如/www/static.jar
):
StaticHttpHandler staticHttpHandler = new CLStaticHttpHandler(new URLClassLoader(new URL[] {new URL("file:///www/static.jar")})); server.getServerConfiguration().addHttpHandler(staticHttpHandler, "/");
或在应用程序jar中嵌入静态资源:
StaticHttpHandler staticHttpHandler = new CLStaticHttpHandler(YourApp.class.getClassLoader()); server.getServerConfiguration().addHttpHandler(staticHttpHandler, "/");
注意不能从 CLStaticHttpHandler 转换为 StaticHttpHandler!
CLStaticHttpHandler staticHttpHandler = new CLStaticHttpHandler(YourApp.class.getClassLoader());
server.getServerConfiguration().addHttpHandler(staticHttpHandler, "/");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.