Java Jax rs返回列表为xml

时间:2016-01-15 10:34:00

标签: java xml resteasy

我有休息的Web服务,当我返回一个结果因为xml响应是好的,但当我尝试返回列表作为xml它返回奇怪的响应。只有一个Object满了params,其他对象没有填充数据。

我的服务类:

@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;
    }

我的模特课:

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

当我返回简单列表时,没有GenericEntity的结果相同。谢谢

1 个答案:

答案 0 :(得分:3)

您可以创建对象prpr1pr2,但只会将值放入p。 因此pr1pr2包含空值。

PaymentResult pr1 = new PaymentResult();
pr.setAmount(12123213);

应该是

PaymentResult pr1 = new PaymentResult();
pr1.setAmount(12123213);