(This is blueprint of our service)
webpage <-> proxy-server <-> api-server <-> datebase
[-------- my work ---------][------- not mine -------]
I send request from proxy-server to api-server using "RestTemplate.exchange".
@Override
public <T> ResponseEntity<T> exchange(RequestEntity<?> requestEntity, ParameterizedTypeReference<T> responseType)
throws RestClientException {
Assert.notNull(requestEntity, "'requestEntity' must not be null");
Type type = responseType.getType();
RequestCallback requestCallback = httpEntityCallback(requestEntity, type);
ResponseExtractor<ResponseEntity<T>> responseExtractor = responseEntityExtractor(type);
return execute(requestEntity.getUrl(), requestEntity.getMethod(), requestCallback, responseExtractor);
}
And this is my question.
Api server make 503 error with ?VO. They send me like this.
{
"result": {
"responseCode": "..."
},
"data": {
"time": ...,
}
}
But when this response is arrived at restTemplate, restTemplate send it to errorHandler and catch exception.
(in RestTemplate.java)
protected void handleResponse(URI url, HttpMethod method, ClientHttpResponse response) throws IOException {
ResponseErrorHandler errorHandler = getErrorHandler();
boolean hasError = errorHandler.hasError(response); // -> hasError:true!
...
}
And I can't get response outside of function. How I get this response?
You need to catch HttpStatusCodeException
exception for this. And you will get http status code by calling getStatusCode()
and the body by calling getResponseBodyAsString()
method of the exception.
Example:
try {
restTemplate.exchange(...);
} catch (HttpStatusCodeException e) {
return new ResponseEntity<>(e.getResponseBodyAsString(), e.getStatusCode());
}