JAXB to JSON using JACKSON
Solution 1
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).
Solution 2
You can use jackson-module-jaxb-annotations as stated in the doc you can register a JaxbAnnotationModule
module:
JaxbAnnotationModule module = new JaxbAnnotationModule();
// configure as necessary
objectMapper.registerModule(module);
Doing so you can now use both JAXB annotation and Jackson native annotation.
Solution 3
The correct solution for me was:
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setAnnotationIntrospector(new JaxbAnnotationIntrospector());
Solution 4
If you update Jackson to 2.0 it is not deprecated:
You can see my configuration here (Spring):
Registrer MappingJackson2HttpMessageConverter in Spring 3.1.2 with JAXB annotations
user1417746
Updated on June 15, 2022Comments
-
user1417746 almost 2 years
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?