Search code examples
javaresthttpjerseyjersey-client

how to get http jersey response status before parsing to dto?


my code used to be:

ClientResponse clientResponse = webResource.path("routingRequest")
                    .queryParam("at", rr.at)
                    .get(ClientResponse.class);

I added:

RoutingResponse routingResponse = webResource.path("routingRequest")
                    .queryParam("at", rr.at)
                    .get(ClientResponse.class)
                    .getEntity(RoutingResponse.class);

How can I get the http result code, now when I don't even get the ClientResponse.class

How would you get this status?


Solution

  • There is no reason why you cannot read the client response and the entity body in the same breath.

        ClientConfig cc = new DefaultClientConfig();
        cc.getProperties().put(
                ClientConfig.PROPERTY_FOLLOW_REDIRECTS, true);
        Client c = Client.create(cc);
        WebResource r = c.resource("https://my_url");
        ClientResponse response = r.get(ClientResponse.class);
        EntityTag e = response.getEntityTag();
        String entity = response.getEntity(String.class);
        System.out.println("The response status is " + response.getStatus());
        System.out.println("The enttiy body is " + entity);