Jersey + Guice + Tomcat在使用除根目录之外的其他内容时生成404

时间:2022-11-22 19:33:41

I have a resource that looks like this:

我有一个看起来像这样的资源:

@Path("/Resources/Console")
public class ConsoleResource {

    @POST
    @Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
    @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
    public String post(/* */) {
        /* */
    }

}

Whenever my JerseyServletModule is configured as follows, the services work:

每当我的JerseyServletModule配置如下,服务工作:

@Override
protected void configureServlets() {
    bind(ConsoleResource.class);

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

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

But things like index.html don't. Changing "/*" to "/Resources/*" causes things like index.html to work, again, but then ConsoleResource's @POST method doesn't work (I get a 404 whenever I access /Resources/Console). I assume I want to get the latter working (like this). Thoughts?

但像index.html这样的东西却没有。将“/ *”更改为“/ Resources / *”会使index.html之类的东西再次起作用,但是然后ConsoleResource的@POST方法不起作用(每当我访问/ Resources / Console时,我都会得到404)。我想我想让后者工作(像这样)。思考?

Thanks!

1 个解决方案

#1


0  

The issue ended up being the server running static content through the various filters and whatnot. By editing the web.xml file to be the following:

问题最终是服务器通过各种过滤器运行静态内容等等。通过将web.xml文件编辑为以下内容:

<filter>
    <filter-name>guiceFilter</filter-name>
    <filter-class>com.google.inject.servlet.GuiceFilter</filter-class>
</filter>

<filter-mapping>
    <filter-name>guiceFilter</filter-name>
    <url-pattern>/Resources/*</url-pattern>
</filter-mapping>

And abiding by the convention that my resources would have /Resources/ as a prefix to their path, static content gets through the Guice filter.

并且遵守我的资源将具有/ Resources /作为其路径的前缀的约定,静态内容通过Guice过滤器。

#1


0  

The issue ended up being the server running static content through the various filters and whatnot. By editing the web.xml file to be the following:

问题最终是服务器通过各种过滤器运行静态内容等等。通过将web.xml文件编辑为以下内容:

<filter>
    <filter-name>guiceFilter</filter-name>
    <filter-class>com.google.inject.servlet.GuiceFilter</filter-class>
</filter>

<filter-mapping>
    <filter-name>guiceFilter</filter-name>
    <url-pattern>/Resources/*</url-pattern>
</filter-mapping>

And abiding by the convention that my resources would have /Resources/ as a prefix to their path, static content gets through the Guice filter.

并且遵守我的资源将具有/ Resources /作为其路径的前缀的约定,静态内容通过Guice过滤器。