Is there a way to integrate java bean validation api With Spring RestTemplate

Viewed 2974

Is there a way to integrate spring RestTemplate with JavaBean validation api. ?

I know there is a integration for spring controller. you can put @Valid on request body param and if Employee is not valid you will get MethodArgumentNotValidException exception . which you can handel in exception handler class.

  @PostMapping(value = "/add", produces = APPLICATION_JSON_VALUE)
  public ResponseEntity<String> addEmployee(
       @RequestBody @Valid Employee emp) {

    //...

  }

But what I want is similar way to validate response from spring restTemplate like when I call this way - I want to get same(or maybe other) exception from spring.

Employee emp = template.exchange("http:///someUrl", HttpMethod.GET, null);

I know I can inject validator like this and call validator.validate(..) on reponse.

  @Autowired
  private Validator validator;

But I do not want to do it each time manually.

1 Answers
Related