我想将下面的 xml 字符串反序列化为 Java 对象,但出现错误
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();
}
我认为出现这个错误是因为两个参数同名。我使用其余请求从服务器获取此 xml,并且无法更改参数名称。我该如何修复这个错误?
首先,您的 xml 是有效的。
因为我对 jackson 不是很熟悉,所以我的第一次尝试是用 MOXy 读取文件.这很有效,没有任何麻烦。
@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;
}
在此工作之后,我相信 Jackson 也有可能做到这一点。
@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;
}
我希望这对你有进一步的帮助。