Channa
Channa

Reputation: 5233

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  
}

Upvotes: 2

Views: 1468

Answers (1)

Channa
Channa

Reputation: 5233

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  
}

Upvotes: 1

Related Questions