0

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;
}
iCode
  • 8,892
  • 21
  • 57
  • 91
  • It looks, your control is not reaching to handleConflictException method and Postman might be doing some additional stuff on its own ( since , it can't throw a Java exception to end user as it being a UI client ). What is `CustomException` & why control would go there in case of `HttpClientErrorException` ? – Sabir Khan Jul 14 '22 at 13:44
  • The code is reaching handleConflictException method. I had added logs and checked. Besides its custom response with 403. And spring security is not added. How postman would get exact response. – iCode Jul 14 '22 at 13:58
  • Possibly [related](https://stackoverflow.com/questions/13670692/403-forbidden-with-java-but-not-web-browser) & [this one too](https://stackoverflow.com/questions/47272962/403-when-using-spring-boot-but-works-well-with-postman?noredirect=1&lq=1) – Sabir Khan Jul 15 '22 at 03:32
  • Setting the user agent doesn't help. Also tried accepting all types instead of just json – iCode Jul 15 '22 at 06:35

0 Answers0