users@jersey.java.net

[Jersey] Re: Guice integration

From: Michael Delamere <michael.delamere_at_sevon-it.com>
Date: Mon, 24 Feb 2014 22:02:07 +0100

Hi Gili,

 

Thanks for your reply, I have come up with a solution now which is
sufficient for my current needs. In case anyone is interested, here is what
I did:

 

1. Created annotation called "ModelParam":

 

@Target({ PARAMETER, METHOD, FIELD })

@Retention(RUNTIME)

@Documented

public @interface ModelParam

{

}

 

2. 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);

}

 

3. 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;

       }

}

 

This may not be the perfect solution, but it works very well for my current
need. Of course this mainly works in my case because all of my model classes
happen to have the "Model" annotation anyway and are already registered
with guice. Nevertheless, maybe someone can get some ideas from this and
save time when a similar problem arises.

 

Best regards,

Michael

 

 

Von: cowwoc [mailto:cowwoc_at_bbs.darktech.org]
Gesendet: Montag, 24. Februar 2014 17:04
An: users_at_jersey.java.net
Betreff: [Jersey] Re: Guice integration

 

Hi Michael,

Jersey 2.0 doesn't support Guice. See
https://java.net/jira/browse/JERSEY-1950

Gili

On 24/02/2014 5:11 AM, Michael Delamere wrote:

Hi all,

 

I am having problems integrating guice in jersey. Basically when I try to
populate the bean "DefaultRole" (interface "Role") using injection with
@InjectParam the implementation class "DefaultRole" gets instantiated, but
not populated. When I leave out @InjectParam and just use the implementation
class directly in the method signature instead of the interface, it works
perfectly.

 

This is the request that I am sending off via POST:

 

{"id":12345,"permissions":["READ_XX","WRITE_XX"]}

 

The following implementation DOES NOT work where I am using the bean
interface as the parameter:

 

       @POST

       @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })

       @Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })

       public Role processLogin(@InjectParam Role role) throws JSONException

       {

             System.out.println(role);

 

             return role;

       }

 

The following implementation WORKS where I am using the bean implementation
as the parameter:

 

       @POST

       @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })

       @Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })

       public Role processLogin(DefaultRole role) throws JSONException

       {

             System.out.println(role);

 

             return role;

       }

 

Unfortunately this is of course not what I want because I want to be able to
use my interfaces and have them injected via jersey/guice.

 

Can anyone help me on this? What could I be doing wrong?

 

Thanks and best regards,

Michael