要求的xml数据格式:
<info detail="" error="0" timeservice="8ms" type="hot_car_details">
<hot_car carReferPrice="40.04" carYear="2017" gearBox="8挡手自一体" id="121280" name="40TFSI 进取型" power="230马力" price="30.83"/>
</info>
建立数据模型:
package com.fh.entity;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "info")
public class Info {
HotCar hot_car;
String type;
String error;
String timeservice;
String detail;
public HotCar getHot_car() {
return hot_car;
}
@XmlElement(name = "hot_car")
public void setHot_car(HotCar hot_car) {
this.hot_car = hot_car;
}
public String getType() {
return type;
}
@XmlAttribute
public void setType(String type) {
this.type = type;
}
public String getError() {
return error;
}
@XmlAttribute
public void setError(String error) {
this.error = error;
}
public String getTimeservice() {
return timeservice;
}
@XmlAttribute
public void setTimeservice(String timeservice) {
this.timeservice = timeservice;
}
public String getDetail() {
return detail;
}
@XmlAttribute
public void setDetail(String detail) {
this.detail = detail;
}
}
package com.fh.entity;
import javax.xml.bind.annotation.XmlAttribute;
public class HotCar {
String id;
String name;
String power;
String carYear;
String gearBox;
String price;
String carReferPrice;
public String getId() {
return id;
}
@XmlAttribute
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
@XmlAttribute
public void setName(String name) {
this.name = name;
}
public String getPower() {
return power;
}
@XmlAttribute
public void setPower(String power) {
this.power = power;
}
public String getCarYear() {
return carYear;
}
@XmlAttribute
public void setCarYear(String carYear) {
this.carYear = carYear;
}
public String getGearBox() {
return gearBox;
}
@XmlAttribute
public void setGearBox(String gearBox) {
this.gearBox = gearBox;
}
public String getPrice() {
return price;
}
@XmlAttribute
public void setPrice(String price) {
this.price = price;
}
public String getCarReferPrice() {
return carReferPrice;
}
@XmlAttribute
public void setCarReferPrice(String carReferPrice) {
this.carReferPrice = carReferPrice;
}
}
控制器里返回xml的代码:
@RequestMapping(value = "/hotCar")
public @ResponseBody Info hotCar(){
Info info = new Info();
info.setType("hot_car_details");
info.setError("0");
info.setTimeservice("8ms");
info.setDetail("");
HotCar hotCar = new HotCar();
hotCar.setId("121280");
hotCar.setName("40TFSI 进取型");
hotCar.setPower("230马力");
hotCar.setCarYear("2017");
hotCar.setGearBox("8挡手自一体");
hotCar.setPrice("30.83");
hotCar.setCarReferPrice("40.04");
info.setHot_car(hotCar);
return info;
}