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?