After I got a reply in the jersey mailing list saying that guice was not supported by jersey, I have digged a bit further to try and come up with a solution. Here is the message:
"Jersey 2.0 doesn't support Guice. See https://java.net/jira/browse/JERSEY-1950". (http://jersey.576304.n2.nabble.com/Guice-integration-td7581958.html)
Just for completeness I'll post the solution here too. The solution I have come up with may not be a generic solution for everyone, but may give some ideas or help if the following conditions are met:
- All objects which are to be injected are "marked" with a class-level annotation.
- All objects which are to be injected implement some interface (although it may work without by specifying Object?).
- You are happy with creating your own custom param annotation.
In my particular case all of my beans are marked with a @Model annotation and implement my "Model" interface. Other classes such as repositories or services can be injected normally into jersey resources by guice anyway. The problem I had was merely to do with the @InjectParam annotation.
The magic part of it all I suppose is mainly the line:
model = request.getEntity(model.getClass());"
which magically populates the guice-injected-object with the deserialized content (no matter whether JSON or XML). I am surprised that this doesn't happen with the built in solution when using @InjectParam already because after all, the actual injecting wasn't the problem - it just wasn't populating the object.
So here is how I solved it:
Created an annotation called “ModelParam”:
@Target({ PARAMETER, METHOD, FIELD }) @Retention(RUNTIME) @Documented public @interface ModelParam { }
Replaced the “InjectParam” annotation in my resource “RoleRestWS” with the new “ModelParam” annotation:
@POST @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) @Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) public Role createRole(@ModelParam Role role) throws JSONException { return userService.createRole(role); }
Created an InjectableProvider for the new “ModelParam” annotation:
@Provider @Singleton public class ModelInjectableProvider extends AbstractHttpContextInjectable<Model> implements InjectableProvider<ModelParam, Type> { private final Type type; public ModelInjectableProvider() { type = null; } public ModelInjectableProvider(Type type) { this.type = type; } @Override public ComponentScope getScope() { return ComponentScope.Undefined; } @Override public Injectable<Model> getInjectable(ComponentContext ic, ModelParam mp, Type type) { if (type instanceof Class && Model.class.isAssignableFrom((Class<?>) type)) { return new ModelInjectableProvider(type); } return null; } @Override public Model getValue(HttpContext ctx) { if (type instanceof Class && Model.class.isAssignableFrom((Class<?>) type)) { HttpRequestContext request = ctx.getRequest(); Model model = null; if (HttpMethod.POST.equals(request.getMethod()) || HttpMethod.PUT.equals(request.getMethod())) { model = (Model) MyGuiceInjector.inject((Class<?>) type); if (model != null) model = request.getEntity(model.getClass()); } return model; } return null; } }
Hope this helps someone with a similar problem.
Best regards, Michael