天天看點

java response 401,當HTTP請求具有傳回狀态401時,如何在Java中解析響應主體

java response 401,當HTTP請求具有傳回狀态401時,如何在Java中解析響應主體

I am consuming a RESTful JSON API using Spring's RestTemplate and Jackson. In some cases we may receive a Status 401 (Unauthorized) response with a custom JSON body, that is defined by the API manufacturer, and looks like this:

{

"code": 123,

"message": "Reason for the error"

}

We need to parse the body, and use the code property in our business logic.

This is the error response Java object we need to parse to:

public class CustomError {

@JsonProperty

private Integer code;

@JsonProperty

private String message;

public Integer getCode() {

return code;

}

public String getMessage() {

return message;

}

}

And a custom error handler to do this:

public class CustomErrorHandler extends DefaultResponseErrorHandler {

private RestTemplate restTemplate;

private ObjectMapper objectMapper;

private MappingJacksonHttpMessageConverter messageConverter;

@Override

public boolean hasError(ClientHttpResponse response) throws IOException {

return super.hasError(response);

}

@Override

public void handleError(final ClientHttpResponse response) throws IOException {

try {

CustomError error =

(CustomError) messageConverter.read(CustomError.class, response);

throw new CustomErrorIOException(error, error.getMessage());

} catch (Exception e) {

// parsing failed, resort to default behavior

super.handleError(response);

}

}

}

The error handler fails with an HttpMessageNotReadableException in the try block:

"Could not read JSON: cannot retry due to server authentication, in streaming mode"

This is how I am sending requests:

restTemplate.postForObject(url, pojoInstance, responseClass);

If the same request is executed with a plain old rest client program, like Postman, the expected JSON response is received. So, I assume the problem could be with the Spring's ClientHttpResponse implementation somehow not allowing access to the response body, in case of the 401 status.

Is it indeed possible to parse the response body?

Update

From what I investigated, the RestTemplate class uses ClientHttpResponse which in turn creates an sun.net.www.protocol.http.HttpURLConnection that provides the input stream. It is there, where the input stream is being neglected and an IOException is thrown:

cannot retry due to server authentication, in streaming mode

So, the HttpURLConnection's implementation is causing the issue.

Will it be possible to avoid this problem? Perhaps we should use an alternative implementation that does not ignore the response body in case of an error status code? Can you recommend any alternatives?

解決方案

Try the following approach without needing a custom handler. The idea is to get the response as a string from the HttpStatusCodeException, and then you can convert it to your object. For the conversion I used the Jackson's ObjectMapper:

try {

restTemplate.postForObject(url, pojoInstance, responseClass);

} catch (HttpStatusCodeException e) {

if (e.getStatusCode() == HttpStatus.UNAUTHORIZED) {

String responseString = e.getResponseBodyAsString();

ObjectMapper mapper = new ObjectMapper();

CustomError result = mapper.readValue(responseString,

CustomError.class);

}

}

Update:

Usage of a different factory may also help since there is a bug in the default one related to your issue (see comment below):

RestTemplate template = new RestTemplate(new HttpComponentsClientHttpRequestFactory());