如何修复FasterXml Jackson中的“属性冲突的getter定义”错误?

时间:2019-01-30 07:33:31

标签: java jackson fasterxml

我想将下面的xml字符串反序列化为Java对象,但是出现错误     java.lang.IllegalArgumentException: Conflicting getter definitions for property "service"

这是用于反序列化的XML字符串:

<result>
<service>service_id</service>
<date>2019-01-30 12:10:33</date>
<status>0</status>
<service>
  <id>123</id>
  <name>name</name>
  <type>90</type>
</service>
</result>

这是POJO对象:

@Data
@JacksonXmlRootElement(localName = "result")
public class CustomResult {

   @JacksonXmlProperty(localName = "service")
   private String service;

   @JacksonXmlProperty(localName = "date")
   private String date;

   @JacksonXmlProperty(localName = "status")
   private Integer status;

   @JacksonXmlProperty(localName = "service")
   private Service statusObj;

}

@Data
public class Service {

   @JacksonXmlProperty(localName = "id")
   private Integer id;

   @JacksonXmlProperty(localName = "name")
   private String name;

   @JacksonXmlProperty(localName = "type")
   private Integer type;
}

和我的转换器代码:

try {
       CustomResult result = new XmlMapper().readValue(xmlString, CustomResult.class);
    } catch (IOException e) {
       e.printStackTrace();
    }

我认为会发生此错误,因为相同的名称是两个参数。我使用其余请求从服务器获取此xml,并且无法更改参数名称。我该如何解决此错误?

1 个答案:

答案 0 :(得分:1)

首先,您的xml有效。

因为我对杰克逊并不那么熟悉,所以我的第一次尝试是使用MOXy读取文件。就像scharm一样,没有任何麻烦。

@Test
public void xml() throws JAXBException, IOException {
    String xml = "<result>\n" +
            "<service>service_id</service>\n" +
            "<date>2019-01-30 12:10:33</date>\n" +
            "<status>0</status>\n" +
            "<service>\n" +
            "  <id>123</id>\n" +
            "  <name>name</name>\n" +
            "  <type>90</type>\n" +
            "</service>\n" +
            "</result>";

    try (ByteArrayInputStream baoust = new ByteArrayInputStream(xml.getBytes())) {
        CustomResult result = unmarshal(baoust, CustomResult.class);
        System.out.println(result);
    }
}

public <T> T unmarshal(final InputStream in, Class<T> clazz) throws JAXBException {
    final Unmarshaller m = createUnmarshaller(clazz);
    return m.unmarshal(new StreamSource(in), clazz).getValue();
}

private <T> Unmarshaller createUnmarshaller(Class<T> clazz) throws JAXBException, PropertyException {
    final JAXBContext context = JAXBContext.newInstance(clazz);
    if (! (context instanceof org.eclipse.persistence.jaxb.JAXBContext)) {
        throw new MissingResourceException("Missing MOXy implementation.",
                "org.eclipse.persistence.jaxb.JAXBContext", "");
    }
    final Unmarshaller m = context.createUnmarshaller();
    m.setProperty(UnmarshallerProperties.MEDIA_TYPE, MediaType.APPLICATION_XML);
    m.setProperty(UnmarshallerProperties.BEAN_VALIDATION_MODE, BeanValidationMode.NONE);
    return m;
}

@XmlRootElement(name = "result")
public static class CustomResult {

    public CustomResult() {}

   @XmlElement(name = "service")
   private String service;

   @XmlElement(name = "date")
   private String date;

   @XmlElement(name = "status")
   private Integer status;

   @XmlElement(name = "service")
   private Service statusObj;

}

@XmlType
public static class Service {

    public Service() {}

    @XmlElement(name = "id")
   private Integer id;

    @XmlElement(name = "name")
   private String name;

    @XmlElement(name = "type")
   private Integer type;
}

在工作之后,我确信杰克逊也有可能。

@Test
public void xml() throws JAXBException, IOException {
    String xml = "<result>\n" +
            "<service>service_id</service>\n" +
            "<date>2019-01-30 12:10:33</date>\n" +
            "<status>0</status>\n" +
            "<service>\n" +
            "  <id>123</id>\n" +
            "  <name>name</name>\n" +
            "  <type>90</type>\n" +
            "</service>\n" +
            "</result>";

    CustomResult result2 = unmarshal(xml, CustomResult.class);
    System.out.println(result2);
}

public <T> T unmarshal(final String input, Class<T> clazz) throws IOException {
    XmlMapper xmlMapper = new XmlMapper();
    xmlMapper.registerModule(new JaxbAnnotationModule());
    return xmlMapper.readValue(input, clazz);
}

@XmlRootElement(name = "result")
public static class CustomResult {

   public CustomResult() {}

   @JsonIgnore
   private List<Object> service = new ArrayList<>();

   @XmlElement(name = "date")
   private String date;

   @XmlElement(name = "status")
   private Integer status;

   @JsonAnySetter
   public void setServices(String name, Object value) {
       if (value instanceof String) {
           service.add(value);
       }
       if (value instanceof Map) {
           // TODO create new Service object from map entries.
       }
       // error?
   }
}

@XmlType
public static class Service {

    public Service() {}

    @XmlElement(name = "id")
   private Integer id;

    @XmlElement(name = "name")
   private String name;

    @XmlElement(name = "type")
   private Integer type;
}

希望这对您有所帮助。