Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jackson unable to consider @XmlElement while serializing to JSON

Tags:

xml

jaxb

jackson

I have a contract class that contains elements with @XmlElement tags. For ex

 @XmlElement(name = "balancemoney")
 protected Amount balanceMoney;

Using the JAXBContext I am able to generate the xml with proper tags.

However, when I use the jackson provided library, the JSON tag still comes in as 'balanceMoney' instead of 'balancemoney'

How do I tell Jackson to consider the @XmlElement tag.

Below is the code which does this.

    //Function to display request object.
public void displayXML(Object reqResp){
    try{
        JAXBContext jaxbContext = JAXBContext.newInstance(reqResp.getClass());
        Marshaller jaxbMarshaller = jaxbContext.createMarshaller();

        // output pretty printed
        jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

        ByteArrayOutputStream bStream=new ByteArrayOutputStream();

        //jaxbMarshaller.marshal(reqResp, System.out);
        jaxbMarshaller.marshal(reqResp,bStream );

        logger.info(bStream.toString());    

        }catch(JAXBException e){
            logger.info(e.getMessage());
        }
        logger.info("*** Payload is: " + reqResp.toString());
}

//Function to display as JSON
public void displayJSON(Object reqResp) throws JsonGenerationException, JsonMappingException, IOException{
    ObjectMapper mapper = new ObjectMapper();
    logger.info(mapper.defaultPrettyPrintingWriter().writeValueAsString(reqResp));

}
like image 605
Gsb128379 Avatar asked Oct 26 '13 00:10

Gsb128379


People also ask

Can Jackson use JAXB annotations?

Jackson by default does not recognize JaxB annotations. To recognize them Jackson requires jackson-module-jaxb-annotations module.

Does Jackson use JAXB?

With XML module Jackson provides support for JAXB (javax. xml. bind) annotations as an alternative to native Jackson annotations, so it is possible to reuse existing data beans that are created with JAXB to read and write XMLs.

What is the difference between Jackson and JAXB?

JAXB converts XML to java objects, and Jackson converts the same java objects to JSON.

What is Jackson Dataformat XML?

dataformat. xml. annotation. Package that contains additional annotations that can be used to configure XML-specific aspects of serialization and deserialization.


2 Answers

  • Maven dependency - pom.xml:

    <dependency>
        <groupId>com.fasterxml.jackson.module</groupId>
        <artifactId>jackson-module-jaxb-annotations</artifactId>
        <version>2.6.1</version>
    </dependency>
    
  • Custom ObjectMapper configuration (uncomment all annotations to register this mapper as default in Spring):

    //@Configuration
    public class JacksonConfig {
    
        //@Primary
        //@Bean
        public static ObjectMapper createCustomObjectMapper() {
            ObjectMapper mapper = new ObjectMapper();
            AnnotationIntrospector aiJaxb = new JaxbAnnotationIntrospector(TypeFactory.defaultInstance());
            AnnotationIntrospector aiJackson = new JacksonAnnotationIntrospector();
            // first Jaxb, second Jackson annotations
            mapper.setAnnotationIntrospector(AnnotationIntrospector.pair(aiJaxb, aiJackson));
            return mapper;
        }
    }
    
  • Code to display as JSON:

    public void displayJSON(Object reqResp) throws JsonProcessingException{
        ObjectMapper mapper = JacksonConfig.createCustomObjectMapper();
        LOG.info(mapper.writeValueAsString(reqResp));
    }
    
like image 64
kinjelom Avatar answered Oct 03 '22 01:10

kinjelom


According to Using JAXB annotations with Jackson: Enabling JAXB annotation support, you need to set mapper.getDeserializationConfig().setAnnotationIntrospector(new JaxbAnnotationIntrospector()); to enable Jackson to use JAXB annotation.

like image 43
user2880879 Avatar answered Oct 03 '22 01:10

user2880879