Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

xml - Jackson unable to consider @XmlElement while serializing to JSON

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

}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
  • 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));
    }
    

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...