Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JAXB to JSON using JACKSON

In my application the JAXB output generates like:

this.marshalOut(jaxb_Object, fileOutputStream);

this is method call to the spring Object XML Mapping Marshallers that generate XML files. Now, I also like to generate JSON files after this line. Any one have idea about generating JSON output using JAXB input.

I found this example code online:

ObjectMapper mapper = new ObjectMapper();
AnnotationIntrospector introspector = new JacksonAnnotationIntrospector();
// make deserializer use JAXB annotations (only)
mapper.getDeserializationConfig().setAnnotationIntrospector(introspector);
// make serializer use JAXB annotations (only)
mapper.getSerializationConfig().setAnnotationIntrospector(introspector);
mapper.writeValue( outputStream, jaxb_object);

The setAnnotationIntrospector is deprecated, is there any other way of solving this problem?

like image 338
user1417746 Avatar asked Jul 11 '12 20:07

user1417746


1 Answers

The following works (and does not use any deprecated constructors) :

ObjectMapper mapper = new ObjectMapper();

AnnotationIntrospector introspector =
    new JaxbAnnotationIntrospector(mapper.getTypeFactory());   

mapper.setAnnotationIntrospector(introspector);

Specifically, this line

new JaxbAnnotationIntrospector(mapper.getTypeFactory());

uses a non-deprecated constructor. I've tested this and it successfully processes JAXB Annotations (such as @XmlTransient, in my case).

like image 152
Michael Christoff Avatar answered Sep 19 '22 17:09

Michael Christoff