You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cxf.apache.org by Mark Streit <mc...@gmail.com> on 2012/03/24 18:47:34 UTC

CXF/Jackson and JAXB propOrder attribute - JSON output

I hope I have what MIGHT be a simple question.  (one article I found on
Google indicated that this *should *work, however I also found a number of
threads relating to this where it was commented that JSON objects have
fields which are inherently UNORDERED).  We have a case where the consumer
is expecting the JSON to adhere to a JSON schema that has been pre-defined.

Have a simple Book class annotated as shown:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name="Book")
@XmlType(name = "Book",
namespace="http://dvo.services.book.acme.com/", *propOrder
*= { "bookID", "bookTitle", "authorName", "ISBN", "bookTypeCode",
"bookPublisher", "bookRetail", "bookCost", "pageCount" })
public class Book {

Using a JAX-RS annotated service class (for a REST endpoint) works
perfectly returning XML when the Accept header is application/xml... the
XML returned *honors *the propOrder attribute *as specified*.

However, if I pass the Accept header as application/json - I DO get a
complete JSON response, as expected... except the propOrder appears to
be *ignored
*- the elements come back in a different order.

I have the JSON provider for Jackson set as follows in the beans
configuration file (called cxf-beans.xml in my case)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:jaxws="http://cxf.apache.org/jaxws"
    xmlns:jaxrs="http://cxf.apache.org/jaxrs"
    xsi:schemaLocation="
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    http://cxf.apache.org/jaxws
    http://cxf.apache.org/schemas/jaxws.xsd
    http://cxf.apache.org/jaxrs
    http://cxf.apache.org/schemas/jaxrs.xsd">

    <import resource="classpath:META-INF/cxf/cxf.xml" />
    <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
    <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />

    <bean id="jsonProvider"
class="org.codehaus.jackson.jaxrs.JacksonJsonProvider"/>

