users@jersey.java.net

Re: [Jersey] RE: JAXB/JSON Array reading problem (Array writing is OK)

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Fri, 22 Aug 2008 09:49:14 +0200

Hi Tylor,

I have forwarded the information you have provided the the RESTful
tooling guys.

Thanks for all the detailed information,
Paul.

On Aug 19, 2008, at 8:54 PM, Tyson Norris wrote:

> Hi All -
> FYI, I have narrowed this down to a JAXB Collection processing issue.
> The procedure for setting collection data on a JAXB bean property is
> something like:
> - get the Collection using the property getter
> - add values to the Collection
>
> Note that there is no use of the Collection property setter. I also
> found additional references to related issues such as "my JAXB
> generated
> bean has no setter for a Collection property", and a plugin to add
> setter functionality via XJC:
> https://jaxb2-commons.dev.java.net/collection-setter-injector/
>
> The reason I ran into this, was the NetBeans RESTful code generator
> creates Converter classes that manipulate entity properties directly.
> This does not work well for Collection properties, since the
> Collection
> of the wrapped entity will contain entities which need to be
> translated
> to the appropriate converter classes. I will get around this by
> managing
> entity/converter collection property translation when the *entity* is
> retrieved from the converter, not when the collection property is
> set/got.
>
> To demonstrate, you make the converter return an unmodifiable
> Collection.
> So, when I have a JAXB bean property like:
> @XmlElement
> public Collection<MyType> getCollectionValue() {
> if (collectionValue == null){
> collectionValue = new ArrayList<MyType>();
> }
> return Collections.unmodifiableCollection(collectionValue);
> }
>
> public void setCollectionValue(Collection<MyType>
> collectionValue) {
> this.collectionValue = collectionValue;
> }
>
> I get an exception like:
>
> com.sun.jersey.api.container.ContainerException: Exception injecting
> parameters to Web resource method
> at
> com.sun.jersey.impl.model.method.dispatch.EntityParamDispatchProvider$
> En
> tityParamInInvoker.getParams(EntityParamDispatchProvider.java:103)
> at
> com.sun.jersey.impl.model.method.dispatch.EntityParamDispatchProvider$
> Re
> sponseOutInvoker._dispatch(EntityParamDispatchProvider.java:148)
>
> ...[snipped]...
> Caused by: java.lang.UnsupportedOperationException
> at
> java.util.Collections$UnmodifiableCollection.clear(Collections.java:
> 1037
> )
> at
> com.sun.xml.bind.v2.runtime.reflect.Lister
> $CollectionLister.startPacking
> (Lister.java:296)
> at
> com.sun.xml.bind.v2.runtime.reflect.Lister
> $CollectionLister.startPacking
> (Lister.java:265)
> at
> com.sun.xml.bind.v2.runtime.unmarshaller.Scope.add(Scope.java:114)
> at
> com.sun.xml.bind.v2.runtime.property.ArrayERProperty
> $ReceiverImpl.receiv
> e(ArrayERProperty.java:206)
> at
> com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext.endEleme
> nt
> (UnmarshallingContext.java:486)
>
>
> Thanks for the feedback!
> Tyson
>
> -----Original Message-----
> From: Jakub.Podlesak_at_Sun.COM [mailto:Jakub.Podlesak_at_Sun.COM]
> Sent: Monday, August 18, 2008 3:46 PM
> To: users_at_jersey.dev.java.net
> Subject: Re: [Jersey] RE: JAXB/JSON Array reading problem (Array
> writing
> is OK)
>
>
> Hi Tyson,
>
> please see inline...
>
> On Mon, Aug 18, 2008 at 10:30:23AM -0700, Tyson Norris wrote:
>> Thanks Paul.
>>
>>
>>
>> You are right - my case is NOT returning a Collection, but
>> returning a
>> JAXB bean that has a Collection property.
>>
>>
>>
>> More testing indicated that:
>>
>> Collection/List typed property:
>>
>> - JSON: Collection/List property is returned via GET;
> property
>> is NOT assigned when submitting similar valid format via POST (i.e.
>> POSTing the same data that was returned from GET does not work)
>>
>> - XML: Collection/List property is returned properly via
>> GET;
>> property is NOT assigned when submitting similar valid format via
>> POST
>> (need to verify this again, but 99% sure this was the case)
>
> Then the behaviour is same whatever mime-type is used, right?
> It would indicate no bug in JSON.
> Maybe a bug in jaxb, then, maybe there is missing something.
> Sending out your JAXB beans would be helpful.
>>
>> Array typed property:
>>
>> - JSON: Array property is returned via GET and assigned via
>> POST
>>
>> - XML: need to test this
>
> I would think XML should work in this case, as JSON defers to JAXB/
> XML.
> You use the same JAXB beans, right?
>>
>>
>>
>> I will work out a simple test and pass it on the list ASAP.
>
> Great, feel free to directly report a bug at [1] and attach your test
> case there.
>
> ~Jakub
>
> [1]https://jersey.dev.java.net/issues/
>
>>
>>
>>
>> Thanks
>>
>> Tyson
>>
>>
>>
>>
>>
>> ________________________________
>>
>> From: Paul.Sandoz_at_Sun.COM [mailto:Paul.Sandoz_at_Sun.COM]
>> Sent: Monday, August 18, 2008 12:24 AM
>> To: users_at_jersey.dev.java.net
>> Subject: Re: [Jersey] RE: JAXB/JSON Array reading problem (Array
> writing
>> is OK)
>>
>>
>>
>> Hi Tyson,
>>
>>
>>
>> That bug is related to a resource method returning a collection. If i
>> understand correctly, in your case, it is something to do with a
>> collection as a JAXB property i.e. your top-level JAXB beans are
> getting
>> instantiated but some part of it is not.
>>
>>
>>
>> Perhaps you can verify the following:
>>
>>
>>
>> 1) Returning an instance of MyObj as XML or JSON. It may be that the
>> JSON that is generated is not what you expect. In general JAXB
>> ignores
>> stuff it does not understand so it could be ignoring the JSON
>> information associated with "collectionValues" because it does not
> have
>> the required XML infoset structure i.e. do
>>
>>
>>
>> @GET
>>
>> @Produces(<xml and json>)
>>
>> public MyObj get() {
>>
>> MyObj o = ....
>>
>> return o;
>>
>> }
>>
>>
>>
>> 2) Check that it works with XML.
>>
>>
>>
>> Also can you send the JAXB beans?
>>
>>
>>
>> Thanks,
>>
>> Paul.
>>
>>
>>
>> On Aug 16, 2008, at 12:16 AM, Tyson Norris wrote:
>>
>>
>>
>>
>>
>> Whoops...
>>
>> Just noticed this:
>>
>> https://jersey.dev.java.net/issues/show_bug.cgi?id=18
>>
>>
>>
>> I will try out creating a Collection wrapper.
>>
>>
>>
>> Sorry for the noise...
>>
>> Tyson
>>
>>
>>
>> ________________________________
>>
>> From: Tyson Norris
>> Sent: Friday, August 15, 2008 2:49 PM
>> To: 'users_at_jersey.dev.java.net'
>> Subject: RE: JAXB/JSON Array reading problem (Array writing is OK)
>>
>>
>>
>> Some additional info I forgot to mention:
>>
>> - version: I'm using 0.8ea version of Jersey
>>
>> - my collection elements are getting created properly, and
> the
>> collection is read from the converter (by JAXB unmarshaller) but the
>> collection is never assigned to the converter property.
>>
>> I will try a similar test with XML data and see what happens...
>>
>> Tyson
>>
>>
>>
>> ________________________________
>>
>> From: Tyson Norris
>> Sent: Friday, August 15, 2008 2:31 PM
>> To: 'users_at_jersey.dev.java.net'
>> Subject: JAXB/JSON Array reading problem (Array writing is OK)
>>
>>
>>
>> Hi -
>>
>> I am trying to send JSON data into a jersey service using HTTP POST.
>>
>>
>>
>> This works, in general, for me, except one JAXB property is type
>> Collection, and JAXB does not seem to unmarshall the collection; the
>> collection value (on the JAXB converter) is always null, and the
> setter
>> is never called.
>>
>>
>>
>> For example, I am sending something like:
>>
>>
>>
>> {"myObj":{"prop1" : "value1", "prop2" : "value2",
>> "collectionValues" :
>> [{"itemProp1" : itemvalue1", "itemProp2":"itemValue2"}]}}
>>
>>
>>
>> My JAXB converter is created properly, and non-collection property
>> values are assigned (i.e. I see that getProp1() returns "value1",
> etc),
>> but: setCollectionValues() is never invoked, and therefore my
> collection
>> values are not available in the JAXB object.
>>
>>
>>
>> When I use the same JAXB Object to return data in JSON format, the
>> collectionValues are properly marshaled to the same JSON format
>> listed
>> here.
>>
>>
>>
>> I have tried changing the type of collection in the
>> getCollectionValues/setCollectionValues methods from
>> java.util.Collection<ItemPropConverter> to
>> java.util.List<ItemPropConverter>, but I get the same results
>> (collection is never set when sending data in, data is properly
>> translated to JSON when sending data out).
>>
>>
>>
>> Any ideas what might cause this?
>>
>>
>>
>> Thanks!
>>
>> Tyson
>>
>>
>>
>>
>>
>
> --
> http://blogs.sun.com/japod
>
> ---------------------------------------------------------------------
> 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
>