Jackson jaxb引发com.fasterxml.jackson.databind.JsonMappingException:属性的getter定义冲突

时间:2020-05-07 12:16:29

标签: java jackson jaxb jersey jax-rs

将球衣(com.sun.jersey)1.15迁移到球衣(org.glassfish.jersey)2.30,然后将Jackson(jersey-media-json-jackson)用于JAXB

我们有POJO之类的

@XmlRootElement(name = "pojo")
class Pojo {

    @XmlElementWrapper(name = "activeCustomers")
    @XmlElement(name = "customer")
    public List<Customer> getActiveCust() { 
        return activeCustomers; 
    }

    @XmlElementWrapper(name = "deactivatedCustomers")
    @XmlElement(name = "customer")
    public List<Customer> getDeactivatedCustomers() { 
        return deactivatedCustomers; 
    }

}

期望的XML:

<pojo>
  <activeCustomers>
    <customer>A</customer>
    <customer>B</customer>
    <customer>C</customer>
  </activeCustomers>
  <deactivatedCustomers >
    <customer>X</customer>
    <customer>Y</customer>
    <customer>Z</customer>
  </deactivatedCustomers >
</pojo>

期望的JSON

pojo : {
   activeCustomers : [
     customer: [ {}, {} ]
   ],
   deactivatedCustomers : [
     customer: [ {}, {} ]
   ]
}

然后将此实体作为对GET api的响应返回,但是在响应处理低于异常的情况下

com.fasterxml.jackson.databind.JsonMappingException:冲突 属性“客户”的吸气剂定义:

在jersey1.x中没有遇到此问题,但在jersey 2.x中遇到了问题,并且向应用程序注册的功能是:

register(JacksonFeature.class);
register(MultiPartFeature.class);

请帮助我,如何在不更改xml元素名称且可能不添加新注释的情况下解决此问题(可能会有成百上千种情况)。如果需要注册更多功能,是否需要创建新的自定义映射器?

Jersey version : 2.30
com.fasterxml.jackson version: 2.9.10

仅当Accept为application / json时才会出现问题,如果Accept为application / xml则工作正常

0 个答案:

没有答案
相关问题