Jersey, Guice using non-root request paths

2019-09-08 07:13发布

I'm using Jersey 1.11 over Guice 3.0 on Tomcat 6.0.32 in a standard configuration:

configureServlets() {
    filter("/ws/*").through(GuiceContainer.class);
}

And a simple resource class:

@Path("/resource")
public class Resource { ... }

Given that, I would suppose that accessing "/ws/resource" would work; but actually no resources are found. The problem seems to lie in the request path not being computed correctly. As a workaround I have set the parameter PROPERTY_FILTER_CONTEXT_PATH to /ws, which make the whole thing work:

Map<String, String> jerseyConfig = new HashMap<String, String>();
jerseyConfig.put(ServletContainer.PROPERTY_FILTER_CONTEXT_PATH, "/ws");
filter("/ws/*").through(GuiceContainer.class, jerseyConfig);

Thus my questions are:

  1. Is this really a bug or a "feature" ?
  2. Is there another solution or workaround for this?

For info, I've seen one Guice bug that can be related, it seems to have been merged in another one but I'm wondering if it's properly fixed (link)

1条回答
贪生不怕死
2楼-- · 2019-09-08 07:30

Use serve instead of filter.

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

You will then be able to hit /ws/resource.

查看更多
登录 后发表回答