Java “如何修复”;属性“”的getter定义冲突;FasterXml中有错误吗?
我想将下面的xml字符串反序列化为Java对象,但我得到了错误Java “如何修复”;属性“”的getter定义冲突;FasterXml中有错误吗?,java,jackson,fasterxml,Java,Jackson,Fasterxml,我想将下面的xml字符串反序列化为Java对象,但我得到了错误 java.lang.IllegalArgumentException:属性“服务”的getter定义冲突 这是用于反序列化的XML字符串: <result> <service>service_id</service> <date>2019-01-30 12:10:33</date> <status>0</status> <service>
java.lang.IllegalArgumentException:属性“服务”的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>
和我的转换器代码:
try {
CustomResult result = new XmlMapper().readValue(xmlString, CustomResult.class);
} catch (IOException e) {
e.printStackTrace();
}
我认为发生此错误是因为相同的名称是两个参数。我使用rest请求从服务器获取此xml,并且无法更改参数名称。如何修复此错误?首先,您的xml是有效的 因为我对杰克逊不太熟悉,所以我的第一次尝试就是带着他的名字读文件。这就像沙姆一样,没有任何麻烦
@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;
}
@测试
public void xml()抛出JAXBEException,IOException{
字符串xml=“\n”+
“服务\u id\n”+
“2019-01-30 12:10:33\n”+
“0\n”+
“\n”+
“123\n”+
“名称\n”+
“90\n”+
“\n”+
"";
try(ByteArrayInputStream=newbytearrayinputstream(xml.getBytes())){
CustomResult=unmarshal(baoust,CustomResult.class);
系统输出打印项次(结果);
}
}
公共T unmarshal(类clazz中的最终输入流)抛出JAXBEException{
最终解组器m=创建解组器(clazz);
返回m.unmarshal(newstreamsource(in),clazz).getValue();
}
私有解组器createUnmarshaller(类clazz)抛出JAXBEException、PropertyException{
final JAXBContext context=JAXBContext.newInstance(clazz);
if(!(org.eclipse.persistence.jaxb.JAXBContext的上下文实例)){
抛出新的MissingResourceException(“缺少MOXy实现”,
“org.eclipse.persistence.jaxb.JAXBContext”,”;
}
final Unmarshaller m=context.createUnmarshaller();
m、 setProperty(UnmarshallerProperties.MEDIA\u TYPE、MediaType.APPLICATION\u XML);
m、 setProperty(UnmarshallerProperties.BEAN\u VALIDATION\u MODE,BeanValidationMode.NONE);
返回m;
}
@XmlRootElement(name=“result”)
公共静态类CustomResult{
公共CustomResult(){}
@xmlement(name=“服务”)
专用字符串服务;
@xmlement(name=“date”)
私有字符串日期;
@xmlement(name=“status”)
私有整数状态;
@xmlement(name=“服务”)
私人服务状态OBJ;
}
@XmlType
公共静态类服务{
公共服务(){}
@xmlement(name=“id”)
私有整数id;
@xmlement(name=“name”)
私有字符串名称;
@xmlement(name=“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;
}
@测试
public void xml()抛出JAXBEException,IOException{
字符串xml=“\n”+
“服务\u id\n”+
“2019-01-30 12:10:33\n”+
“0\n”+
“\n”+
“123\n”+
“名称\n”+
“90\n”+
“\n”+
"";
CustomResult result2=unmarshal(xml,CustomResult.class);
系统输出打印项次(结果2);
}
公共T unmarshal(最终字符串输入,类clazz)引发IOException{
XmlMapper XmlMapper=新的XmlMapper();
registerModule(新的JaxbAnnotationModule());
返回xmlMapper.readValue(输入,clazz);
}
@XmlRootElement(name=“result”)
公共静态类CustomResult{
公共CustomResult(){}
@杰索尼奥雷
私有列表服务=新的ArrayList();
@xmlement(name=“date”)
私有字符串日期;
@xmlement(name=“status”)
私有整数状态;
@JSONANYSETER
公共void设置服务(字符串名称、对象值){
if(字符串的值实例){
服务增值;
}
if(映射的值实例){
//TODO从映射项创建新的服务对象。
}
//错误?
}
}
@XmlType
公共静态类服务{
公共服务(){}
@xmlement(name=“id”)
私有整数id;
@xmlement(name=“name”)
私有字符串名称;
@xmlement(name=“type”)
私有整数类型;
}
我希望这能进一步帮助您。s在XML中不能有两个名称相等的标记。重命名其中一个,例如:
service\u id
I没有访问服务器的权限。服务器开发人员不想修复xmlI的第二个响应方法从这个链接得到了帮助:在unmarshal(最终字符串输入,类clazz)
行return xmlMapper.readValue(输入,clazz)中代码>抛出异常java.lang.IllegalArgumentException:属性“服务”…@avazbek的getter定义冲突您使用的是什么版本的jackson?我用了2.9.7。我刚刚从上面复制了代码,测试运行也没有例外。我使用了jackson 2.9.6I版本,这一链接的第二种响应方法帮助了我:
@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;
}