users@jersey.java.net

Re: [Jersey] Passing objects in Jersey Service.

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Fri, 18 Dec 2009 16:18:04 +0100

On Dec 18, 2009, at 4:01 PM, RajKal wrote:

> Thanks Paul for the response. I was under the assumption that Jersey
> supports Dependency Injection.

It does a bit but there are other better DI frameworks out there and
it is a lot of work to reproduce the same features. So the approach
has been do enough DI but defer to integrating with other DI
frameworks for full-on DI.


>
> Now,I am thinking of this approach ,method takes xml string as
> input, parse the xml build the Employee and User objects and boolean
> variable.Then call my regular method. Is this a good approach?

It can if you want Jersey and some other method to call this. Same
goes for construction.

Again, how do you propose to bind the type Employee, User and boolean
to values so that Jersey can get instances of those?

What scopes should they be in? where should the value of the boolean
be obtained from?



> Is there any other approach you can suggest me off?
>

If you were using Guice you could do:

   @RequestScoped
   @Path("/")
   public class FooResource {
     private final Employee employee
     private final User user;
     private final boolean isSSNRequired;

     @Inject
     public FooResource(Employee employee, User user, @Named("xxxx")
boolean isSSNRequired) {
       this.employee = employee;
       this.user = user;
       this.isSSNRequired = isSSNRequired;
     }

     @GET
     @Path("/result")
     @Produces( { "text/xml"})
     public EmployeeDetail getResult() { ... }
   }

Then you would bind Employee, User and @Named("xxxx") boolean in the
Guice module when it is configured.

See:

   https://jersey.dev.java.net/nonav/apidocs/latest/contribs/jersey-guice/com/sun/jersey/guice/spi/container/servlet/package-summary.html

You can also use CDI in Java EE 6 supported by GlassFish v3, which
supports same annotation names as Guice.


You can also use Jeresy itself. If you want to i can provide guidance
in that respect on how to wire things up (the boolean value is
problematic so there needs to be another way, and that is dependent on
where the boolean value comes from).

Paul.

> Raj
> --- On Thu, 12/17/09, Paul Sandoz <Paul.Sandoz_at_Sun.COM> wrote:
>
>> From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
>> Subject: Re: [Jersey] Passing objects in Jersey Service.
>> To: users_at_jersey.dev.java.net
>> Date: Thursday, December 17, 2009, 11:11 AM
>> Hi Raj,
>>
>> I suspect you are getting a 404 because your application
>> fails to deploy, which is because Jersey will throw an
>> exception on deployment because it cannot bind the
>> parameters on the getResult method?
>>
>> How do you propose to bind the type Employee, User and
>> boolean to values? The latter is especially problematic, it
>> will additional contextual information.
>>
>> Note that Jersey does not provide a fully featured
>> Dependency Injection (DI) framework and instead relies on
>> integration with other DI frameworks, like Guice.
>>
>> Having said that Jersey does have some DI support, and you
>> could use that if you do not want to use say Guice, as i
>> said it all depends on how you want to bind the parameters.
>> And from that i can provide more details.
>>
>> Paul.
>>
>> On Dec 16, 2009, at 10:48 PM, RajKal wrote:
>>
>>> Writing a web app that can be used as an application
>> as well as service to return a employee detail object.
>> Method takes 2 objects as input and one boolean variable. I
>> want below method to be used in the app and the same method
>> to serve as restful service.
>>>
>>> Is this correct approach or do I need to do anything
>> else? Also I need to know how I can test with the browser or
>> any client? When I am trying to access it to with the below
>> url it gives me 404 -not found error.
>>>
>>> http://localhost:7001/jersey/detail/result
>>>
>>> @GET
>>> @Path("/result")
>>> @Produces( { "text/xml"})
>>> public EmployeeDetail getResult(@Context
>> Employee employee, @Context User user, @Context boolean
>> isSSNRequired) throws Exception {
>>>
>>> return employeeDetail;
>>> }
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
>>> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>>>
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
>> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>>
>>
>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>