How to: Spring get rid of @Validate for automatic Controller validation?

后端 未结 3 1691
自闭症患者
自闭症患者 2021-02-09 13:39

I know about @Valid annotation to instruct spring to validate for example a Controller argument according to JSR-303 in such this example:

@GetMappi         


        
3条回答
  •  耶瑟儿~
    2021-02-09 14:18

    It should be possible to do this however like Markus in the similar question I'm not sure I agree its solving an actual problem.

    Without getting too far into the weeds, Spring performs validation as part of model binding within ModelAttributeMethodProcessor's validateIfApplicable method. Per the javadoc

    Validate the model attribute if applicable. The default implementation checks for @javax.validation.Valid, Spring's Validated, and custom annotations whose name starts with "Valid".

    To override this functionality you need to create a custom ModelAttributeMethodProcessor / ServletModelAttributeMethodProcessor. You then would need to register it as an argument resolver

    public class ApplicationConfiguration extends WebMvcConfigurerAdapter {
        @Override
        public void addArgumentResolvers(final List argumentResolvers) {
            // add your custom model attribute processor here
        }
    }
    

提交回复
热议问题