6

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?

posdef
  • 6,498
  • 11
  • 46
  • 94
user1417746
  • 191
  • 2
  • 3
  • 11
  • You may be interested in the object-to-JSON support we've added to EclipseLink JAXB (MOXy): http://blog.bdoughan.com/2011/08/json-binding-with-eclipselink-moxy.html – bdoughan Jul 11 '12 at 21:19
  • I need anything using Jackson, coz we are using jackson in our project. – user1417746 Jul 11 '12 at 21:28
  • Have you looked at Spring's MappingJacksonHttpMessageConverter? If you're not using HttpMessageConverters the source might at least give you some hints. – MattR Jul 12 '12 at 04:43

5 Answers5

8

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).

4

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.

landal79
  • 1,520
  • 1
  • 11
  • 26
2

If you update Jackson to 2.0 it is not deprecated:

http://fasterxml.github.com/jackson-databind/javadoc/2.0.0/com/fasterxml/jackson/databind/ObjectMapper.html

You can see my configuration here (Spring):

Registrer MappingJackson2HttpMessageConverter in Spring 3.1.2 with JAXB annotations

Community
  • 1
  • 1
molholm
  • 1,992
  • 3
  • 25
  • 29
2

The correct solution for me was:

ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setAnnotationIntrospector(new JaxbAnnotationIntrospector());
McIntosh
  • 2,051
  • 1
  • 22
  • 34
0

According to the Jackson javadoc:

setAnnotationIntrospector

@Deprecated
public final void setAnnotationIntrospector(AnnotationIntrospector ai)

    Deprecated. Since 1.8, use either withAnnotationIntrospector(AnnotationIntrospector) or Module API instead

    Method for replacing existing annotation introspector(s) with specified introspector. Since this method modifies state of configuration object directly, its use is not recommended

Did you check the method withAnnotationIntrospector(AnnotationIntrospector ai) to see wheter or not it's useful in your case?

posdef
  • 6,498
  • 11
  • 46
  • 94