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

查看:141
本文介绍了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'

如何做我告诉杰克逊考虑@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));

}


推荐答案

根据与Jackson一起使用JAXB注释启用JAXB注释支持,您需要设置 mapper.getDeserializationConfig()。setAnnotationIntrospector(new JaxbAnnotationIntrospector()); 使Jackson能够使用JAXB注释。

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.

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

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