如何使用JAX-RS grails发布域对象集合

时间:2013-09-03 00:49:54

标签: rest grails jax-rs

我正在使用带有grails的JAX-RS插件(版本0.8),我有一个代表单个数据点的域类

class DataPoint {

   static hasOne = [user: User]
   int time
   int accelerationX
   int accelerationY
   int accelerationZ
   ....
}

现在我希望能够发布这些集合,以减少服务器的点击次数(我们以高频率进行采样)。

我知道JAX-RS插件不支持Domain Class Collections作为输入,所以我在src / groovy中写了一个Wrapper

public class DataPoints {

   List<DataPoint> data = new ArrayList<>();

   public void add(DataPoint dataPoint) {
       data.add(dataPoint)
   }

   public List<DataPoint> getData() {
       return data
   }
}

我使用了生成的Resource类

@Path('/api/data')
@Consumes(['application/xml', 'application/json'])
@Produces(['application/xml', 'application/json'])
class DataPointCollectionResource {

   def dataPointResourceService

   @POST
   Response create(DataPoints dto) {
       created dataPointResourceService.create(dto) //overwritten to take wrapper class
   }

   @GET
   Response readAll() {
       DataPoints dataPoints = new DataPoints();
       DataPoint.findAll().each {
           dataPoints.add(it)
       }
       ok dataPoints
   }
}

然而,这无效。

我尝试发布一些xml

<dataPoints>
  <data>
    <dataPoint>
      <accelerationX>0</accelerationX>
      <accelerationY>0</accelerationY>
      <accelerationZ>0</accelerationZ>
      <user id="1"/>
    </dataPoint>
    <dataPoint>
      <accelerationX>0</accelerationX>
      <accelerationY>0</accelerationY>
      <accelerationZ>0</accelerationZ>
      <user id="1"/>
    </dataPoint>
  </data>
</dataPoints>

使用curl

curl -H "Content-Type: application/xml" -H "Accept: application/xml" --request POST -d <xml data> <path to resource>

我得到的错误是:

ERROR container.ContainerRequest  - A message body reader for Java class 
com.wristband.atlas.SensorDataPoints, and Java type class     
com.wristband.atlas.SensorDataPoints, and MIME media type application/xml was not found.
The registered message body readers compatible with the MIME media type are:

尝试在资源上进行GET给了我几乎相同的东西。

我知道我错过了一些东西,因为我之前在java中已经完成了这个,之后我才知道我缺少什么配置。

干杯

1 个答案:

答案 0 :(得分:0)

试试这个

@GET
Response readAll() {
   ok DataPoint.findAll()
}
相关问题