Jersey + Guice + Tomcat与根目录以外的内容一起使用时产生404 [英] Jersey + Guice + Tomcat producing 404 when served with something other than root directory

查看:107
本文介绍了Jersey + Guice + Tomcat与根目录以外的内容一起使用时产生404的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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(/* */) {
        /* */
    }

}

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

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

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

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

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

但是诸如index.html之类的东西却没有.将"/*"更改为"/Resources/*"会使index.html之类的东西再次起作用,但是ConsoleResource@POST方法不起作用(每次访问/Resources/Console时,我都会得到404).我假设我想使后者工作(例如).有想法吗?

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?

谢谢!

推荐答案

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

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:

<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>

并遵循惯例,即我的资源以/Resources/作为其路径的前缀,静态内容通过Guice过滤器获取.

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

这篇关于Jersey + Guice + Tomcat与根目录以外的内容一起使用时产生404的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