I have 2 spring boot applications with Rest APIs and I'm running it locally for testing. I'm using Rest Template to connect from one service to the second service and getting the response data and processing based on the response.
Spring security is not implemented in both services. Both are simple REST API services only.
As part of it, error responses is handled through exception handling and reading the error json. But for 403 error I'm getting the response body as empty in the HttpClientErrorException. However in postman I'm getting proper json format.
In first service, I use Exception Handler to send error response.
Apart from 403, I'm getting all other response body as json string and I'm handling it. But for 403, the response body is empty. And through postman response coming properly. So I believe the server code is fine. What could be the issue?
Service 1
@ExceptionHandler(CustomException.class)
public final ResponseEntity<FailureResponse> handleConflictException(OperationNotAllowed exception,
WebRequest request) {
ErrorResult response = new ErrorResult ("Unauthorized", "Unauthorized", exception.getErrorList());
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(response);
}
Service 2 - Rest Template
public SuccessResult<MyResponseObject> restConnectPost(String url, MyRequestObj request,
MultiValueMap<String, String> headers) throws MyCustomException {
log.debug("{} - Connecting to service", url);
SuccessResult<MyResponseObject> response = null;
try {
headers.set(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE);
HttpEntity<MyRequestObj> requestEntity = new HttpEntity<MyRequestObj>(request, headers);
ResponseEntity<SuccessResult<MyResponseObject>> exchange = restClient.getTemplate().exchange(url,
HttpMethod.post, requestEntity, new ParameterizedTypeReference<SuccessResult<MyResponseObject>>() {
});
response = exchange.getBody();
} catch (HttpClientErrorException | HttpServerErrorException http) {
log.error("Response contain errors: {}", http.getResponseBodyAsString());
// Handling code
} catch (Exception e) {
log.error("Exception while connecting to service {} " + e.getMessage());
throw e;
}
return response;
}