I have a REST api put together using jersey and guice for dependency injection. Everything works great, I have FEATURE_CANONICALIZE_URI_PATH and FEATURE_NORMALIZE_URI turned on and that handles extra slashes in almost all cases. For instance:
http://localhost:8080/my_service/param1//param2
The problem is I can't sort out the proper way to handle an extra slash at the beginning of the url (before the service path). Like this:
http://localhost:8080//my_service/param1/param2
This is how I have stuff set up currently:
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>/*</url-pattern>
</filter-mapping>
<listener>
<listener-class>MyServletConfig</listener-class>
</listener>
MyServletConfig.java
public class MyServletConfig extends GuiceServletContextListener {
@Override
protected Injector getInjector() {
return Guice.createInjector(new JerseyServletModule() {
@Override
protected void configureServlets() {
bind(MyService.class);
guiceContainerConfig.put(ResourceConfig.FEATURE_NORMALIZE_URI, "true");
guiceContainerConfig.put(ResourceConfig.FEATURE_CANONICALIZE_URI_PATH, "true");
serve("/*").with(GuiceContainer.class, guiceContainerConfig);
}
});
}
}
MyService.java
@Path("/")
public class MyService {
@GET
@Path("/{param1}/{param2}")
@Produces("application/json")
public String get(
@PathParam("param1") final String param1,
@PathParam("param2") final String param2) {
return "{\"param1\":\"" + param1 + "\"}";
}
}
I can see that the issue really is that the request with the // before the service name is not actually getting handled by the servlet because it is not matched in the url-mapping of the filter-mapping but I'm just not really sure the right solution. Do I need to do an url rewrite before that url is matched to a servlet or is there a better way to map the request urls?
Quick leads:
http://tuckey.org/urlrewrite/ is a java web filter. It is a very powerful tool just like Apache's mod_rewrite. Setup the filter with classic htaccess rewrites from http://www.mydigitallife.info/redirect-or-rewrite-to-remove-double-or-multiple-slashes-in-url/