简体   繁体   English

Static Grizzly 中的内容 jersey

[英]Static content in Grizzly jersey

I want to serve static html pages in / and /index.html我想在 / 和 /index.html 中提供 static html 页

final HttpServer server = GrizzlyHttpServerFactory.createHttpServer(URI.create(BASE_URI), rc);

BASE_URL = http://locahost:8080/api/ BASE_URL = http://locahost:8080/api/

StaticHttpHandler staticHttpHandler = new StaticHttpHandler("/static");
server.getServerConfiguration().addHttpHandler(staticHttpHandler, "/");

My question is where should i put the static file in我的问题是我应该把 static 文件放在哪里

In your case, static data should be located in /static folder in root of your disk. 在您的情况下,静态数据应位于磁盘根目录下的/static文件夹中。

There are a couple of options where the static resources can be located: 静态资源可以位于几个选项中:

  • you can serve static resources from your filesystem via absolute path (eg /www/static/ ): 您可以通过绝对路径(例如/www/static/ )从文件系统提供静态资源:

     StaticHttpHandler staticHttpHandler = new StaticHttpHandler("/www/static/"); server.getServerConfiguration().addHttpHandler(staticHttpHandler, "/"); 
  • or embedded your resources into jar archive (eg /www/static.jar ): 或将您的资源嵌入jar存档(例如/www/static.jar ):

     StaticHttpHandler staticHttpHandler = new CLStaticHttpHandler(new URLClassLoader(new URL[] {new URL("file:///www/static.jar")})); server.getServerConfiguration().addHttpHandler(staticHttpHandler, "/"); 
  • or embed static resources inside your application jar: 或在应用程序jar中嵌入静态资源:

     StaticHttpHandler staticHttpHandler = new CLStaticHttpHandler(YourApp.class.getClassLoader()); server.getServerConfiguration().addHttpHandler(staticHttpHandler, "/"); 

ATTENTION Cannot cast from CLStaticHttpHandler to StaticHttpHandler!注意不能从 CLStaticHttpHandler 转换为 StaticHttpHandler!

CLStaticHttpHandler staticHttpHandler = new CLStaticHttpHandler(YourApp.class.getClassLoader());
server.getServerConfiguration().addHttpHandler(staticHttpHandler, "/");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM