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

后端 未结 2 1998
后悔当初
后悔当初 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:02

    The Resteasy Client-API has been marked deprecated as JAX-RS standardized a Client-API. You can now use the equivalent javax.ws.rs classes:

    javax.ws.rs.client.Client client = javax.ws.rs.client.ClientBuilder.newClient();
    javax.ws.rs.client.WebTarget target = client.target("someUrl");
    List<Student> students = 
        target.request().get(new javax.ws.rs.core.GenericType<List<Student>>() {});
    
    0 讨论(0)
  • 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<MyPojo> result = response.readEntity(new GenericType<List<MyPojo>>(){});
        return result;
    } finally {
      if (response != null) {
        response.close();
      }
    }
    
    0 讨论(0)
提交回复
热议问题