I am trying to understand what is the difference between JerseyServletModule
present in jersey-guice
library and ServletModule
present in Guice
library.

- 2,381
- 3
- 24
- 46
1 Answers
TL;DR - JerseyServletModule is dead. It used to (in the Jersey 1 days) allow you to use Jersey and Guice and have them play nice. That's no longer simple.
ServletModule is a Guice module which allows you to configure servlets, servlet mappings, filters and filter mappings (amongst other things) using Guice as well as inject objects into your servlets. Create a class which inherits from ServletModule and override the configureServlets
method a bit like this:
class MyModule extends ServletModule {
@Override
protected void configureServlets() {
serve("/hello").with(HelloAppEngine.class);
}
}
The documentation for building webservices with Guice and ServletModule is here.
Jersey is an implementation of of the JSR 370 so called JAX-RS API for creating rest services. It allows you to build rest services with little more than annotated classes - for example:
@Path("/user")
public class UserController {
UserPersistService userPersistService;
ObjectMapper objectMapper;
UserController(UserPersistService userPersistService, ObjectMapper objectMapper) {
this.userPersistService = userPersistService;
this.objectMapper = objectMapper;
}
@GET
@Produces(MediaType.APPLICATION_JSON)
public String getUsersAsJSON() throws IOException {
final List<User> users = userPersistService.getUsers();
return objectMapper.writeValueAsString(users);
}
}
In the old days of Jersey 1.0, overriding JerseyServletModule
instead of Servlet Module
would permit you to do both at the same time, so the above class could become:
@Singleton
@Path("/user")
public class UserController {
@Inject
UserPersistService userPersistService;
@Inject
ObjectMapper objectMapper;
@GET
@Produces(MediaType.APPLICATION_JSON)
public String getUsersAsJSON() throws IOException {
final List<User> users = userPersistService.getUsers();
return objectMapper.writeValueAsString(users);
}
However as I alluded in the opening, there is no equivalent of JerseyServletModule in Jersey 2.0. Your options are to configure an HK-Guice bridge and continue with Jersey, or to use RestEasy which is an alternative JAX-RS implementation which does play nicely with guice.

- 318
- 3
- 12