I am developing a REST API and want to validate some @RequestParam
using
@Validated
annotation of Spring.
But the problem is that @Validated
results in ConstraintViolationException
due to which I cannot get details of FieldError so that I can check which Field has error and give proper error object as per requested parameters.
Here is the sample code I am using for handling this validations:
Controller Class
@RestController
@Validated
public class Controller{
@GetMapping("/getValue")
public Response controller(@RequestParam @NotNull String username,
@RequestParam String value, @RequestParam @NotNull @Size(max=21) String id, @RequestParam @Pattern(regexp="")String value){
return responseObject;
}
ValidationHandlerClass
@ControllerAdvice
public class ControllerValidation{
@ExpectionHandler
@ResponseBody
@ResponseStatus(HttpStatus.BAD_REQUEST)
public Set<ConstraintViolation> validateRequest(ConstraintViolationException e){
return e.getConstraintViolations();
}
Can anyone help me how get FieldErrors in such Validation scenario.
Thanks in Advance.