You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cxf.apache.org by "KARR, DAVID (ATTSI)" <dk...@att.com> on 2011/04/26 18:02:12 UTC

Unmarshalling JSON to Java fails, even though corresponding XML works fine

I'm returning an instance of the following paraphrased class from my handler method:

@XmlType(name = "activeThings", propOrder = {"things"})
@XmlRootElement(name = "activeThings")
public class GetActiveThingsResponse extends ResponseBase {
    @XmlElementWrapper(name = "things")
    @XmlElement(name = "thing")
    private List<Thing>    things;

Where "ResponseBase" looks like this:

@XmlType(name = "responseBase")
public class ResponseBase {
    @XmlElementWrapper(name = "errors")
    @XmlElement(name = "error")
    private List<Error> errors;
    private boolean lastBlock;

When this is rendered as XML, it looks like this:

<activeThings xmlns="namespace"><lastBlock>false</lastBlock><things><thing><id>1</id></thing></things></activeThings>

This is fine.

When it's rendered as JSON, it looks like this:

{"things":[{"id":1,"longDescription":null,"displayName":null,"description":null}],"errors":[],"lastBlock":false}

This looks reasonable, but when WebClient tries to "get" the object of this type, it fails with this:

Apr 26, 2011 8:48:05 AM org.apache.cxf.jaxrs.provider.AbstractJAXBProvider handleJAXBException
WARNING: javax.xml.bind.UnmarshalException
 - with linked exception:
[javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"things"). Expected elements are <{namespace}activeThings>]

This is happening in my unit tests, not in my core code, but it's still preventing me from testing some aspects of the JSON rendering.

Re: Unmarshalling JSON to Java fails, even though corresponding XML works fine

Posted by Sergey Beryozkin <sb...@gmail.com>.
>>
>> Do you use Jackson or default Jettison provider ?
>
> Jackson.  Will this be a significant problem?  It's been a while since I've looked at this difference, so I don't remember why I went with Jackson, as opposed to Jettison.  I can't change this right now, as I'm on the edge of a release, but I'll look into changing back to Jettison for the next release, if you think that will make a difference.
>

Just make sure you also register a Jackson provider on the client side:

WebClient.create(address, Collections.singletonList(new JacksonProvider()));

Jackson drops the top level activeThings element, but Jettison-based
provider expects it.
Jettison provider can actually be configured to append it to the
incoming payload, but I guess using the same provider on the client
side will be simpler

Cheers, Sergey

>> On Tue, Apr 26, 2011 at 5:02 PM, KARR, DAVID (ATTSI) <dk...@att.com>
>> wrote:
>> > I'm returning an instance of the following paraphrased class from my
>> handler method:
>> >
>> > @XmlType(name = "activeThings", propOrder = {"things"})
>> > @XmlRootElement(name = "activeThings")
>> > public class GetActiveThingsResponse extends ResponseBase {
>> >    @XmlElementWrapper(name = "things")
>> >    @XmlElement(name = "thing")
>> >    private List<Thing>    things;
>> >
>> > Where "ResponseBase" looks like this:
>> >
>> > @XmlType(name = "responseBase")
>> > public class ResponseBase {
>> >    @XmlElementWrapper(name = "errors")
>> >    @XmlElement(name = "error")
>> >    private List<Error> errors;
>> >    private boolean lastBlock;
>> >
>> > When this is rendered as XML, it looks like this:
>> >
>> > <activeThings
>> xmlns="namespace"><lastBlock>false</lastBlock><things><thing><id>1</id>
>> </thing></things></activeThings>
>> >
>> > This is fine.
>> >
>> > When it's rendered as JSON, it looks like this:
>> >
>> >
>> {"things":[{"id":1,"longDescription":null,"displayName":null,"descripti
>> on":null}],"errors":[],"lastBlock":false}
>> >
>> > This looks reasonable, but when WebClient tries to "get" the object
>> of this type, it fails with this:
>> >
>> > Apr 26, 2011 8:48:05 AM
>> org.apache.cxf.jaxrs.provider.AbstractJAXBProvider handleJAXBException
>> > WARNING: javax.xml.bind.UnmarshalException
>> >  - with linked exception:
>> > [javax.xml.bind.UnmarshalException: unexpected element (uri:"",
>> local:"things"). Expected elements are <{namespace}activeThings>]
>> >
>> > This is happening in my unit tests, not in my core code, but it's
>> still preventing me from testing some aspects of the JSON rendering.
>> >
>



-- 
Sergey Beryozkin

Application Integration Division of Talend
http://sberyozkin.blogspot.com

RE: Unmarshalling JSON to Java fails, even though corresponding XML works fine

Posted by "KARR, DAVID (ATTSI)" <dk...@att.com>.
> -----Original Message-----
> From: Sergey Beryozkin [mailto:sberyozkin@gmail.com]
> Sent: Tuesday, April 26, 2011 9:20 AM
> To: users@cxf.apache.org
> Subject: Re: Unmarshalling JSON to Java fails, even though
> corresponding XML works fine
> 
> Do you use Jackson or default Jettison provider ?

Jackson.  Will this be a significant problem?  It's been a while since I've looked at this difference, so I don't remember why I went with Jackson, as opposed to Jettison.  I can't change this right now, as I'm on the edge of a release, but I'll look into changing back to Jettison for the next release, if you think that will make a difference.

> On Tue, Apr 26, 2011 at 5:02 PM, KARR, DAVID (ATTSI) <dk...@att.com>
> wrote:
> > I'm returning an instance of the following paraphrased class from my
> handler method:
> >
> > @XmlType(name = "activeThings", propOrder = {"things"})
> > @XmlRootElement(name = "activeThings")
> > public class GetActiveThingsResponse extends ResponseBase {
> >    @XmlElementWrapper(name = "things")
> >    @XmlElement(name = "thing")
> >    private List<Thing>    things;
> >
> > Where "ResponseBase" looks like this:
> >
> > @XmlType(name = "responseBase")
> > public class ResponseBase {
> >    @XmlElementWrapper(name = "errors")
> >    @XmlElement(name = "error")
> >    private List<Error> errors;
> >    private boolean lastBlock;
> >
> > When this is rendered as XML, it looks like this:
> >
> > <activeThings
> xmlns="namespace"><lastBlock>false</lastBlock><things><thing><id>1</id>
> </thing></things></activeThings>
> >
> > This is fine.
> >
> > When it's rendered as JSON, it looks like this:
> >
> >
> {"things":[{"id":1,"longDescription":null,"displayName":null,"descripti
> on":null}],"errors":[],"lastBlock":false}
> >
> > This looks reasonable, but when WebClient tries to "get" the object
> of this type, it fails with this:
> >
> > Apr 26, 2011 8:48:05 AM
> org.apache.cxf.jaxrs.provider.AbstractJAXBProvider handleJAXBException
> > WARNING: javax.xml.bind.UnmarshalException
> >  - with linked exception:
> > [javax.xml.bind.UnmarshalException: unexpected element (uri:"",
> local:"things"). Expected elements are <{namespace}activeThings>]
> >
> > This is happening in my unit tests, not in my core code, but it's
> still preventing me from testing some aspects of the JSON rendering.
> >

Re: Unmarshalling JSON to Java fails, even though corresponding XML works fine

Posted by Sergey Beryozkin <sb...@gmail.com>.
Do you use Jackson or default Jettison provider ?

Cheers, Sergey

On Tue, Apr 26, 2011 at 5:02 PM, KARR, DAVID (ATTSI) <dk...@att.com> wrote:
> I'm returning an instance of the following paraphrased class from my handler method:
>
> @XmlType(name = "activeThings", propOrder = {"things"})
> @XmlRootElement(name = "activeThings")
> public class GetActiveThingsResponse extends ResponseBase {
>    @XmlElementWrapper(name = "things")
>    @XmlElement(name = "thing")
>    private List<Thing>    things;
>
> Where "ResponseBase" looks like this:
>
> @XmlType(name = "responseBase")
> public class ResponseBase {
>    @XmlElementWrapper(name = "errors")
>    @XmlElement(name = "error")
>    private List<Error> errors;
>    private boolean lastBlock;
>
> When this is rendered as XML, it looks like this:
>
> <activeThings xmlns="namespace"><lastBlock>false</lastBlock><things><thing><id>1</id></thing></things></activeThings>
>
> This is fine.
>
> When it's rendered as JSON, it looks like this:
>
> {"things":[{"id":1,"longDescription":null,"displayName":null,"description":null}],"errors":[],"lastBlock":false}
>
> This looks reasonable, but when WebClient tries to "get" the object of this type, it fails with this:
>
> Apr 26, 2011 8:48:05 AM org.apache.cxf.jaxrs.provider.AbstractJAXBProvider handleJAXBException
> WARNING: javax.xml.bind.UnmarshalException
>  - with linked exception:
> [javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"things"). Expected elements are <{namespace}activeThings>]
>
> This is happening in my unit tests, not in my core code, but it's still preventing me from testing some aspects of the JSON rendering.
>