Jackson 在序列化为 JSON 时无法考虑 @XmlElement [英] Jackson unable to consider @XmlElement while serializing to JSON

查看:28
本文介绍了Jackson 在序列化为 JSON 时无法考虑 @XmlElement的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个合同类,其中包含带有 @XmlElement 标签的元素.对于前

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

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

使用 JAXBContext 我能够生成带有适当标签的 xml.

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

但是,当我使用 jackson 提供的库时,JSON 标签仍然是balanceMoney"而不是balancemoney"

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

我如何告诉 Jackson 考虑 @XmlElement 标签.

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

}

推荐答案

  • Maven 依赖项 - pom.xml:

    • Maven dependency - pom.xml:

      <dependency>
          <groupId>com.fasterxml.jackson.module</groupId>
          <artifactId>jackson-module-jaxb-annotations</artifactId>
          <version>2.6.1</version>
      </dependency>
      

    • 自定义 ObjectMapper 配置(取消注释所有注解以在 Spring 中将此映射器注册为默认值):

    • 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;
          }
      }
      

    • 显示为 JSON 的代码:

    • Code to display as JSON:

      public void displayJSON(Object reqResp) throws JsonProcessingException{
          ObjectMapper mapper = JacksonConfig.createCustomObjectMapper();
          LOG.info(mapper.writeValueAsString(reqResp));
      }
      

    • 这篇关于Jackson 在序列化为 JSON 时无法考虑 @XmlElement的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