You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cxf.apache.org by Jim Talbut <jt...@spudsoft.co.uk> on 2013/02/13 12:49:14 UTC

Can't deserialize JAX-RS using client.readEntity

Hi,

I have a class defined as:
         @XmlAccessorType(XmlAccessType.FIELD)
         @XmlRootElement( name = "" )
         @XmlType(name = "ReportRunResult", propOrder = {
             "runInformation",
         })
         public class ReportRunResult    implements Serializable
         {

             @XmlElement(name = "RunInformation", required = true)
             protected ReportRunInformationType runInformation;
             ...

I return it from a JAX-RS method as such:
         @Override
         @Path( "/run" )
         @Consumes({ "application/json", "application/xml" })
         @POST
         public ReportRunResult runReport( RunReportOptions 
runReportOptions ) {
             ...
             try {
                 ...
                 ReportRunResult result = new ReportRunResult();
result.setRunInformation(response.getRunInformation());
                 return result;

But I can't deserialise it like this:
         Response response = client.post( runOpts );
         assertEquals(200, response.getStatus() );
         ReportRunResult runResult = response.readEntity( 
ReportRunResult.class );

This produces an error of:
         javax.xml.bind.UnmarshalException: unexpected element (uri:"", 
local:"RunInformation"). Expected elements are <{}>

If I read the entity as a string it gives me:
         {"RunInformation":{"RunHeader":{"Project":"Ringo",...

Looking here: 
http://cxf.apache.org/docs/jax-rs-data-bindings.html#JAX-RSDataBindings-DealingwithJSONarrayserializationissues
(the only place I can find an example of the JSON that is expected from 
CXF) shows that the top level object is contained with a wrapper JSON 
object:
{"post":{"title":"post","comments":[{"title":"comment1"},{"title":"comment2"}]}} 

But that isn't what happened in my case (no wrapper JSON object).

What do I need to change to get this to work correctly?

Thanks

Jim

Re: Can't deserialize JAX-RS using client.readEntity

Posted by Sergey Beryozkin <sb...@gmail.com>.
On 13/02/13 12:29, Jim Talbut wrote:
> On 13/02/2013 11:49, Jim Talbut wrote:
>> Hi,
>>
>> I have a class defined as:
>> @XmlAccessorType(XmlAccessType.FIELD)
>> @XmlRootElement( name = "" )
>> @XmlType(name = "ReportRunResult", propOrder = {
>> "runInformation",
>> })
>> public class ReportRunResult implements Serializable
>> {
>>
>> @XmlElement(name = "RunInformation", required = true)
>> protected ReportRunInformationType runInformation;
>> ...
>>
>> I return it from a JAX-RS method as such:
>> @Override
>> @Path( "/run" )
>> @Consumes({ "application/json", "application/xml" })
>> @POST
>> public ReportRunResult runReport( RunReportOptions
>> runReportOptions ) {
>> ...
>> try {
>> ...
>> ReportRunResult result = new ReportRunResult();
>> result.setRunInformation(response.getRunInformation());
>> return result;
>>
>> But I can't deserialise it like this:
>> Response response = client.post( runOpts );
>> assertEquals(200, response.getStatus() );
>> ReportRunResult runResult = response.readEntity(
>> ReportRunResult.class );
>>
>> This produces an error of:
>> javax.xml.bind.UnmarshalException: unexpected element (uri:"",
>> local:"RunInformation"). Expected elements are <{}>
>>
>> If I read the entity as a string it gives me:
>> {"RunInformation":{"RunHeader":{"Project":"Ringo",...
>>
>> Looking here:
>> http://cxf.apache.org/docs/jax-rs-data-bindings.html#JAX-RSDataBindings-DealingwithJSONarrayserializationissues
>>
>>
>> (the only place I can find an example of the JSON that is expected from
>> CXF) shows that the top level object is contained with a wrapper JSON
>> object:
>> {"post":{"title":"post","comments":[{"title":"comment1"},{"title":"comment2"}]}}
>>
>>
>> But that isn't what happened in my case (no wrapper JSON object).
>>
>> What do I need to change to get this to work correctly?
>>
>> Thanks
>>
>> Jim
>
> Ah, I've got it.
> The JSON providers were configured with dropRootElement = true, changing
> this to the following fixed it, and broke a load of my jQuery code, but
> that's OK :) :
> <jaxrs:providers>
> ...
> <bean id="jsonProvider"
> class="org.apache.cxf.jaxrs.provider.json.JSONProvider">
> <property name="dropRootElement" value="false"/>
> <property name="dropCollectionWrapperElement" value="false"/>
> <property name="serializeAsArray" value="true"/>
> <property name="ignoreNamespaces" value="true"/>
> <property name="arrayKeys" >
> <list>
> ...
> </list>
> </property>
> </bean>
> </jaxrs:providers>
>
Hi Jim - thanks for the confirmation, I was just typing a long 
explanation why it might've happened, got lost along the way, and then 
fortunately, this email has arrived :-)
Cheers, Sergey

Re: Can't deserialize JAX-RS using client.readEntity

Posted by Jim Talbut <jt...@spudsoft.co.uk>.
On 13/02/2013 11:49, Jim Talbut wrote:
> Hi,
>
> I have a class defined as:
>          @XmlAccessorType(XmlAccessType.FIELD)
>          @XmlRootElement( name = "" )
>          @XmlType(name = "ReportRunResult", propOrder = {
>              "runInformation",
>          })
>          public class ReportRunResult    implements Serializable
>          {
>
>              @XmlElement(name = "RunInformation", required = true)
>              protected ReportRunInformationType runInformation;
>              ...
>
> I return it from a JAX-RS method as such:
>          @Override
>          @Path( "/run" )
>          @Consumes({ "application/json", "application/xml" })
>          @POST
>          public ReportRunResult runReport( RunReportOptions
> runReportOptions ) {
>              ...
>              try {
>                  ...
>                  ReportRunResult result = new ReportRunResult();
> result.setRunInformation(response.getRunInformation());
>                  return result;
>
> But I can't deserialise it like this:
>          Response response = client.post( runOpts );
>          assertEquals(200, response.getStatus() );
>          ReportRunResult runResult = response.readEntity(
> ReportRunResult.class );
>
> This produces an error of:
>          javax.xml.bind.UnmarshalException: unexpected element (uri:"",
> local:"RunInformation"). Expected elements are <{}>
>
> If I read the entity as a string it gives me:
>          {"RunInformation":{"RunHeader":{"Project":"Ringo",...
>
> Looking here:
> http://cxf.apache.org/docs/jax-rs-data-bindings.html#JAX-RSDataBindings-DealingwithJSONarrayserializationissues
>
> (the only place I can find an example of the JSON that is expected from
> CXF) shows that the top level object is contained with a wrapper JSON
> object:
> {"post":{"title":"post","comments":[{"title":"comment1"},{"title":"comment2"}]}}
>
> But that isn't what happened in my case (no wrapper JSON object).
>
> What do I need to change to get this to work correctly?
>
> Thanks
>
> Jim

Ah, I've got it.
The JSON providers were configured with dropRootElement = true, changing 
this to the following fixed it, and broke a load of my jQuery code, but 
that's OK :)  :
         <jaxrs:providers>
...
             <bean id="jsonProvider" 
class="org.apache.cxf.jaxrs.provider.json.JSONProvider">
                 <property name="dropRootElement" value="false"/>
                 <property name="dropCollectionWrapperElement" 
value="false"/>
                 <property name="serializeAsArray" value="true"/>
                 <property name="ignoreNamespaces" value="true"/>
                 <property name="arrayKeys" >
                     <list>
...
                     </list>
                 </property>
             </bean>
         </jaxrs:providers>