About the question that the JSR303 check is not valid?

in the spring in actin fourth edition, Chapter V 5.4.2 check data section, use JSR303 for data verification, but it does not take effect, and there is no error

uses the implementation of hibernate-validator, and gradle depends on:

 @RequestMapping(value = "/register", method = RequestMethod.POST)
    public String processRegistration(@Valid Spitter spitter, Errors errors) {
        System.out.println(spitter);
        if (errors.hasErrors()) {
            errors.getAllErrors().forEach(System.out::println);
            return "registerForm";
        }

        spitterRepository.save(spitter);

        return "redirect:/spitter/" + spitter.getUsername();
    }
Mar.03,2021

I tried it. I have no problem with your code. If there is a problem with the parameters, you can enter the if block. For example, when you pass only username and password, the output is as follows:

Field error in object 'spitter' on field 'lastName': rejected value [null]; codes [NotNull.spitter.lastName,NotNull.lastName,NotNull.java.lang.String,NotNull]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [spitter.lastName,lastName]; arguments []; default message [lastName]]; default message [null]
Field error in object 'spitter' on field 'email': rejected value [null]; codes [NotNull.spitter.email,NotNull.email,NotNull.java.lang.String,NotNull]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [spitter.email,email]; arguments []; default message [email]]; default message [null]
Field error in object 'spitter' on field 'firstName': rejected value [null]; codes [NotNull.spitter.firstName,NotNull.firstName,NotNull.java.lang.String,NotNull]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [spitter.firstName,firstName]; arguments []; default message [firstName]]; default message [null]

has the landlord solved it

Menu