簡體   English   中英

從XML解析后,JAXB Unmarshaller在對象中返回null

[英]JAXB Unmarshaller returning null in object after parsing from XML

我有一個帶有xml數據的String對象。 我想要POJO中的數據,我嘗試使用JAXB unmarshaller進行轉換,但它始終為我提供對象屬性中的空值。

這是我的代碼:

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));

字符串中的值是:

 <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>

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

我得到的響應是所有這些對象中的空值。

分別[stat = null,photos =照片[total = null,page = null,pages = null,perpage = null,photo =]]

我得到的String中的值是絕對正確的,但是當我嘗試將數據映射到POJO時,它開始產生錯誤。

就像我在其他問題中提到的那樣,我使用它的另一種方法直接在對象中獲取數據,但是它也存在一些問題。

RestTemplate以String形式返回數據,但不填充列表嵌套對象

如果有人可以提供幫助,那將是有幫助的。

正如@ f1sh已經提到的那樣,您的問題是由POJO類中的幾個問題引起的

  • 在您的XML響應中,您具有<rsp stat="ok">...</rsp> (即stat是XML屬性),而不是<rsp><stat>ok</stat>...</rsp>stat是XML元素)。
    因此,在您的Resp類中,需要使用@XmlAttribute而不是@XmlElement來注釋stat屬性。
  • 出於相同的原因,在您的Photos類中,除photoObject屬性外的所有屬性都需要使用@XmlAttribute而不是@XmlElement進行注釋。
  • 出於同樣的原因,在您的Photo類中,所有屬性都需要使用@XmlAttribute而不是@XmlElement進行注釋。

需要考慮的更多最佳做法:

  • PhotosPhoto類中,可以刪除@XmlRootElement批注,因為這些不是根元素。 只有Resp類是根元素,因此需要@XmlRootElement
  • Photos類中,應將photoObject屬性重命名為photoObjects (帶有s ),因為它是一個列表。 這將使代碼更易於理解。

我找到了解決方案。

注釋@XMLAttribute和@XmlElements需要放在setter上。

不適用於獲取器和聲明。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM