users@jersey.java.net

Re: [Jersey] best practice for generating unique id in the resources

From: Jeff Schmidt <jas_at_535consulting.com>
Date: Fri, 31 Oct 2008 21:18:44 -0600

Hi Arul:

I think you want to do this:

   @POST
   @Consumes("application/xml")
   @Produces("application/xml")
   public Response addCustomer(Customer customer) {
     long customerId = 1234L; //Wherever this comes from (DB sequence,
atomic counter etc.).
     UriBuilder ub = uriInfo.getAbsolutePathBuilder();
     URI uri = ub.path(String.valueOf(customerId)).build();
     customer.setId(customerId);
     Response r = Response.created(uri).build();
     customer.setUri(uri.toString());
     return r;
   }
}

That should give you the response Jeff R. stipulated below.

Cheers,

Jeff [another Jeff :)]
--
Jeff Schmidt
On Oct 31, 2008, at 3:45 PM, Arul Dhesiaseelan wrote:
> Thanks Jeff.
>
> How would this work in the below case?
>
> @Path("/customers")
> public class CustomersResource {
>   @Context
>   UriInfo uriInfo;
>   @Context
>   Request request;
>
>   public CustomersResource(UriInfo uriInfo, Request request) {
>     this.uriInfo = uriInfo;
>     this.request = request;
>   }
>   @Path("/add")
>   public CustomerResource getCustomerResource() {
>     return new CustomerResource(uriInfo, request);
>   }
> }
>
> public class CustomerResource {
>   @Context
>   UriInfo uriInfo;
>   @Context
>   Request request;
>
>   public CustomerResource(UriInfo uriInfo, Request request) {
>     this.uriInfo = uriInfo;
>     this.request = request;
>   }
>
>   @POST
>   @Consumes("application/xml")
>   @Produces("application/xml")
>   public Response addCustomer(Customer customer) {
>     URI uri = uriInfo.getAbsolutePath();
>           //how to generate the new URI with id?
>     customer.setId(1234);
>     Response r = Response.created(uri).build();
>     customer.setUri(uri.toString());
>     return r;
>   }
> }
>
> How would I create the new URI with customer  Id?
>
> Thanks!
> -Arul
>
> Robertson, Jeff wrote:
>> Wouldn't you normally want the client to create a customer by  
>> POSTing to
>> the parent /customers resource?
>>
>> Request:
>>
>>         POST /customers HTTP/1.1
>>         Content-type: xxx
>>         Content-length: nnn
>>
>>         ... etc ...
>>
>>
>> Response:
>>
>>         HTTP 201 Created
>>         Location: /customers/1234
>>
>>
>>
>> -----Original Message-----
>> From: Arul Dhesiaseelan [mailto:arul_at_fluxcorp.com] Sent: Friday,  
>> October 31, 2008 4:26 PM
>> To: users_at_jersey.dev.java.net
>> Subject: [Jersey] best practice for generating unique id in the
>> resources
>>
>> Hello,
>>
>> I have a CustomersResource with sub-resource CustomerResource :
>>
>> /customers/{id} =>
>>
>> /customers/1909
>> /customers/2234
>> /customers/3454
>>
>> The client can add customers to the customers resource using POST.  
>> But, it needs to populate the unique id which forms part of the URI  
>> as well as customer ID.
>>
>> What would be the best method to generate such unique id?
>>
>> I can think of an ID generator service on the server-side and the  
>> client
>>
>> can first do a GET to get an ID and use this ID in the POST to  
>> create a new customer.
>>
>> Would this approach cause any issue? Is there a better approach to  
>> implement this behavior?
>>
>> Appreciate your suggestions.
>>
>> Thanks!
>> Arul
>>
>>
>>
>>
>> ---------------------------------------------------------------------
>> 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
>