我想将下面的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?
首先,您的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;
}
我希望这对你有进一步的帮助。
我只是得到了重定向代码调用的错误,但我应该用我的代码得到重定向: 还有我得到的错误: 核心.js:5847 错误错误:未捕获(在promise中):类型错误:无法读取未定义的类型的属性“拆分”错误:无法读取默认未定义的属性“拆分”在应用重定向.push.的匹配(路由器.js:2718)处的UrlMatcher(路由器.js:530)。/node_modules/@angular/路由器/fesm5
我得到未定义的索引错误。我在同一个网站上看到了一个类似类型的问题。我无法修复这个错误,有人能帮助我吗
代码:import functools import json import os import tensorflow as tf import sys。路径附加(“C:\Users\Gilbertchristian\Documents\Anaconda\Object\u detection\u api\models\research”)系统。路径附加(“C:\Users\Gilbertchris
这是我的功能。显示解析错误(data.ops[0])
我正在尝试将我的react表单上载到状态,以便获取具有这些凭据的API 我尝试创建一个如下所示的函数:
我面临着这个奇怪的错误,我在jsp中使用了标记,而我使用的属性名抛出了一个错误。 以下是我在