【问题标题】:Static content in Grizzly jersey灰熊球衣中的静态内容
【发布时间】:2015-08-19 10:42:01
【问题描述】:

我想在/和/index.html中提供静态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, "/");

我的问题是我应该把静态文件放在哪里

【问题讨论】:

    标签: java jersey grizzly


    【解决方案1】:

    在您的情况下,静态数据应位于磁盘根目录的/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, "/");
      

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-06-06
      • 1970-01-01
      • 1970-01-01
      • 2015-11-11
      • 1970-01-01
      • 2013-12-07
      • 2016-01-03
      • 2013-08-11
      相关资源
      最近更新 更多