JAX-RS返回自定义对象的集合?

时间:2013-03-30 14:03:37

标签: jpa jax-rs

我有一个使用JAX-RS,JPA和JAXB的web服务。我写了一个方法,它应该从我的数据库中返回一组对象。但这不会发生。它只返回一个项目。

我的方法:

@GET
    @Produces(MediaType.APPLICATION_XML)
    @Path("findMeasurementsByRunID/{runID}")
    public List<Measurement> getMeasurementByRunId(@PathParam("runID") int runID) {
        List<Measurement> results = null;
        Query query = emf.createEntityManager().createNamedQuery(
                "findMeasurementsByRunID");
        query.setParameter("runid", runID);
        results = query.getResultList();
        return results;
    }

我的实体级:

    @Entity
    @NamedQueries({
            @NamedQuery(name = "findMeasurementsByRunID", query = "SELECT m "
                    + "FROM Measurement m "
                    + "WHERE m.runID = :runid"),
            @NamedQuery(name = "findMeasurementsByRunIDPosition", query = "SELECT m "
                    + "FROM Measurement m "
                    + "WHERE m.runID = :runid AND "
                    + "m.position = :position") })
    @XmlRootElement
    @XmlType(propOrder = { "runID", "rack", "position", "completionStatus",
            "countingTime", "cr51Counts", "cr51CPM", "cr51Error",
            "measurementDateTime", "protocolID", "protocolName" })
    public class Measurement implements Serializable {
        private static final long serialVersionUID = 1L;

        @Id
        @AttributeOverrides({
                @AttributeOverride(name = "runID", column = @Column(name = "RunID")),
                @AttributeOverride(name = "position", column = @Column(name = "Position")) })
        @Column(name = "RunID")
        private int runID;

        @Column(name = "Rack")
        private int rack;

        @Column(name = "Position")
        private int position;

        @Column(name = "Completionstatus")
        private int completionStatus;

        @Column(name = "CountingTime")
        private double countingTime;

        @Column(name = "Cr51Counts")
        private double cr51Counts;

        @Column(name = "Cr51CPM")
        private double cr51CPM;

        @Column(name = "Cr51Error")
        private double cr51Error;

        @Column(name = "MeasurementDateTime")
        @Temporal(TemporalType.TIMESTAMP)
        private Date measurementDateTime;

        @Column(name = "ProtocolID")
        private int protocolID;

        @Column(name = "ProtocolName")
        private String protocolName;

        public Measurement() {
        }

            // Getters and Setters...
}

如果我调用该服务,它只会将查询中的第一项作为xml而不是整个集合返回。

输出:

<measurements>
  <measurement>
    <runID>418</runID>
    <rack>57</rack>
    <position>1</position>
    <completionStatus>0</completionStatus>
    <countingTime>3599.97</countingTime>
    <cr51Counts>2225.53</cr51Counts>
    <cr51CPM>5.11</cr51CPM>
    <cr51Error>44.26</cr51Error>
    <measurementDateTime>2012-12-14T14:08:37.0</measurementDateTime>
    <protocolID>3</protocolID>
    <protocolName>Cr-51 GFR (almindelig blodproever)</protocolName>
  </measurement>
</measurements>

如何让服务返回整个测量集合,而不仅仅是一个项目?

我的数据: enter image description here

1 个答案:

答案 0 :(得分:0)

因此,您执行一些查询“findMeasurementsByRunID”,将其限制为特定的“runId”,您不明白为什么只获得1个Measurement对象?更新您的查询以删除/更改过滤器,以便它允许多个