What should we use for ClientResponse and GenericType in latest version (3.0.x) Resteasy?

后端 未结 2 2001
后悔当初
后悔当初 2021-01-15 12:25

I am developing Resteasy. I migrated my application\'s maven dependencies from 2.2.x to 3.0.x and suddenly I saw most of the API\'s ar

2条回答
  •  余生分开走
    2021-01-15 13:13

    Here's an example with query parameters and a check for the HTTP response code, which is handy for failing fast and avoiding stack traces from the JSON mapper, often hiding the real cause:

    Response response = null;
    try {
      response = target.path("apiMethod")
                    .queryParam("stringParam", "test")
                    .queryParam("booleanParam", Boolean.FALSE)
                    .request().accept(MediaType.APPLICATION_JSON).get();
    
      if (response.getStatus() == Response.Status.BAD_REQUEST.getStatusCode()) { // HTTP 400
        throw new BadRequestException("invalid parameters!");
      } else if (response.getStatus() == 
        Response.Status.NOT_FOUND.getStatusCode()) {  // HTTP 404
            throw new NotFoundException("resource was not found on server!");
      }
    
        List result = response.readEntity(new GenericType>(){});
        return result;
    } finally {
      if (response != null) {
        response.close();
      }
    }
    

提交回复
热议问题