如何从RestTemplate调用URL中提取HTTP状态代码?

我正在使用RestTemplate对我们的服务进行HTTP调用,该服务返回一个简单的JSON响应。 我根本不需要解析那个JSON。 我只需要返回我从该服务中获得的任何内容。

所以我将它映射到String.class并将实际的JSON response作为字符串返回。

 RestTemplate restTemplate = new RestTemplate(); String response = restTemplate.getForObject(url, String.class); return response; 

现在的问题是 –

我试图在点击URL后提取HTTP Status codes 。 如何从上面的代码中提取HTTP状态代码? 我是否需要以当前的方式对其进行任何更改?

更新: –

这是我尝试过的,我能够得到回复和状态代码。 但是我总是需要像下面那样设置HttpHeadersEntity对象吗?

  RestTemplate restTemplate = new RestTemplate(); //and do I need this JSON media type for my use case? HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_JSON); //set my entity HttpEntity entity = new HttpEntity(headers); ResponseEntity out = restTemplate.exchange(url, HttpMethod.GET, entity, String.class); System.out.println(out.getBody()); System.out.println(out.getStatusCode()); 

几个问题 – 我需要有MediaType.APPLICATION_JSON因为我只是调用url来返回响应,它可以返回JSON或XML或简单的字符串。

使用返回ResponseEntityRestTemplate#exchange(..)方法。 这使您可以访问状态行和标题(显然是身体)。

如果你不想把漂亮的抽象RestTemplate.get/postForObject...方法背后像我一样,不喜欢摆弄使用RestTemplate.exchange...所需的样板RestTemplate.exchange... (Request-和ResponseEntity,HttpHeaders,等),还有另一种选择来访问HttpStatus代码。

只需使用try / catch for org.springframework.web.client.HttpClientErrorExceptionorg.springframework.web.client.HttpServerErrorException包围通常的RestTemplate.get/postForObject... ,就像在这个例子中一样:

 try { return restTemplate.postForObject("http://your.url.here", "YourRequestObjectForPostBodyHere", YourResponse.class); } catch (HttpClientErrorException | HttpServerErrorException httpClientOrServerExc) { if(HttpStatus.NOT_FOUND.equals(httpClientOrServerExc.getStatusCode())) { // your handling of "NOT FOUND" here // eg throw new RuntimeException("Your Error Message here", httpClientOrServerExc); } else { // your handling of other errors here } 

这里添加了org.springframework.web.client.HttpServerErrorException ,用于50x的错误。

现在你可以简单地对所需的所有StatusCode作出反应 – 除了适当的一个,与你的HTTP方法相匹配 – 比如GET200 ,它们不会作为例外处理,因为它是匹配的。 但是,如果您正在实施/使用RESTful服务,这应该是直截了当的:)

有人可能会陷入一些稍微棘手的用例(正如我所做的那样)。 考虑以下:

支持Page对象以便将其与RestTemplate和ParameterizedTypeReference一起使用:

RestPageResponse

 import java.util.ArrayList; import java.util.List; import org.springframework.data.domain.PageImpl; import org.springframework.data.domain.Pageable; public class RestResponsePage extends PageImpl{ private static final long serialVersionUID = 3248189030448292002L; public RestResponsePage(List content, Pageable pageable, long total) { super(content, pageable, total); } public RestResponsePage(List content) { super(content); } public RestResponsePage() { super(new ArrayList()); } } 

使用ParameterizedTypeReference将产生以下结果:

 ParameterizedTypeReference> responseType = new ParameterizedTypeReference>() {}; HttpEntity> response = restTemplate.exchange(oauthUrl, HttpMethod.GET, entity, responseType); 

调用#exchange :

 HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.MULTIPART_FORM_DATA); HttpEntity entity = new HttpEntity<>(headers); response = restTemplate.exchange("localhost:8080/example", HttpMethod.GET, entity, responseType); 

现在这里是“棘手”的部分。

试图调用exchange的getStatusCode是不可能的,因为不幸的是,编译器不会意识到“预期”类型的response

这是因为generics是通过类型擦除实现的,它在编译期间删除了有关generics类型的所有信息(更多信息 – 源代码)

((ResponseEntity>) response).getStatusCode()

在这种情况下,您必须显式地将变量statusCode转换为所需的类以获取statusCode (和/或其他属性)!

 private RestTemplate restTemplate = new RestTemplate(); ResponseEntity response = restTemplate.exchange(url,HttpMethod.GET, requestEntity,String.class); 

响应包含’body’,’headers’和’statusCode’

获取statusCode:response.getStatusCode();