Managing loading resting webservices with spring boot
I am developing a REST client library and REST server using spring / spring-boot. The library will be used as a dependency in other applications.
What is the best way to handle errors, I was thinking about server side error handling, using for example ControllerAdvice to map errors to DTO error class. On the client side (which uses RestTemplate ) I would like:
- error (4xx / 5xx response), then deserialize the DTO error and throw the checked exception (which needs to be handled in applications that use the library)
- normally just deserializes the expected DTO and returns.
I tried to achieve this with a ResponseErrorHandler and I came up with two solutions that are not entirely satisfying to me, so I would like to hear your opinion or get some better suggestions:
idea 1:
public List<SomeDTO> list() throws MyException {
HttpHeaders headers = new HttpHeaders();
headers.add("Accept", MediaType.APPLICATION_JSON_VALUE);
HttpEntity<String> request = new HttpEntity<>(headers);
ResponseEntity<String> response = restTemplate.exchange("endpoint/getAll", HttpMethod.GET, request, String.class);
String responseBody = response.getBody();
try {
if (MyResponseErrorHandler.isError(response.getStatusCode())) {
ErrorInfo error = objectMapper.readValue(responseBody, ErrorInfo.class);
throw new MyException();
} else {
List<SomeDTO> SomeDTOs = Arrays.asList(objectMapper.readValue(responseBody));
return SomeDTOs;
}
} catch (IOException e) {
throw new MyException();
}
}
But it looks like a lot of templates for each method.
idea 2 A
throw custom exception in ResponseErrorHandler, which should be Runtime or IOException, but wrapped with ResourceAccessException in case of IOException. In a client method, an exception can be caught and another can be re-caught. But somewhere (in the catch block?) The error response has to be matched against a custom exception (which can also throw an IOException)
idea 2 B do not use ResponseErrorHandler, catch HttpClientErrorException, get response body from it in catch block using getResponseBodyAsString, map it to custom error type (catching IOException))
Any thoughts? thank you in advance
source to share
The standard way to return an error in a REST API is with HTTP codes.
But in this case, you are developing a client library so you can make your own designs.
I also had this situation. Although only the client library will be used by other people. This way the REST API is hidden from them. Although I've handled the known ControllerAdvice exceptions and always returned an object like
int code;
String message;
Object payload;
Obviously the HTTP code is always 200 (OK).
The advantage of having the response code as ENUM to get all the errors I handled, including the specific message to throw an exception in the client, and I also know the specific payload object class (for successful requests). Therefore, on the client side, this ENUM return code makes it easy to throw exceptions on the client side.
source to share