[英]can not bind xml to pojo using jackson, com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field
While I try to bind xml response to POJO using jackson, I exepct to get values using getters. 当我尝试使用jackson将xml响应绑定到POJO时,我会使用getter获取值。 But I get the error an error. 但是我得到一个错误。 I tried searching about this but couldn't find relevant info. 我尝试搜索有关此内容,但找不到相关信息。 Most of folks suggested to use ignore. 大多数人建议使用忽略。 However I can't beause I am expecting that field. 但是我不能因为我期望这个领域。 My pojos look like below. 我的pojos如下所示。
public class Consumer {
private String xmlns;
private String version;
private Info info;
private Status status;
public Status getStatus() {
return status;
}
public void setStatus(Status status) {
this.status = status;
}
public Info getInfo() {
return info;
}
public void setInfo(Info info) {
this.info = info;
}
public String getXmlns() {
return xmlns;
}
public void setXmlns(String xmlns) {
this.xmlns = xmlns;
}
public String getVersion() {
return version;
}
public class Info {
private String state;
private String id;
private String shopId;
private String time;
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getShopId() {
return shopId;
}
public void setShopId(String shopId) {
this.shopId = shopId;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public class Status {
private Response response;
public Response getResponse() {
return response;
}
public void setResponse(Response response) {
this.response = response;
}
public class Response {
private String value;
private int statusCode;
private String messageCode;
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public int getStatusCode() {
return statusCode;
}
public void setStatusCode(int statusCode) {
this.statusCode = statusCode;
}
public String getMessageCode() {
return messageCode;
}
public void setMessageCode(String messageCode) {
this.messageCode = messageCode;
}
public class Temp {
public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException, JAXBException {
readXml();
}
public static void readXml() throws IOException, SAXException, ParserConfigurationException, JAXBException {
String xmlString = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
"<Consumer xmlns=\"http://www.someurl.com\" version=\"1.5\">\n" +
" <Info state=\"CA\">\n" +
" <Id>160c464da1ad89a2fec50436</Id>\n" +
" <ShopId>1234</ShopId>\n" +
" <time>2019-08-22T23:13:47.909-04:00</time>\n" +
" </Info>\n" +
" <Status>\n" +
" <Response value=\"Success\" statusCode=\"3456\" messageCode=\"Request processed successfully\" />\n" +
" </Status>\n" +
"</Consumer>";
XmlMapper xmlMapper = new XmlMapper();
Consumer consumer = xmlMapper.readValue(xmlString, Consumer.class);
System.out.println("Shop id "+consumer.getInfo().getShopId());
}
Exception in thread "main" com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "Info" (class com.motivatedmind.api.stackoverflow.Consumer), not marked as ignorable (3 known properties: "version", "xmlns", "info"])
at [Source: (StringReader); line: `enter code here`3, column: 21] (through reference chain: com.motivatedmind.api.stackoverflow.Consumer["Info"])
at com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException.from(UnrecognizedPropertyException.java:61)
at com.fasterxml.jackson.databind.DeserializationContext.handleUnknownProperty(DeserializationContext.java:823)
at com.fasterxml.jackson.databind.deser.std.StdDeserializer.handleUnknownProperty(StdDeserializer.java:1153)
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownProperty(BeanDeserializerBase.java:1589)
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownVanilla(BeanDeserializerBase.java:1567)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:294)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:151)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4014)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3005)
I suspect the problem is in XML string itself. 我怀疑问题出在XML字符串本身。
Change the <Consumersumer>
to <Consumer>
, that should work. 将<Consumersumer>
更改为<Consumer>
,这应该起作用。
----------------------- AFTER UPDATE--------- -----------------------更新后---------
Why the state
is passes as attribute to XML tag 'Info'..? 为什么将state
作为属性传递给XML标签'Info'..? It should match the children of <Info>
as instance values of your POJO class Info.java
. 它应与<Info>
的子项匹配,作为POJO类Info.java
实例值。
State is instance variable there in POJO, but is not child tag in <info>
tag. 状态是POJO中的实例变量,但不是<info>
标记中的子标记。
String xmlString = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
"<Consumer xmlns=\"http://www.someurl.com\" version=\"1.5\">\n" +
" <Info>\n" +
" <State>CA</State>\n" +
" <Id>160c464da1ad89a2fec50436</Id>\n" +
" <ShopId>1234</ShopId>\n" +
" <time>2019-08-22T23:13:47.909-04:00</time>\n" +
" </Info>\n" +
" <Status>\n" +
" <Response value=\"Success\" statusCode=\"3456\" messageCode=\"Request processed successfully\" />\n" +
" </Status>\n" +
"</Consumer>";
Try this..!! 尝试这个..!!
----------------- AFTER UPDATE in Comments--------- -----------------评论后更新---------
As you are getting the data (xml string) from some other service.. You can change your model object to match with xml data. 从其他服务获取数据(xml字符串)时。您可以更改模型对象以与xml数据匹配。
For this, you can use annotation @XmlAttribute
on your setter method of state
field in pojo. 为此,可以在pojo中的state
字段的setter方法上使用注解@XmlAttribute
。
For more reference, you can see this answer 有关更多参考,您可以看到此答案
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.