[英]JAXB Unmarshaller returning null in object after parsing from XML
I have a String object with xml data in it . 我有一个带有xml数据的String对象。 I want that data in POJO , i tried using JAXB unmarshaller to convert but it always give me null values in the object attributes. 我想要POJO中的数据,我尝试使用JAXB unmarshaller进行转换,但它始终为我提供对象属性中的空值。
this is my code : 这是我的代码:
ResponseEntity<String> response = restTemplate.getForEntity("https://api.flickr.com/services/rest/?api_key=MY_API_KEY&method=flickr.photos.search&tags=nature", String.class);
String resp = response.getBody();
JAXBContext jaxBcontext = JAXBContext.newInstance(Resp.class);
Unmarshaller unmarshaller = jaxBcontext.createUnmarshaller();
Resp respObj = (Resp)unmarshaller.unmarshal(new StringReader(resp));
the value in String is : 字符串中的值是:
<rsp stat="ok">
<photos page="1" pages="4226" perpage="100" total="422597">
<photo id="28534349567" owner="79805131@N08" secret="b8bd7fe7cb"
server="843" farm="1" title="Savoie S006." ispublic="1" isfriend="0"
isfamily="0"/>
<photo id="43355895332" owner="155237230@N05" secret="75fd48d040"
server="1769" farm="2" title="IMG_3139" ispublic="1" isfriend="0"
isfamily="0"/>
<photo id="41595746070" owner="125407841@N08" secret="1f216ab8b8"
server="1822" farm="2" title="" ispublic="1" isfriend="0" isfamily="0"/>
</photos>
</rsp>
the POJOS are : POJOS是:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "rsp")
public class Resp {
@XmlElement(name="stat")
private String stat;
@XmlElement(name="photos" , type = Photos.class)
private Photos photos;
public String getStat() {
return stat;
}
//constructors and getter setters
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "photos")
public class Photos {
@XmlElement(name="total")
private String total;
@XmlElement(name="page")
private String page;
@XmlElement(name="pages")
private String pages;
@XmlElement(name="perpage")
private String perpage;
@XmlElement(name="photo" , type=Photo.class)
private List<Photo> photoObject = new ArrayList<Photo>();
// constructors and getter setters.
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "photo")
public class Photo {
@XmlElement(name="id")
private String id;
@XmlElement(name="isfamily")
private String isfamily;
@XmlElement(name="title")
private String title;
@XmlElement(name="ispublic")
private String ispublic;
@XmlElement(name="owner")
private String owner;
@XmlElement(name="secret")
private String secret;
@XmlElement(name="server")
private String server;
@XmlElement(name="isfriend")
private String isfriend;
// constructors and setter getter
the response i get is null values in all these objects. 我得到的响应是所有这些对象中的空值。
Resp [stat=null, photos=Photos [total=null, page=null, pages=null, perpage=null, photo=]] 分别[stat = null,photos =照片[total = null,page = null,pages = null,perpage = null,photo =]]
the value in String which i get is absolutely correct , but when i try to map my data to a POJO it starts giving error. 我得到的String中的值是绝对正确的,但是当我尝试将数据映射到POJO时,它开始产生错误。
Other approach I used it to get the data directly in the object , like I have mentioned in my other question , but it also have some issues in that. 就像我在其他问题中提到的那样,我使用它的另一种方法直接在对象中获取数据,但是它也存在一些问题。
RestTemplate returns data in String but not populate list nested objects RestTemplate以String形式返回数据,但不填充列表嵌套对象
if anyone can help in either one of these that'd be helpful. 如果有人可以提供帮助,那将是有帮助的。
As @f1sh already already commented, your problems are caused by several issues within your POJO classes 正如@ f1sh已经提到的那样,您的问题是由POJO类中的几个问题引起的
<rsp stat="ok">...</rsp>
(ie stat
is an XML attribute) instead of <rsp><stat>ok</stat>...</rsp>
( stat
being an XML element). 在您的XML响应中,您具有<rsp stat="ok">...</rsp>
(即stat
是XML属性),而不是<rsp><stat>ok</stat>...</rsp>
( stat
是XML元素)。 Resp
class the stat
property needs to annotated with @XmlAttribute
instead of @XmlElement
. 因此,在您的Resp
类中,需要使用@XmlAttribute
而不是@XmlElement
来注释stat
属性。 Photos
class, all properties except for the photoObject
property need to be annotated with @XmlAttribute
instead of @XmlElement
. 出于相同的原因,在您的Photos
类中,除photoObject
属性外的所有属性都需要使用@XmlAttribute
而不是@XmlElement
进行注释。 Photo
class all properties need to be annotated with @XmlAttribute
instead of @XmlElement
. 出于同样的原因,在您的Photo
类中,所有属性都需要使用@XmlAttribute
而不是@XmlElement
进行注释。 Some more best practices to consider: 需要考虑的更多最佳做法:
Photos
and Photo
classes you can remove the @XmlRootElement
annotation because theses are not root elements. 在Photos
和Photo
类中,可以删除@XmlRootElement
批注,因为这些不是根元素。 Only the Resp
class is a root element and therefore needs @XmlRootElement
. 只有Resp
类是根元素,因此需要@XmlRootElement
。 Photos
class you should rename the photoObject
property to photoObjects
(with a plural s ) because it is a list. 在Photos
类中,应将photoObject
属性重命名为photoObjects
(带有s ),因为它是一个列表。 This would make the code easier to understand. 这将使代码更易于理解。 I found the solution . 我找到了解决方案。
The annotations @XMLAttribute and @XmlElements needed to be put on setters . 注释@XMLAttribute和@XmlElements需要放在setter上。
Did not work for getters and declarations. 不适用于获取器和声明。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.