【问题标题】:Serve static files together with Jersey, Guice config与 Jersey、Guice 配置一起提供静态文件
【发布时间】:2013-03-17 21:57:29
【问题描述】:

我确实有以下配置作为 Guice 模块(而不是 web.xml)

public class RestModule extends JerseyServletModule {
@Override
protected void configureServlets() {
    install(new JpaPersistModule("myDB"));
    filter("/*").through(PersistFilter.class);

    bind(CustomerList.class);
    bind(OrdersList.class);

    bind(MessageBodyReader.class).to(JacksonJsonProvider.class);
    bind(MessageBodyWriter.class).to(JacksonJsonProvider.class);

    ImmutableMap<String, String> settings = ImmutableMap.of(
            JSONConfiguration.FEATURE_POJO_MAPPING, "true"
            );

    serve("/*").with(GuiceContainer.class, settings);
}
}

为 REST 端点提供服务已经非常有效了。

当用户请求 http://example.com/ 时,我想从 /webapp/index.html 提供一个静态 html 文件

以及http://example.com/customershttp://example.com/orders 的其余服务

我不使用 web.xml。网络服务器是码头

【问题讨论】:

  • 我知道stackoverflow.com/questions/10808610/… 中有类似的答案,但我真的想避免使用 /services/ 前缀
  • 安德烈亚斯,你找到解决办法了吗?我已经为此苦苦挣扎了好几个小时了。我已经尝试了从在 guice 社区发布的各种正则表达式的 serveRegex 到这个解决方案 (stackoverflow.com/a/3593513/695318) 到使用 tuckey UrlRewriteFilter 的所有方法,但我每次都失败了。最后,由于您的问题,我找到了 condit 的帖子,并且运行良好。如果您使用过其他解决方案,我会很好奇您是如何解决的。

标签: jersey jetty guice guice-servlet


【解决方案1】:

见:Jersey /* servlet mapping causes 404 error for static resources 并将适当的参数添加到您的 settings 对象。比如:

ImmutableMap<String, String> settings = ImmutableMap.of(
  JSONConfiguration.FEATURE_POJO_MAPPING, "true"
  "com.sun.jersey.config.property.WebPageContentRegex", "/.*html");

【讨论】:

    【解决方案2】:

    正如我在评论中所说,我几乎整天都在为此苦苦挣扎。 condit 链接到正确答案,但为了完整起见,这对我有用。我正在使用 Tomcat、jersey(和 jersey-guice)1.17.1 和 guice 3.0。

    虽然我使用的是 Tomcat,但困难的部分(提供静态资源)应该没有那么不同。

    public class JerseyConfig extends GuiceServletContextListener {
        @Override
        protected Injector getInjector() {
            return Guice.createInjector(new JerseyServletModule() {
                @Override
                protected void configureServlets() {
    
                    // Bind  REST resources
                    bind(HomeController.class);
                    bind(AboutController.class);
    
                    Map<String, String> params = new HashMap<String, String>();
                    params.put("com.sun.jersey.config.property.WebPageContentRegex",
                        "/(images|css)/.*");
                    filter("/*").through(GuiceContainer.class, params);
                }
            });
        }
    }
    

    注意:最后两行是最重要的。

    首先,忽略静态文件的参数:

    "com.sun.jersey.config.property.WebPageContentRegex" (1.7 only!!!)
    

    我已将此设置为正则表达式:

    "/(images|css)/.*"
    

    ...因为我的静态资源位于 src/main/webapp/images 和 src/main/webapp/css (我使用的是 maven 项目结构)。比如:

    http://localhost:8080/myapp/images/logo.png
    

    (这仅适用于 jersey 1.7.x - 如果您使用 jersey 2.x,则应使用键“jersey.config.servlet.filter.staticContentRegex”或最好使用 Java 常量 ServletProperties.FILTER_STATIC_CONTENT_REGEX)。

    最后,最后一行:

    filter("/*").through(GuiceContainer.class, params);
    

    你应该使用:filter("/*").through 和 NOT serve("/*).with

    如果你想出一个不同的解决方案,我想看看它是什么。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-12-14
      • 1970-01-01
      • 2020-08-02
      • 1970-01-01
      • 2015-06-21
      • 2011-01-19
      相关资源
      最近更新 更多