无法解析 xml 根元素使用 jackson
Can not parse xml root element use jackson
测试 Bean
@JacksonXmlRootElement(localName = "DATA_RECORD")
public class TestBean{
@JacksonXmlProperty(localName="ERROR_MESSAGE_CODE")
private String error_message_code;
@JacksonXmlProperty(localName="ERROR_MESSAGE")
private String error_message;
//...getter/setter
}
XMl样本
String xml = "<?xml version=\"1.0\" encoding=\"Windows-31J\" standalone=\"no\"?>"
+ "<Message xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" "
//+">"
+ "xsi:noNamespaceSchemaLocation=\"TEST.xsd\">" // if comment out this,it will work.
+ "<DATA_RECORD>"
+ "<ERROR_MESSAGE>some message</ERROR_MESSAGE>"
+ "<ERROR_MESSAGE_CODE>CODE111</ERROR_MESSAGE_CODE>"
+ "</DATA_RECORD>"
+ "</Message>";
反序列化
XmlMapper xmlMapper = new XmlMapper();
//xmlMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
xmlMapper.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true);
TestBean test = xmlMapper.readValue(xml, TestBean.class);
log.debug(test.toString());
我从 Junit 运行 它得到异常,如:
Root name 'noNamespaceSchemaLocation' does not match expected
('DATA_RECORD') ....
如果我从 String xml
中删除 xsi:noNamespaceSchemaLocation="TEST.xsd"
,它将正常工作。
有这个想法吗?感谢您的帮助。
根据 docs,当您指定 UNWRAP_ROOT_VALUE
时,Jackson(此处 XML 而不是 JSON)
Will verify that the root JSON value is a JSON Object, and that it has
a single property with expected root name. If not, a
JsonMappingException is thrown;
在这种情况下,根 Message
除了 DATA_RECORD
之外还有另一个 属性,名称为 noNamespaceSchemaLocation
的 XML 属性和指定的 JsonMappingException
被抛出。
恐怕您将不得不解析 Message
并从那里得到 TestBean
。例如:
@JacksonXmlRootElement
class Message {
@JacksonXmlProperty(localName = "DATA_RECORD")
private TestBean dataRecord;
}
class TestBean {
@JacksonXmlProperty(localName = "ERROR_MESSAGE_CODE")
private String error_message_code;
@JacksonXmlProperty(localName = "ERROR_MESSAGE")
private String error_message;
}
和
xmlMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
Message test = xmlMapper.readValue(xml, Message.class);
log.debug(test.getDataRecord().toString());
测试 Bean
@JacksonXmlRootElement(localName = "DATA_RECORD")
public class TestBean{
@JacksonXmlProperty(localName="ERROR_MESSAGE_CODE")
private String error_message_code;
@JacksonXmlProperty(localName="ERROR_MESSAGE")
private String error_message;
//...getter/setter
}
XMl样本
String xml = "<?xml version=\"1.0\" encoding=\"Windows-31J\" standalone=\"no\"?>"
+ "<Message xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" "
//+">"
+ "xsi:noNamespaceSchemaLocation=\"TEST.xsd\">" // if comment out this,it will work.
+ "<DATA_RECORD>"
+ "<ERROR_MESSAGE>some message</ERROR_MESSAGE>"
+ "<ERROR_MESSAGE_CODE>CODE111</ERROR_MESSAGE_CODE>"
+ "</DATA_RECORD>"
+ "</Message>";
反序列化
XmlMapper xmlMapper = new XmlMapper();
//xmlMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
xmlMapper.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true);
TestBean test = xmlMapper.readValue(xml, TestBean.class);
log.debug(test.toString());
我从 Junit 运行 它得到异常,如:
Root name 'noNamespaceSchemaLocation' does not match expected ('DATA_RECORD') ....
如果我从 String xml
中删除 xsi:noNamespaceSchemaLocation="TEST.xsd"
,它将正常工作。
有这个想法吗?感谢您的帮助。
根据 docs,当您指定 UNWRAP_ROOT_VALUE
时,Jackson(此处 XML 而不是 JSON)
Will verify that the root JSON value is a JSON Object, and that it has a single property with expected root name. If not, a JsonMappingException is thrown;
在这种情况下,根 Message
除了 DATA_RECORD
之外还有另一个 属性,名称为 noNamespaceSchemaLocation
的 XML 属性和指定的 JsonMappingException
被抛出。
恐怕您将不得不解析 Message
并从那里得到 TestBean
。例如:
@JacksonXmlRootElement
class Message {
@JacksonXmlProperty(localName = "DATA_RECORD")
private TestBean dataRecord;
}
class TestBean {
@JacksonXmlProperty(localName = "ERROR_MESSAGE_CODE")
private String error_message_code;
@JacksonXmlProperty(localName = "ERROR_MESSAGE")
private String error_message;
}
和
xmlMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
Message test = xmlMapper.readValue(xml, Message.class);
log.debug(test.getDataRecord().toString());