How to I get Spring-Data-MongoDB to validate my objects?

前端 未结 4 983
悲哀的现实
悲哀的现实 2020-12-03 03:16

I have a very simple Spring Boot application that uses Spring-Data-Mongodb

All I want to do is set a JSR-303 validation rule that says the object I\'m saving must ha

4条回答
  •  情书的邮戳
    2020-12-03 03:56

    I found that if I add

    public User addUser(@RequestBody  @Valid User newUser, 
                       BindingResult bindingResult) throws Exception {
    
      if (bindingResult.hasErrors()) {
        throw new Exception("Validation Error");
      }
    

    To my controller this validates the incoming json against my rules, though I should still try and setup the validatingMongoEventListener to intercept any other parts of my code that attempt to update the model with invalid data.

提交回复
热议问题