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" <dk...@att.com> on 2013/11/06 17:47:51 UTC

Concise summary of steps to integrate Jackson 2.x into CXF with Spring and JAXB annotations?

I'm trying to investigate integrating Jackson 2.x into a Spring CXF JAX-RS prototype, using JAXB annotations to facilitate both XML and JSON rendering.  I've browsed the Jackson documentation, but it seems scattered.  I have an older CXF JAX-RS app using Jackson 1.x, but I'd like to see what the newer Jackson does.  I've seen some examples on the web using the older Jackson, but not the new one.  Is there a straightforward guide to this somewhere?

Re: Concise summary of steps to integrate Jackson 2.x into CXF with Spring and JAXB annotations?

Posted by Sergey Beryozkin <sb...@gmail.com>.
On 06/11/13 16:47, KARR, DAVID wrote:
> I'm trying to investigate integrating Jackson 2.x into a Spring CXF JAX-RS prototype, using JAXB annotations to facilitate both XML and JSON rendering.  I've browsed the Jackson documentation, but it seems scattered.  I have an older CXF JAX-RS app using Jackson 1.x, but I'd like to see what the newer Jackson does.  I've seen some examples on the web using the older Jackson, but not the new one.  Is there a straightforward guide to this somewhere?
>
As far as CXF is concerned it should work with Jackson 2 starting from 
CXF 2.7.6

Cheers, Sergey

-- 
Sergey Beryozkin

Talend Community Coders
http://coders.talend.com/

Blog: http://sberyozkin.blogspot.com

Re: Concise summary of steps to integrate Jackson 2.x into CXF with Spring and JAXB annotations?

Posted by Aaron Titus <at...@fwdco.com>.
I don't use Maven. (While I realize that I may be in the minority in that
regard, I prefer not to from personal choice.)  There was a link from one
of the maven repository to get the jar but it didn't work. So I built them
instead.




On Wed, Nov 6, 2013 at 12:21 PM, KARR, DAVID <dk...@att.com> wrote:

