cxf-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jim Talbut <jtal...@spudsoft.co.uk>
Subject Re: Can't deserialize JAX-RS using client.readEntity
Date Wed, 13 Feb 2013 12:29:21 GMT
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>


Mime
View raw message