Reputation: 16518
I do have the following configuration as a Guice module (instead of a web.xml)
public class RestModule extends JerseyServletModule {
@Override
protected void configureServlets() {
install(new JpaPersistModule("myDB"));
filter("/*").through(PersistFilter.class);
bind(CustomerList.class);
bind(OrdersList.class);
bind(MessageBodyReader.class).to(JacksonJsonProvider.class);
bind(MessageBodyWriter.class).to(JacksonJsonProvider.class);
ImmutableMap<String, String> settings = ImmutableMap.of(
JSONConfiguration.FEATURE_POJO_MAPPING, "true"
);
serve("/*").with(GuiceContainer.class, settings);
}
}
Serving the REST endpoints does work very well already.
I would like to serve a static html file from /webapp/index.html when the user requests http://example.com/
and the rest services at http://example.com/customers or http://example.com/orders
i do not use a web.xml. the webserver is jetty
Upvotes: 2
Views: 5519
Reputation: 3703
As I said in my comment, I've struggled with this nearly all day. condit linked to the correct answer, but for completeness here's what worked for me. I'm using Tomcat, jersey (and jersey-guice) 1.17.1, and guice 3.0.
Though I'm using Tomcat, the difficult part (serving static resources) shouldn't be that different.
public class JerseyConfig extends GuiceServletContextListener {
@Override
protected Injector getInjector() {
return Guice.createInjector(new JerseyServletModule() {
@Override
protected void configureServlets() {
// Bind REST resources
bind(HomeController.class);
bind(AboutController.class);
Map<String, String> params = new HashMap<String, String>();
params.put("com.sun.jersey.config.property.WebPageContentRegex",
"/(images|css)/.*");
filter("/*").through(GuiceContainer.class, params);
}
});
}
}
Note: the last two lines are the most important.
First, the param to ignore static files:
"com.sun.jersey.config.property.WebPageContentRegex" (1.7 only!!!)
I have this set to the regex:
"/(images|css)/.*"
...since my static resources are located at src/main/webapp/images and src/main/webapp/css (I'm using the maven project structure). So for instance:
http://localhost:8080/myapp/images/logo.png
(this is for jersey 1.7.x only - if you're using jersey 2.x, you should use the key "jersey.config.servlet.filter.staticContentRegex" or ideally the Java constant ServletProperties.FILTER_STATIC_CONTENT_REGEX).
Finally, the last line:
filter("/*").through(GuiceContainer.class, params);
You should use: filter("/*").through and NOT serve("/*).with
If you came up with a different solution, I'd like to see what it was.
Upvotes: 2
Reputation: 10962
See: Jersey /* servlet mapping causes 404 error for static resources
and add the appropriate parameters to your settings
object. Something like:
ImmutableMap<String, String> settings = ImmutableMap.of(
JSONConfiguration.FEATURE_POJO_MAPPING, "true"
"com.sun.jersey.config.property.WebPageContentRegex", "/.*html");
Upvotes: 2