> > -----Original Message-----
> > From: Aaron Titus [mailto:atitus@fwdco.com]
> > Sent: Wednesday, November 06, 2013 9:11 AM
> > To: users@cxf.apache.org
> > Subject: Re: Concise summary of steps to integrate Jackson 2.x into CXF
> with
> > Spring and JAXB annotations?
> >
> > The custom provider can be as simple as this:
> >
> > package mypackage;
> >
> > import com.fasterxml.jackson.databind.DeserializationFeature;
> > import com.fasterxml.jackson.databind.ObjectMapper;
> > import com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider;
> > import com.fasterxml.jackson.module.jaxb.JaxbAnnotationModule;
> >
> > public class CustomJsonProvider extends JacksonJaxbJsonProvider {
> >
> > // this class exists so we can customize the object mapper
> > // and therefore the way that the JSON is produced
> >  public CustomJsonProvider() {
> >     ObjectMapper mapper = new ObjectMapper();
> >     mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
> >     JaxbAnnotationModule jaxbModule = new JaxbAnnotationModule();
> >     mapper.registerModule(jaxbModule);
> > this._mapperConfig.setMapper(mapper);
> >  }
> >
> >
> > And then in the configuration file, it looks like this:
> >
> >
> > <jaxrs:server id="restServer" address="/rest">
> >       <jaxrs:serviceBeans>
> >       ...
> >       </jaxrs:serviceBeans>
> >     <jaxrs:providers>
> >  <bean id="jaxbProvider" class="mypackage.CustomJsonProvider"/>
> >    </jaxrs:providers>
> > </jaxrs:server>
> >
> >
> > The jaxb annotation and providers needed to get built from github:
> >
> > https://github.com/FasterXML/jackson-module-jaxb-annotations.git
> >
> > https://github.com/FasterXML/jackson-jaxrs-providers.git
>
> Ok.  I'll give this a try.  I don't know why you had to build these.  The
> docs provide Maven coordinates for these.  Perhaps that was added after you
> did this?
>
> > On Wed, Nov 6, 2013 at 12:03 PM, Aaron Titus <at...@fwdco.com> wrote:
> >
> > > I just went through this myself, and found the same struggle.  One of
> the
> > > challenges is that with Jackson 2.0 is that the JAXB stuff is now in
> > > separate github projects, and there weren't any jars available.  I had
> to
> > > clone the github projects and build.  I don't use spring in my app
> directly
> > > but I do use the spring beans in the CXF configuration file. It was
> > > difficult to try and define the parameters I needed directly in there
> so I
> > > opted to extend the jackson jaxb provider class and then refer to my
> > > provider within the spring configuration file.  As Sergey mentioned,
> you
> > > also need to use CXF 2.7.6 or greater otherwise you run into another
> > > problem.
> > >
> > >
> > >
> > >
> > > On Wed, Nov 6, 2013 at 11:47 AM, KARR, DAVID <dk...@att.com> wrote:
> > >
> > >> I'm trying to investigate integrating Jackson 2.x into a Spring CXF
> > >> JAX-RS prototype, using JAXB annotations to facilitate both XML and
> JSON
> > >> rendering.  I've browsed the Jackson documentation, but it seems
> > scattered.
> > >>  I have an older CXF JAX-RS app using Jackson 1.x, but I'd like to see
> > what
> > >> the newer Jackson does.  I've seen some examples on the web using the
> > older
> > >> Jackson, but not the new one.  Is there a straightforward guide to
> this
> > >> somewhere?
> > >>
> > >
> > >
>

RE: Concise summary of steps to integrate Jackson 2.x into CXF with Spring and JAXB annotations?

Posted by "KARR, DAVID" <dk...@att.com>.
> -----Original Message-----
> From: Aaron Titus [mailto:atitus@fwdco.com]
> Sent: Wednesday, November 06, 2013 9:11 AM
> To: users@cxf.apache.org
> Subject: Re: Concise summary of steps to integrate Jackson 2.x into CXF with
> Spring and JAXB annotations?
> 
> The custom provider can be as simple as this:
> 
> package mypackage;
> 
> import com.fasterxml.jackson.databind.DeserializationFeature;
> import com.fasterxml.jackson.databind.ObjectMapper;
> import com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider;
> import com.fasterxml.jackson.module.jaxb.JaxbAnnotationModule;
> 
> public class CustomJsonProvider extends JacksonJaxbJsonProvider {
> 
> // this class exists so we can customize the object mapper
> // and therefore the way that the JSON is produced
>  public CustomJsonProvider() {
>     ObjectMapper mapper = new ObjectMapper();
>     mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
>     JaxbAnnotationModule jaxbModule = new JaxbAnnotationModule();
>     mapper.registerModule(jaxbModule);
> this._mapperConfig.setMapper(mapper);
>  }
> 
> 
> And then in the configuration file, it looks like this:
> 
> 
> <jaxrs:server id="restServer" address="/rest">
>       <jaxrs:serviceBeans>
>       ...
>       </jaxrs:serviceBeans>
>     <jaxrs:providers>
>  <bean id="jaxbProvider" class="mypackage.CustomJsonProvider"/>
>    </jaxrs:providers>
> </jaxrs:server>
> 
> 
> The jaxb annotation and providers needed to get built from github:
> 
> https://github.com/FasterXML/jackson-module-jaxb-annotations.git
> 
> https://github.com/FasterXML/jackson-jaxrs-providers.git

Ok.  I'll give this a try.  I don't know why you had to build these.  The docs provide Maven coordinates for these.  Perhaps that was added after you did this?

> On Wed, Nov 6, 2013 at 12:03 PM, Aaron Titus <at...@fwdco.com> wrote:
> 
> > I just went through this myself, and found the same struggle.  One of the
> > challenges is that with Jackson 2.0 is that the JAXB stuff is now in
> > separate github projects, and there weren't any jars available.  I had to
> > clone the github projects and build.  I don't use spring in my app directly
> > but I do use the spring beans in the CXF configuration file. It was
> > difficult to try and define the parameters I needed directly in there so I
> > opted to extend the jackson jaxb provider class and then refer to my
> > provider within the spring configuration file.  As Sergey mentioned, you
> > also need to use CXF 2.7.6 or greater otherwise you run into another
> > problem.
> >
> >
> >
> >
> > On Wed, Nov 6, 2013 at 11:47 AM, KARR, DAVID <dk...@att.com> wrote:
> >
> >> I'm trying to investigate integrating Jackson 2.x into a Spring CXF
> >> JAX-RS prototype, using JAXB annotations to facilitate both XML and JSON
> >> rendering.  I've browsed the Jackson documentation, but it seems
> scattered.
> >>  I have an older CXF JAX-RS app using Jackson 1.x, but I'd like to see
> what
> >> the newer Jackson does.  I've seen some examples on the web using the
> older
> >> Jackson, but not the new one.  Is there a straightforward guide to this
> >> somewhere?
> >>
> >
> >

RE: Concise summary of steps to integrate Jackson 2.x into CXF with Spring and JAXB annotations?

Posted by "KARR, DAVID" <dk...@att.com>.
> -----Original Message-----
> From: Aaron Titus [mailto:atitus@fwdco.com]
> Sent: Wednesday, November 06, 2013 9:11 AM
> To: users@cxf.apache.org
> Subject: Re: Concise summary of steps to integrate Jackson 2.x into CXF with
> Spring and JAXB annotations?
> 
> The custom provider can be as simple as this:
> 
> package mypackage;
> 
> import com.fasterxml.jackson.databind.DeserializationFeature;
> import com.fasterxml.jackson.databind.ObjectMapper;
> import com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider;
> import com.fasterxml.jackson.module.jaxb.JaxbAnnotationModule;
> 
> public class CustomJsonProvider extends JacksonJaxbJsonProvider {
> 
> // this class exists so we can customize the object mapper
> // and therefore the way that the JSON is produced
>  public CustomJsonProvider() {
>     ObjectMapper mapper = new ObjectMapper();
>     mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
>     JaxbAnnotationModule jaxbModule = new JaxbAnnotationModule();
>     mapper.registerModule(jaxbModule);
> this._mapperConfig.setMapper(mapper);
>  }
> 
> 
> And then in the configuration file, it looks like this:
> 
> 
> <jaxrs:server id="restServer" address="/rest">
>       <jaxrs:serviceBeans>
>       ...
>       </jaxrs:serviceBeans>
>     <jaxrs:providers>
>  <bean id="jaxbProvider" class="mypackage.CustomJsonProvider"/>
>    </jaxrs:providers>
> </jaxrs:server>

I thought I had this all working, but I realized the resulting JSON output isn't quite right.

In my sample data, I have this annotated class:
-----------------
@XmlType(name = "configInfo")
@XmlRootElement(name = "configInfo")
public class ConfigInfo {
    @XmlElement(name = "foo")
    private String  foo;
    @XmlElementWrapper(name = "devices")
    @XmlElement(name = "device")
    private List<Device>    devices;

    public final List<Device> getDevices() {
        if (devices == null)
            devices = new ArrayList<Device>();
        return devices;
    }
}
-----------------

I created an instance with just a single Device in the list, and this rendered as the following:

{"device":[{"name":"abc","type":"def"}]}

The "device" property should be "devices".  How do I make that happen?

> On Wed, Nov 6, 2013 at 12:03 PM, Aaron Titus <at...@fwdco.com> wrote:
> 
> > I just went through this myself, and found the same struggle.  One of the
> > challenges is that with Jackson 2.0 is that the JAXB stuff is now in
> > separate github projects, and there weren't any jars available.  I had to
> > clone the github projects and build.  I don't use spring in my app directly
> > but I do use the spring beans in the CXF configuration file. It was
> > difficult to try and define the parameters I needed directly in there so I
> > opted to extend the jackson jaxb provider class and then refer to my
> > provider within the spring configuration file.  As Sergey mentioned, you
> > also need to use CXF 2.7.6 or greater otherwise you run into another
> > problem.
> >
> >
> >
> >
> > On Wed, Nov 6, 2013 at 11:47 AM, KARR, DAVID <dk...@att.com> wrote:
> >
> >> I'm trying to investigate integrating Jackson 2.x into a Spring CXF
> >> JAX-RS prototype, using JAXB annotations to facilitate both XML and JSON
> >> rendering.  I've browsed the Jackson documentation, but it seems
> scattered.
> >>  I have an older CXF JAX-RS app using Jackson 1.x, but I'd like to see
> what
> >> the newer Jackson does.  I've seen some examples on the web using the
> older
> >> Jackson, but not the new one.  Is there a straightforward guide to this
> >> somewhere?
> >>
> >
> >

Re: Concise summary of steps to integrate Jackson 2.x into CXF with Spring and JAXB annotations?

Posted by Aaron Titus <at...@fwdco.com>.
The custom provider can be as simple as this:

package mypackage;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider;
import com.fasterxml.jackson.module.jaxb.JaxbAnnotationModule;

public class CustomJsonProvider extends JacksonJaxbJsonProvider {

// this class exists so we can customize the object mapper
// and therefore the way that the JSON is produced
 public CustomJsonProvider() {
    ObjectMapper mapper = new ObjectMapper();
    mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
    JaxbAnnotationModule jaxbModule = new JaxbAnnotationModule();
    mapper.registerModule(jaxbModule);
this._mapperConfig.setMapper(mapper);
 }


And then in the configuration file, it looks like this:


<jaxrs:server id="restServer" address="/rest">
      <jaxrs:serviceBeans>
      ...
      </jaxrs:serviceBeans>
    <jaxrs:providers>
 <bean id="jaxbProvider" class="mypackage.CustomJsonProvider"/>
   </jaxrs:providers>
</jaxrs:server>


The jaxb annotation and providers needed to get built from github:

https://github.com/FasterXML/jackson-module-jaxb-annotations.git

https://github.com/FasterXML/jackson-jaxrs-providers.git



On Wed, Nov 6, 2013 at 12:03 PM, Aaron Titus <at...@fwdco.com> wrote:

> I just went through this myself, and found the same struggle.  One of the
> challenges is that with Jackson 2.0 is that the JAXB stuff is now in
> separate github projects, and there weren't any jars available.  I had to
> clone the github projects and build.  I don't use spring in my app directly
> but I do use the spring beans in the CXF configuration file. It was
> difficult to try and define the parameters I needed directly in there so I
> opted to extend the jackson jaxb provider class and then refer to my
> provider within the spring configuration file.  As Sergey mentioned, you
> also need to use CXF 2.7.6 or greater otherwise you run into another
> problem.
>
>
>
>
> On Wed, Nov 6, 2013 at 11:47 AM, KARR, DAVID <dk...@att.com> wrote:
>
>> I'm trying to investigate integrating Jackson 2.x into a Spring CXF
>> JAX-RS prototype, using JAXB annotations to facilitate both XML and JSON
>> rendering.  I've browsed the Jackson documentation, but it seems scattered.
>>  I have an older CXF JAX-RS app using Jackson 1.x, but I'd like to see what
>> the newer Jackson does.  I've seen some examples on the web using the older
>> Jackson, but not the new one.  Is there a straightforward guide to this
>> somewhere?
>>
>
>

Re: Concise summary of steps to integrate Jackson 2.x into CXF with Spring and JAXB annotations?

Posted by Aaron Titus <at...@fwdco.com>.
I just went through this myself, and found the same struggle.  One of the
challenges is that with Jackson 2.0 is that the JAXB stuff is now in
separate github projects, and there weren't any jars available.  I had to
clone the github projects and build.  I don't use spring in my app directly
but I do use the spring beans in the CXF configuration file. It was
difficult to try and define the parameters I needed directly in there so I
opted to extend the jackson jaxb provider class and then refer to my
provider within the spring configuration file.  As Sergey mentioned, you
also need to use CXF 2.7.6 or greater otherwise you run into another
problem.


*Aaron Titus*
Senior Software Engineer
F.W. Davison & Company, Inc.
508-747-7261 x245
atitus@fwdco.com



On Wed, Nov 6, 2013 at 11:47 AM, KARR, DAVID <dk...@att.com> wrote:

> I'm trying to investigate integrating Jackson 2.x into a Spring CXF JAX-RS
> prototype, using JAXB annotations to facilitate both XML and JSON
> rendering.  I've browsed the Jackson documentation, but it seems scattered.
>  I have an older CXF JAX-RS app using Jackson 1.x, but I'd like to see what
> the newer Jackson does.  I've seen some examples on the web using the older
> Jackson, but not the new one.  Is there a straightforward guide to this
> somewhere?
>