Java Jax rs return 列为 xml
Java Jax rs return list as xml
我有休息网络服务,当我 return 一个结果作为 xml 响应是好的,但是当我尝试 return 列为 xml 它 return 奇怪的反应。只有一个对象充满了参数,其他对象没有填充数据。
我的服务 class :
@Path("/result")
public class Service {
@GET
@Path("/{paymentId}")
@Produces("application/xml")
public Response getPaymentResult(@PathParam("paymentId") String paymentId){
List<PaymentResult> ls = new ArrayList<PaymentResult>();
PaymentResult pr = new PaymentResult();
pr.setAmount(123);
pr.setCliendId(paymentId);
pr.setPan("1111111111");
pr.setTypesId(2);
pr.setDate(new Date());
ls.add(pr);
PaymentResult pr1 = new PaymentResult();
pr.setAmount(12123213);
pr.setCliendId(paymentId + "321");
pr.setPan("2222222222");
pr.setTypesId(1);
pr.setDate(new Date());
ls.add(pr1);
PaymentResult pr2 = new PaymentResult();
pr.setAmount(33333333333);
pr.setCliendId(paymentId + "321");
pr.setPan("11313");
pr.setTypesId(3);
pr.setDate(new Date());
ls.add(pr2);
GenericEntity<List<PaymentResult>> entity = new GenericEntity<List<PaymentResult>>(ls){};
Response response = Response.ok(entity).build();
return response;
}
我的模型 class :
@XmlRootElement(name = "result")
public class PaymentResult {
private String cliendId;
private int amount;
private String paymentId;
private String pan;
private int typesId;
private Date date;
public PaymentResult(){}
@XmlElement
public String getCliendId() {
return cliendId;
}
public void setCliendId(String cliendId) {
this.cliendId = cliendId;
}
@XmlElement
public int getAmount() {
return amount;
}
public void setAmount(int amount) {
this.amount = amount;
}
@XmlElement
public String getPaymentId() {
return paymentId;
}
public void setPaymentId(String paymentId) {
this.paymentId = paymentId;
}
@XmlElement
public String getPan() {
return pan;
}
public void setPan(String pan) {
this.pan = pan;
}
@XmlElement
public int getTypesId() {
return typesId;
}
public void setTypesId(int typesId) {
this.typesId = typesId;
}
@XmlElement
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
}
响应是:
<collection>
<result>
<amount>1222222222</amount>
<cliendId>1111111111111111321</cliendId>
<date>2016-01-15T14:21:05.445+04:00</date>
<pan>11313</pan>
<typesId>3</typesId>
</result>
<result>
<amount>0</amount>
<typesId>0</typesId>
</result>
<result>
<amount>0</amount>
<typesId>0</typesId>
</result>
</collection>
当我 return 简单列表时,没有 GenericEntity 也有相同的结果。谢谢
您创建对象 pr
、pr1
、pr2
,但只将值放入 p
。
因此 pr1
和 pr2
具有空值。
即
PaymentResult pr1 = new PaymentResult();
pr.setAmount(12123213);
应该是
PaymentResult pr1 = new PaymentResult();
pr1.setAmount(12123213);
等等
我有休息网络服务,当我 return 一个结果作为 xml 响应是好的,但是当我尝试 return 列为 xml 它 return 奇怪的反应。只有一个对象充满了参数,其他对象没有填充数据。
我的服务 class :
@Path("/result")
public class Service {
@GET
@Path("/{paymentId}")
@Produces("application/xml")
public Response getPaymentResult(@PathParam("paymentId") String paymentId){
List<PaymentResult> ls = new ArrayList<PaymentResult>();
PaymentResult pr = new PaymentResult();
pr.setAmount(123);
pr.setCliendId(paymentId);
pr.setPan("1111111111");
pr.setTypesId(2);
pr.setDate(new Date());
ls.add(pr);
PaymentResult pr1 = new PaymentResult();
pr.setAmount(12123213);
pr.setCliendId(paymentId + "321");
pr.setPan("2222222222");
pr.setTypesId(1);
pr.setDate(new Date());
ls.add(pr1);
PaymentResult pr2 = new PaymentResult();
pr.setAmount(33333333333);
pr.setCliendId(paymentId + "321");
pr.setPan("11313");
pr.setTypesId(3);
pr.setDate(new Date());
ls.add(pr2);
GenericEntity<List<PaymentResult>> entity = new GenericEntity<List<PaymentResult>>(ls){};
Response response = Response.ok(entity).build();
return response;
}
我的模型 class :
@XmlRootElement(name = "result")
public class PaymentResult {
private String cliendId;
private int amount;
private String paymentId;
private String pan;
private int typesId;
private Date date;
public PaymentResult(){}
@XmlElement
public String getCliendId() {
return cliendId;
}
public void setCliendId(String cliendId) {
this.cliendId = cliendId;
}
@XmlElement
public int getAmount() {
return amount;
}
public void setAmount(int amount) {
this.amount = amount;
}
@XmlElement
public String getPaymentId() {
return paymentId;
}
public void setPaymentId(String paymentId) {
this.paymentId = paymentId;
}
@XmlElement
public String getPan() {
return pan;
}
public void setPan(String pan) {
this.pan = pan;
}
@XmlElement
public int getTypesId() {
return typesId;
}
public void setTypesId(int typesId) {
this.typesId = typesId;
}
@XmlElement
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
}
响应是:
<collection>
<result>
<amount>1222222222</amount>
<cliendId>1111111111111111321</cliendId>
<date>2016-01-15T14:21:05.445+04:00</date>
<pan>11313</pan>
<typesId>3</typesId>
</result>
<result>
<amount>0</amount>
<typesId>0</typesId>
</result>
<result>
<amount>0</amount>
<typesId>0</typesId>
</result>
</collection>
当我 return 简单列表时,没有 GenericEntity 也有相同的结果。谢谢
您创建对象 pr
、pr1
、pr2
,但只将值放入 p
。
因此 pr1
和 pr2
具有空值。
即
PaymentResult pr1 = new PaymentResult();
pr.setAmount(12123213);
应该是
PaymentResult pr1 = new PaymentResult();
pr1.setAmount(12123213);
等等