Re: Google Guice & Jersey multiple URL patterns to same servlet while applying package filtering

204 views
Skip to first unread message

Fábio Uechi

unread,
Oct 26, 2013, 5:56:49 PM10/26/13
to google...@googlegroups.com
I need to do something very similar.
Did you find a workaround?

On Wednesday, May 15, 2013 12:23:48 PM UTC-3, mihaichiritescu wrote:
I am trying, using Jersey & Google Guice 3.0, to map 2 different URL patterns to the same servlet and each of these URL patterns to be applied to a different package in my project.

To be clear I am pasting part of the code below and I will also explain.

web.xml

<listener>
    <listener-class>com.abc.web.listeners.GuiceContextListener</listener-class>
</listener>
<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>/*</url-pattern>
</filter-mapping>


GuiceContextListener

public final class GuiceContextListener extends GuiceServletContextListener {
...
    private JerseyServletModule getJerseyServletModule() {
        JerseyServletModule jerseyModule = new JerseyServletModule() {
            @Override
            protected void configureServlets() {
                filter("/*").through(WebServerStateFilter.class);
                final Map<String, String> params = new HashMap<String, String>();
                StringBuilder sb = new StringBuilder();
                sb.append("com.abc.web.stats.services");
                params.put(PackagesResourceConfig.PROPERTY_PACKAGES, sb.toString());
                serve("/api/v1/*").with(GuiceContainer.class, params);

                final Map<String, String> params1 = new HashMap<String, String>();
                StringBuilder sb1 = new StringBuilder();
                sb1.append("com.abc.web.stats.otherservices");
                params1.put(PackagesResourceConfig.PROPERTY_PACKAGES, sb1.toString());
                serve("/api/*").with(GuiceContainer.class, params1);
            }
        }
    }
...
}
So basically I want:

URLs starting with "/api/v1/*" to be handled through servlet GuiceContainer by services in package "com.abc.web.stats.services"
URLs starting with "/api/*" to be handled through same servlet GuiceContainer by services in package "com.abc.web.stats.otherservices"
I used the code above but it does not seem to work, it seems that only the first "serve" call is taken into consideration so in this case only URLs matching "/api/v1/*" are served. I do not want to hardcode "v1" in my services since the version might change (to "v2") in the future.

Can somebody help me out?

Thanks, Paul
Reply all
Reply to author
Forward
0 new messages