    <!-- JAX-RS -->
    <jaxrs:server id="bookService" address="/RS">
        <jaxrs:serviceBeans>
            <ref bean="bookServiceRS" />
        </jaxrs:serviceBeans>
        <jaxrs:providers>
            <ref bean="jsonProvider"/>
        </jaxrs:providers>
    </jaxrs:server>
...
...

I will keep looking but thought I'd post this in case someone has seen this
behavior before.  I am guessing I have missed something in telling Jackson
how to serialize, perhaps...

I do not have any sort of custom JSON serializer configured - just relying
on whatever OOTB behavior JAXB/Jackson provide when used with CXF - (using
version 2.5.2).

Thanks

*
*

Re: CXF/Jackson and JAXB propOrder attribute - JSON output

Posted by Sergey Beryozkin <sb...@gmail.com>.
Hi, I updated the documentation to get JacksonJaxbJsonProvider 
referenced as well

Thanks, Sergey
On 24/03/12 21:05, Mark Streit wrote:
> Think I SOLVED this.  Appears the issue is with the following line in
> cxf-beans.xml file...
>
> <bean id="jsonProvider" class="org.codehaus.jackson.jaxrs.*
> JacksonJsonProvider*"/>
>
> SHOULD be:
>
> <bean id="jsonProvider" class="org.codehaus.jackson.jaxrs.*
> JacksonJaxbJsonProvider*"/>
>
> Simply changing that class reference, appears to have solved the issue.  *Now
> *the JSON element order matches the XML element order in the response
> output.
>
> Hopefully this is indeed the correction.
>
>
> On Sat, Mar 24, 2012 at 1:47 PM, Mark Streit<mc...@gmail.com>  wrote:
>
>> I hope I have what MIGHT be a simple question.  (one article I found on
>> Google indicated that this *should *work, however I also found a number
>> of threads relating to this where it was commented that JSON objects have
>> fields which are inherently UNORDERED).  We have a case where the consumer
>> is expecting the JSON to adhere to a JSON schema that has been pre-defined.
>>
>> Have a simple Book class annotated as shown:
>>
>> @XmlAccessorType(XmlAccessType.FIELD)
>> @XmlRootElement(name="Book")
>> @XmlType(name = "Book", namespace="http://dvo.services.book.acme.com/", *propOrder
>> *= { "bookID", "bookTitle", "authorName", "ISBN", "bookTypeCode",
>> "bookPublisher", "bookRetail", "bookCost", "pageCount" })
>> public class Book {
>>
>> Using a JAX-RS annotated service class (for a REST endpoint) works
>> perfectly returning XML when the Accept header is application/xml... the
>> XML returned *honors *the propOrder attribute *as specified*.
>>
>> However, if I pass the Accept header as application/json - I DO get a
>> complete JSON response, as expected... except the propOrder appears to be
>> *ignored *- the elements come back in a different order.
>>
>> I have the JSON provider for Jackson set as follows in the beans
>> configuration file (called cxf-beans.xml in my case)
>>
>> <?xml version="1.0" encoding="UTF-8"?>
>> <beans xmlns="http://www.springframework.org/schema/beans"
>>      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
>>      xmlns:jaxws="http://cxf.apache.org/jaxws"
>>      xmlns:jaxrs="http://cxf.apache.org/jaxrs"
>>      xsi:schemaLocation="
>>      http://www.springframework.org/schema/beans
>>      http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
>>      http://cxf.apache.org/jaxws
>>      http://cxf.apache.org/schemas/jaxws.xsd
>>      http://cxf.apache.org/jaxrs
>>      http://cxf.apache.org/schemas/jaxrs.xsd">
>>
>>      <import resource="classpath:META-INF/cxf/cxf.xml" />
>>      <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
>>      <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
>>
>>      <bean id="jsonProvider"
>> class="org.codehaus.jackson.jaxrs.JacksonJsonProvider"/>
>>
>>      <!-- JAX-RS -->
>>      <jaxrs:server id="bookService" address="/RS">
>>          <jaxrs:serviceBeans>
>>              <ref bean="bookServiceRS" />
>>          </jaxrs:serviceBeans>
>>          <jaxrs:providers>
>>              <ref bean="jsonProvider"/>
>>          </jaxrs:providers>
>>      </jaxrs:server>
>> ...
>> ...
>>
>> I will keep looking but thought I'd post this in case someone has seen
>> this behavior before.  I am guessing I have missed something in telling
>> Jackson how to serialize, perhaps...
>>
>> I do not have any sort of custom JSON serializer configured - just relying
>> on whatever OOTB behavior JAXB/Jackson provide when used with CXF - (using
>> version 2.5.2).
>>
>> Thanks
>>
>> *
>> *
>>
>
>
> *
> *
>

Re: CXF/Jackson and JAXB propOrder attribute - JSON output

Posted by Mark Streit <mc...@gmail.com>.
Think I SOLVED this.  Appears the issue is with the following line in
cxf-beans.xml file...

<bean id="jsonProvider" class="org.codehaus.jackson.jaxrs.*
JacksonJsonProvider*"/>

SHOULD be:

<bean id="jsonProvider" class="org.codehaus.jackson.jaxrs.*
JacksonJaxbJsonProvider*"/>

Simply changing that class reference, appears to have solved the issue.  *Now
*the JSON element order matches the XML element order in the response
output.

Hopefully this is indeed the correction.


On Sat, Mar 24, 2012 at 1:47 PM, Mark Streit <mc...@gmail.com> wrote:

> I hope I have what MIGHT be a simple question.  (one article I found on
> Google indicated that this *should *work, however I also found a number
> of threads relating to this where it was commented that JSON objects have
> fields which are inherently UNORDERED).  We have a case where the consumer
> is expecting the JSON to adhere to a JSON schema that has been pre-defined.
>
> Have a simple Book class annotated as shown:
>
> @XmlAccessorType(XmlAccessType.FIELD)
> @XmlRootElement(name="Book")
> @XmlType(name = "Book", namespace="http://dvo.services.book.acme.com/", *propOrder
> *= { "bookID", "bookTitle", "authorName", "ISBN", "bookTypeCode",
> "bookPublisher", "bookRetail", "bookCost", "pageCount" })
> public class Book {
>
> Using a JAX-RS annotated service class (for a REST endpoint) works
> perfectly returning XML when the Accept header is application/xml... the
> XML returned *honors *the propOrder attribute *as specified*.
>
> However, if I pass the Accept header as application/json - I DO get a
> complete JSON response, as expected... except the propOrder appears to be
> *ignored *- the elements come back in a different order.
>
> I have the JSON provider for Jackson set as follows in the beans
> configuration file (called cxf-beans.xml in my case)
>
> <?xml version="1.0" encoding="UTF-8"?>
> <beans xmlns="http://www.springframework.org/schema/beans"
>     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
>     xmlns:jaxws="http://cxf.apache.org/jaxws"
>     xmlns:jaxrs="http://cxf.apache.org/jaxrs"
>     xsi:schemaLocation="
>     http://www.springframework.org/schema/beans
>     http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
>     http://cxf.apache.org/jaxws
>     http://cxf.apache.org/schemas/jaxws.xsd
>     http://cxf.apache.org/jaxrs
>     http://cxf.apache.org/schemas/jaxrs.xsd">
>
>     <import resource="classpath:META-INF/cxf/cxf.xml" />
>     <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
>     <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
>
>     <bean id="jsonProvider"
> class="org.codehaus.jackson.jaxrs.JacksonJsonProvider"/>
>
>     <!-- JAX-RS -->
>     <jaxrs:server id="bookService" address="/RS">
>         <jaxrs:serviceBeans>
>             <ref bean="bookServiceRS" />
>         </jaxrs:serviceBeans>
>         <jaxrs:providers>
>             <ref bean="jsonProvider"/>
>         </jaxrs:providers>
>     </jaxrs:server>
> ...
> ...
>
> I will keep looking but thought I'd post this in case someone has seen
> this behavior before.  I am guessing I have missed something in telling
> Jackson how to serialize, perhaps...
>
> I do not have any sort of custom JSON serializer configured - just relying
> on whatever OOTB behavior JAXB/Jackson provide when used with CXF - (using
> version 2.5.2).
>
> Thanks
>
> *
> *
>


*
*