简体   繁体   中英

Spring RestTemplate + map XML result to Domain object

With the use of Spring RestTemplate, how can map following XML result to some Domain Object? As a solution I have designed following Domain classes but I am wonder whether how can I map those contain values (ex: 100, 200, 300) to domain object property. Thanks.

XML result

<counting>
 <value id="1" name="Robin" date="2015-09-03">100</value>
 <value id="2" name="Joy" date="2015-09-03">200</value>
 <value id="3" name="Tan" date="2015-09-03">300</value>
<counting>

Domain Class

@XmlRootElement(name = "counting")
public class Count {

  private Value value;

  public Count() {}

  // Getters and setters     
}

@XmlRootElement(name = "value")
public class Value {

  public Value() {}

  private long id;
  private String name;
  private Date date;  

  // Getters and setters  
}

I have solved the issue with following data model

@XmlRootElement(name = "counting")
public class Count {

  private List<Value> value;

  public Count() {}

  // Getters and setters 
  @XmlElement
  public List<Value> getValue() {
    return value;
  }

  public void setValue(List<Value> value) {
    return this.value = value;
  }        
}

@XmlAccessorType(XmlAccessorType.FIELD)
public class Value {

  public Value() {}

  @XmlAttribute
  private long id;

  @XmlAttribute
  private String name;

  @XmlAttribute
  private String date; 

  @XmlValue
  private String xmlValue; 

  // Getters and setters  
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM