There is a front that sends different requests for an intermediate service (hereinafter loadbalancer ), it redirects requests for the api service (hereinafter api ).

The same loadbalancer :

@RestController @RequestMapping("/api/profile") public class ProfileController { @Autowired private ProfileProcessor profileProcessor; @PostMapping @ResponseBody public Long create(@RequestBody ProfileInfo request) { LOGGER.info("Called createProfile... "); return profileProcessor.create(request); } @Component public class ProfileProcessor { @LoadBalanced @Autowired private RestTemplate restTemplate; public Long create(ProfileInfo request) { return restTemplate.postForObject(serviceURI + "/profile", request, Long.class); } 

api :

 @RestController @RequestMapping("/profile") public class ProfileController { @Autowired private ProfileRepository profileRepository; @PostMapping @ResponseBody public Long createProfile(@Valid @RequestBody ProfileEntity request) { return profileRepository.createProfile(request); } 

then an error occurs, because an invalid profile came in the request, the exception handler intercepts it:

 @ControllerAdvice public class ExceptionHandlingController { @Autowired private Validation validationUtil; @ExceptionHandler(value = MethodArgumentNotValidException.class) public ResponseEntity<ExceptionResponse> invalidInput(MethodArgumentNotValidException ex) { ExceptionResponse response = new ExceptionResponse(); BindingResult result = ex.getBindingResult(); response.setErrorCode("400"); response.setErrorMessage("Invalid inputs."); response.setErrors(validationUtil.fromBindingErrors(result)); return new ResponseEntity<ExceptionResponse>(response, HttpStatus.BAD_REQUEST); } 

it turns out that the loadbalancer does not return the expected Long (id), but the ResponseEntity with the error info, and a JsonMappingException error occurs.

How can I intercept ResponseEntity in loadbalancer in such a situation?

    1 answer 1

    Here is a similar question .

     @Configuration public class RestCommunicationConfig { @Bean public RestOperations restOperations(ResponseErrorHandler errorHandler){ RestTemplate restTemplate = new RestTemplate(); restTemplate.setErrorHandler(mmsErrorHandler); return restTemplate; } @Bean public ResponseErrorHandler errorHandler(){ return new ResponseErrorHandler() { @Override public boolean hasError(ClientHttpResponse response) throws IOException { HttpStatus httpStatus = response.getStatusCode(); return httpStatus.is4xxClientError() || httpStatus.is5xxServerError(); } @Override public void handleError(ClientHttpResponse response) throws IOException { ObjectMapper objectMapper = new ObjectMapper(); MyErrorEntity myErrorEntity = objectMapper.readValue(response.getBody(), MyErrorEntity.class); /* либо залогировать, либо создать свое исключение и выбросить, а затем обработать на этой строчке -> return restTemplate.postForObject(serviceURI + "/profile", request, Long.class);*/ } }; } } 
    • You can improve the quality of the response if you transfer the comment from the code into the response body. - 0xdb