我们如何将 java 对象传递给 groovyx.net.http.RestClient 的 POST 调用

How can we pass java object to groovyx.net.http.RestClient's POST call

我写了一个 Restfull 网络服务 POST api 调用,这个 api 正在接受 java object 作为请求参数

示例代码:

@POST
@Path("/sample")
@ApiOperation(value = "insert sample data",
        notes = "insert sample data", response = SampleRequest.class)
public Response processSampleData(@ApiParam(value = "SampleRequest", required = true) SampleRequest sampleRequest) {
   //code to insert data
}

我正在编写集成测试方法,但无法将 java object 传递给 RestClient

样本测试方法:

def "process sample data"(){
    when:
    /*String json = '{"sampleDataList":[{ "name": "test1", "id": "12345" },{ "name": "test2", "id": "123456"}]}'*/
    Sample sample= new Sample();
    sample.setName("test1");
    sample.setId("12345");
    SampleRequest reqObj = new SampleRequest();
    reqObj.getSampleList().add(sample);

    //tried with json
    /*HttpResponseDecorator response = getRestClient().post([path: "$BASE_URL"+"/sample", body: json])*/

    //tried with java object also
    HttpResponseDecorator response = getRestClient().post([path: "$BASE_URL"+"/sample", body: SampleRequest])

    then:
    response
}

我尝试同时使用 jsonjava object,但其中 none 对我有用。获取 No encoder found for request content type */* 错误

当我添加 contentType: "application/json 时,它适用于 json 体型。

我变了

HttpResponseDecorator response = getRestClient().post([path: "$BASE_URL"+"/sample",body: json])

HttpResponseDecorator response = getRestClient().post([path: "$BASE_URL"+"/sample",contentType: "application/json" ,body: json])