当前位置: 首页 > 知识库问答 >
问题:

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

柯鸿振
2023-03-14

我想将下面的xml字符串反序列化为Java对象,但我得到错误< code > Java . lang . illegalargumentexception:属性“service”的getter定义冲突

这是用于反序列化的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();
    }

我认为出现这个错误是因为两个参数同名。我使用rest请求从服务器获得这个xml,参数名不能更改。如何修复这个bug?

共有1个答案

金瑞
2023-03-14

首先,您的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;
}

我希望这对你有进一步的帮助。

 类似资料: