如何在解析为dto之前获得http jersey响应状态?

时间:2015-02-15 13:00:14

标签: java rest http jersey jersey-client

我的代码曾经是:

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

我补充说:

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

如果我甚至没有得到ClientResponse.class

,我怎样才能获得http结果代码

你怎么会得到这个状态?

1 个答案:

答案 0 :(得分:2)

没有理由不能同时读取客户端响应和实体主体。

    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);