[英]Jackson unable to consider @XmlElement while serializing to JSON
I have a contract class that contains elements with @XmlElement tags. 我有一个合同类,其中包含带有@XmlElement标记的元素。 For ex
对于前者
@XmlElement(name = "balancemoney")
protected Amount balanceMoney;
Using the JAXBContext I am able to generate the xml with proper tags. 使用JAXBContext,我能够使用适当的标签生成xml。
However, when I use the jackson provided library, the JSON tag still comes in as 'balanceMoney' instead of 'balancemoney' 但是,当我使用jackson提供的库时,JSON标记仍然以'balanceMoney'而不是'balancemoney'形式出现
How do I tell Jackson to consider the @XmlElement tag. 如何告诉Jackson考虑@XmlElement标记。
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 dependency - pom.xml: Maven依赖--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): 自定义ObjectMapper配置(取消注释所有注释以在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: 要显示为JSON的代码:
public void displayJSON(Object reqResp) throws JsonProcessingException{ ObjectMapper mapper = JacksonConfig.createCustomObjectMapper(); LOG.info(mapper.writeValueAsString(reqResp)); }
According to Using JAXB annotations with Jackson : Enabling JAXB annotation support , you need to set mapper.getDeserializationConfig().setAnnotationIntrospector(new JaxbAnnotationIntrospector());
根据Jackson使用JAXB注释 : 启用JAXB注释支持 ,您需要设置
mapper.getDeserializationConfig().setAnnotationIntrospector(new JaxbAnnotationIntrospector());
to enable Jackson to use JAXB annotation. 使Jackson能够使用JAXB注释。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.