Java 从XML解析后,JAXB解组器在对象中返回null
我有一个字符串对象,其中包含xml数据。 我希望在POJO中使用该数据,我尝试使用JAXB解组器进行转换,但它总是在对象属性中为我提供空值 这是我的代码:Java 从XML解析后,JAXB解组器在对象中返回null,java,xml-parsing,jaxb,unmarshalling,pojo,Java,Xml Parsing,Jaxb,Unmarshalling,Pojo,我有一个字符串对象,其中包含xml数据。 我希望在POJO中使用该数据,我尝试使用JAXB解组器进行转换,但它总是在对象属性中为我提供空值 这是我的代码: ResponseEntity<String> response = restTemplate.getForEntity("https://api.flickr.com/services/rest/?api_key=MY_API_KEY&method=flickr.photos.search&tags=nature"
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));
我得到的响应是所有这些对象中的空值 Resp[stat=null,photos=photos[total=null,page=null,pages=null, perpage=null,photo=]] 我得到的字符串中的值是绝对正确的,但当我尝试将数据映射到POJO时,它开始给出错误 我使用它直接在对象中获取数据的另一种方法,就像我在我的另一个问题中提到的,但它在这方面也有一些问题
如果有人能在其中任何一个方面提供帮助,那都会很有帮助。正如@f1sh已经评论过的那样, 您的问题是由POJO类中的几个问题引起的
- 在XML响应中,您有
(即…
是一个XML属性) 而不是stat
(ok…
是XML元素)。stat
因此,在
类中,Resp
属性需要用stat
而不是@xmldattribute
@xmlement
- 出于同样的原因,在
照片
类中,所有属性 除了
属性需要用photoObject外
而不是@xmldattribute
@xmlement
- 出于同样的原因,在
类中,所有属性 需要使用Photo
而不是@xmldattribute
进行注释@xmlement
- 在
和Photos
类中,可以删除Photo
注释 因为这些不是根元素。 只有@XmlRootElement
类是根元素,因此需要Resp
@XmlRootElement
- 在
类中,您应该将Photos
属性重命名为photoObject
(用复数s)因为它是一个列表。这将使代码更容易理解photoObjects
- 我找到了解决办法
注释@xmltattribute和@XmlElements需要放在setter上
不适用于getter和声明。
stat
不是xmlement
,而是xmldattribute
(对于total
,page
等也一样)<代码>照片不是XmlRootElement
。
<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>
@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