When using rest template we can handle response statuses other than 200 based on the exception thrown by resttemplate. This article describes this in detail.
By default, the RestTemplate will throw one of these exceptions in the case of an HTTP error:
HttpClientErrorException – in the case of HTTP status 4xx
HttpServerErrorException – in the case of HTTP status 5xx
UnknownHttpStatusCodeException – in the case of an unknown HTTP status
Is a similar approach possible when using java HttpClient. I am using JSONBodyHandler approach to automatically serialize the response into a pojo.
// create a client
var client = HttpClient.newHttpClient();
// create a request
var request = HttpRequest.newBuilder(
URI.create("https://api.nasa.gov/planetary/apod?api_key=DEMO_KEY"))
.header("accept", "application/json")
.build();
// use the client to send the request
var response = client.send(request, new JsonBodyHandler<>(APOD.class));
APOD apod = response.body().get();
The above works well when the response is 200 but it does not work when there is some server error or client error.
Say for a client error, with an incorrect api key in the request (i.e query param i.e api_key=Incorrect_KEY),
curl --location 'https://api.nasa.gov/planetary/apod?api_key=INCORRECT_KEY' the response from server is 403 -
{
"error": {
"code": "API_KEY_INVALID",
"message": "An invalid api_key was supplied. Get one at https://api.nasa.gov:443"
}
}
The above is definitely different from APOD.class structure and cannot be deserialzed in to this class. I don't see any exception being thrown by HttpClient and as Jackson tries to deserialze it throws UnrecognizedPropertyException: Unrecognized field "error" and every thing fails. The error response message: An invalid api_key was supplied...etc, from server is important for me so i can correct and resent the request.
How to handle above cases of SUCCESS and ERROR responses from server using the same code flow.
(I am NOT looking at BodyHandlers.ofString() approach as I want to continue using the JSONBodyHandler approach.)
Check the responses status code - that's what you are indicating anyway. But even if it is different than 200 you can still evaluate the response body.