Java 从XML解析后,JAXB解组器在对象中返回null

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"

我有一个字符串对象,其中包含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", 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响应中,您有
    (即
    stat
    是一个XML属性) 而不是
    ok…
    stat
    是XML元素)。
    因此,在
    Resp
    类中,
    stat
    属性需要用
    @xmldattribute
    而不是
    @xmlement
  • 出于同样的原因,在
    照片
    类中,所有属性
    除了
    photoObject外
    属性需要用
    @xmldattribute
    而不是
    @xmlement
  • 出于同样的原因,在
    Photo
    类中,所有属性 需要使用
    @xmldattribute
    而不是
    @xmlement
    进行注释
需要考虑的其他一些最佳实践:

  • Photos
    Photo
    类中,可以删除
    @XmlRootElement
    注释 因为这些不是根元素。 只有
    Resp
    类是根元素,因此需要
    @XmlRootElement
  • Photos
    类中,您应该将
    photoObject
    属性重命名为
    photoObjects
    (用复数s)因为它是一个列表。这将使代码更容易理解
    • 我找到了解决办法

      注释@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