Can I override RESTClient default “HttpResponseException” response to >399 Return Codes?

后端 未结 3 1311
面向向阳花
面向向阳花 2021-02-13 15:58

I\'m using the Groovy RESTClient class to do write some (spock) Acceptance tests for Java WebServices I\'ve been authoring.

One frustration I\'ve had is in testing the r

相关标签:
3条回答
  • 2021-02-13 16:38

    As Tomasz already linked in a comment, here is my little one-liner answer from a similar question.

    client.handler.failure = client.handler.success
    
    0 讨论(0)
  • 2021-02-13 16:52

    @JonPeterson came up with what I think is a better solution, so I gave his answer the checkmark:

    client.handler.failure = client.handler.success
    

    More info here


    The solution I (previously) landed on:

     restClient.handler.failure = { it }
    

    Which is shorthand for

    restClient.handler.failure = { resp -> return resp }
    

    @JimmyLuong noted in the comments that this approach drops the data from the response, and suggested the following enhancement:

     restClient.handler.failure = { resp, data -> resp.setData(data); return resp }
    
    0 讨论(0)
  • 2021-02-13 17:01

    One approach would be to make a Groovy convenience method that wraps your REST calls that catches exceptions and turns them into status codes

    0 讨论(0)
提交回复
热议问题