【问题标题】:How can we pass java object to groovyx.net.http.RestClient's POST call我们如何将 java 对象传递给 groovyx.net.http.RestClient 的 POST 调用
【发布时间】:2018-10-12 09:25:59
【问题描述】:

我写了一个Restfull webservice 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
}

我尝试同时使用json 和java object,但它们都不适合我。获取No encoder found for request content type */* 错误

【问题讨论】:

    标签: java spock rest-client


    【解决方案1】:

    当我添加 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])
    

    【讨论】:

      猜你喜欢
      • 2014-06-24
      • 1970-01-01
      • 1970-01-01
      • 2023-02-24
      • 1970-01-01
      • 2018-01-08
      • 2020-10-15
      • 2014-06-23
      • 2019-06-01
      相关资源
      最近更新 更多