users@jersey.java.net

Re: [Jersey] Optional JAXB Parameters

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Fri, 14 Aug 2009 20:52:49 +0200

On Aug 14, 2009, at 7:56 PM, Jonathan Holloway wrote:

> HI Paul,
>
> Many thanks for the response, EntityHolder works great for what I
> want to do...
> I'm a little bit wary about upgrading from 1.1.0-ea to 1.1.1-ea
> though... are the differences between the two releases fairly
> minor...?

You can look at the changes.txt of 1.1.1-ea to get an idea:

   http://fisheye4.atlassian.com/browse/~raw,r=2489/jersey/trunk/
jersey/changes.txt


> Just being cautious as almost at the end of a project...
>

If you wish you can always copy the code for EntityHolder and the
corresponding message body reader:

http://fisheye4.atlassian.com/browse/~raw,r=2404/jersey/trunk/jersey/
jersey-core/src/main/java/com/sun/jersey/core/impl/provider/entity/
EntityHolderReader.java

Paul.

> Many thanks,
> Jon.
>
> 2009/8/13 Paul Sandoz <Paul.Sandoz_at_sun.com>
> H Jon,
>
> You can do use EntityHolder:
>
> https://jersey.dev.java.net/nonav/apidocs/1.1.1-ea/jersey/com/sun/jersey/core/provider/EntityHolder.html
>
> For example:
>
>
> @POST
> @Consumes(MediaType.APPLICATION_XML)
> @Path("/{name}")
> public Response getSummary(EntityHolder<Animal> ah,
> @PathParam(MessageConstants.NAME_PARAM) final String name
> @DefaultValue("none") {
> Animal a = (ah.hasEntity()) ? ah.getEntity() : new Animal();
> }
>
>
> For compliance with JAX-RS we need to ensure that the JAXB providers
> require that an XML document be present. The semantics of dealing
> with the presence or not of an entity really need to be determined
> by the application and EntityHolder provides a generic solution to
> help in that regard.
>
> Paul.
>
>
> On Aug 13, 2009, at 10:32 PM, Jonathan Holloway wrote:
>
> Hi,
>
> Please excuse the silly example, but I'm wondering whether it's
> possible to have JAXB annotated objects that are optional with
> Jersey/JAX-RS.
> In the example below I have an animal object which I bind to based
> on the XML present in the body of the POST request.
>
> @POST
> @Consumes(MediaType.APPLICATION_XML)
> @Path("/{name}")
> public Response getSummary(Animal animal,
> @PathParam(MessageConstants.NAME_PARAM) final String name
> @DefaultValue("none")
>
> However, what I want to do is to be able to cope with the body not
> containing an XML block. In this case I don't want it to attempt to
> find a reader for
> Animal, it would be great if I could just have it construct an empty
> Animal instance using the default constructor. At present this
> fails with:
>
> A message body reader for Java type, class com.test.Animal and MIME
> media type, application/octet-stream, was not found
>
> Is there a way to do this?
>
> Many thanks,
> Jon.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>