1

I have a controller looks like this:

@RestController
@RequestMapping(value="/api/events")
public class EventController{

    @Inject
    private EventValidator eventValidator;

    @InitBinder
    @Qualifier("eventValidatior")
    private void initBinder(WebDataBinder binder){
        binder.setValidator(eventValidator);
    }

    @PostMapping()
    public ResponseEntity<EventModel> save(@Valid @RequestBody EventRequest request, BindingResult result){
        if(result.hasErrors()){
            //some validation
        }
        //some other logic

    }   
}

Then i have a EventRequest pojo:

 public class EventRequest{
 private String eventName;

 @Valid
 @NotNull
 private List<Event> events;

 //setters and getters
}

In my controller, I have 2 types of validation, the InitBinder, and also java bean validation (JSR-303) that use @NotNull in EventRequest class.

The problem is, if I have BindingResult result in the controller, the @NotNull annotation won't work. And even the cascaded validation in Event class is not working too.

Why is that, how can I have both 2 types of validation?


Tried to add this but still not working

@Configuration
public class ValidatorConfig {

 @Bean
 public LocalValidatorFactoryBean defaultValidator() {
    return new LocalValidatorFactoryBean();
 }

 @Bean
 public MethodValidationPostProcessor methodValidationPostProcessor() {
    return new MethodValidationPostProcessor();
 }
}
2

1 Answer 1

2

binder.setValidator(eventValidator); will replace other registered validators.

Change to:

binder.addValidators(eventValidator);

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.