How to turn on annotation driven validation in Spring 4?

后端 未结 2 984
耶瑟儿~
耶瑟儿~ 2020-12-10 21:10

I am using the annotation validation as below:

    public String processRegistration(@Valid Spitter spitter, Errors errors,
            Model model) {
               


        
2条回答
  •  臣服心动
    2020-12-10 21:49

    Judging from your explanation and the following error java.lang.ClassNotFoundException: javax.validation.Validator Spring doesn't see the classes and as such doesn't enable JSF-303 validation.

    Make sure that the correct jars are on the classpath and that you have an implementation as well. When using maven adding something like the following should do the trick.

    
        javax.validation
        validation-api
        1.1.0.Final
    
    
        org.hibernate
        hibernate-validator
        5.1.3.Final
    
    

    This will add the needed jars to the WEB-INF/lib directory which in turn lets Spring Web MVC detect it and configure the appropriate bean.

    For an explanations of the different annotations you might want to check In Hibernate Validator 4.1+, what is the difference between @NotNull, @NotEmpty, and @NotBlank?.

提交回复
热议问题