JacksonJaxbJsonProvider doesn't work properly after upgrading CXF to 3.x

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

JacksonJaxbJsonProvider doesn't work properly after upgrading CXF to 3.x

Neehal
This post was updated on .
Hi,
I have karaf v4.1.5 and have installed CXF v3.2.2. What I notice is that, my
jackson serialization only works if I remove any XML JAXB annotations from
the response class.
I have my JacksonJaxbJsonProvider configured in the blueprint as a provider.

<bean id="jsonProvider" class="com.xyz.cxf.CustomJacksonJaxbJsonProvider">
        </bean>
<jaxrs:server id="xyz" address="/cxf">
                <jaxrs:serviceBeans>
                        <ref component-id="applicationWebService" />
                </jaxrs:serviceBeans>
                <jaxrs:providers>
                        <ref component-id="jsonProvider" /> 
                </jaxrs:providers>
        </jaxrs:server>

My 'CustomJacksonJaxbJsonProvider' extends 'JacksonJaxbJsonProvider', and I
have configured the following:

ObjectMapper mapper = new ObjectMapper();
mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, true);
mapper.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true);
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
setMapper(mapper);
setAnnotationsToUse(DEFAULT_ANNOTATIONS);


The above take effect only if do not have any JAXB annotations(e.g.
@XmlRootElement) in the response class.
My response has a map that shows like,
{ entry:[{ key: "xyz", value: "abc"}]}, instead of ,
{ "xyz" : "abc" }

This was not a problem with my earlier CXF v2.7.13 on Karaf. Has anyone else
faced a similar problem?

Regards,
Neehal.




--
Sent from: http://karaf.922171.n3.nabble.com/Karaf-User-f930749.html
Reply | Threaded
Open this post in threaded view
|

Re: JacksonJaxbJsonProvider doesn't work properly after upgrading CXF to 3.x

Łukasz Dywicki
Hey Neehal,
Have you checked if you have, by accident, two different versions of JAXB in runtime? One of reasons why annotations get ignored is class loading.

Kind regards,
Lukasz

> On 3 May 2018, at 11:39, Neehal <[hidden email]> wrote:
>
> Hi,
> I have karaf v4.1.5 and have installed CXF v3.2.2. What I notice is that, my
> jackson serialization only works if I remove any XML JAXB annotations from
> the response class.
> I have my JacksonJaxbJsonProvider configured in the blueprint as a provider.
>
> <bean id="jsonProvider" class="com.xyz.cxf.CustomJacksonJaxbJsonProvider">
> </bean>
> <jaxrs:server id="xyz" address="/cxf">
> <jaxrs:serviceBeans>
> <ref component-id="applicationWebService" />
> </jaxrs:serviceBeans>
> <jaxrs:providers>
> <ref component-id="jsonProvider" />  
> </jaxrs:providers>
> </jaxrs:server>
>
> My 'CustomJacksonJaxbJsonProvider' extends 'JacksonJaxbJsonProvider', and I
> have configured the following:
>
> ObjectMapper mapper = new ObjectMapper();
> mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, true);
> mapper.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true);
> mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
> setMapper(mapper);
> setAnnotationsToUse(DEFAULT_ANNOTATIONS);
>
>
> The above take effect only if do not have any JAXB annotations(e.g.
> @XmlRootElement) in the response class.
> My response has a map that shows like,
> { entry: { key: "xyz", value: "abc"}}, instead of ,
> { "xyz" : "abc" }
>
> This was not a problem with my earlier CXF v2.7.13 on Karaf. Has anyone else
> faced a similar problem?
>
> Regards,
> Neehal.
>
>
>
>
> --
> Sent from: http://karaf.922171.n3.nabble.com/Karaf-User-f930749.html

Reply | Threaded
Open this post in threaded view
|

Re: JacksonJaxbJsonProvider doesn't work properly after upgrading CXF to 3.x

Neehal
Hi Lukasz,
Thank you for you reply. I actually have found a solution to this problem.
It all happened because CXF 3.x registers its own JSONProvider and uses it
by default. Hence even if want to use JacksonJaxbJsonProvider or
JacksonJsonProvider, it wont be honored if we had any Jaxb annotations in
our code. I removed my Jaxb annotations and it all worked fine. But we
actually wanted to use Jaxb annotations too. So we needed a way to skip the
registration of the default JSONProvider.

We put the following in our blueprint where we defined our rest endpoints:

<cxf:bus>
        ...
        <cxf:properties>
         
          <entry key="skip.default.json.provider.registration" value="true"
></entry>
        </cxf:properties>
    </cxf:bus>


I found this solution at https://access.redhat.com/solutions/3150741





--
Sent from: http://karaf.922171.n3.nabble.com/Karaf-User-f930749.html