Spring MVC @Valid Validation with custom HandlerMethodArgumentResolver

后端 未结 2 1025
北海茫月
北海茫月 2020-12-14 17:33

I want to register a custom HandlerMethodArgumentResolver that could handle the following @Controller handler method definition

@Re         


        
相关标签:
2条回答
  • 2020-12-14 18:12

    Nice description of the issue that you are facing.

    I checked out the code that you have outlined and have come to the same conclusion that you have - there is no built-in way to have both a custom HandlerMethodArgumentResolver as well as @Valid related validation applied at the same time, the only choice is to do what the ModelAttributeMethodProcessor does which is to check if the parameter has a @Valid annotation and call the validation logic related code.

    You can probably derive your HandlerMethodResolverArgumentResolver from ModelAttributeMethodProcessor and call super.validateIfApplicable(..) atleast this way the existing code is leveraged.

    0 讨论(0)
  • It's may be too late, but your HandlerMethodArgumentResolver gets WebDataBinderFactory object as last argument, then, to hook up the validation, simply add this to your resolver implementation:

    Object resolvedObject = // your logic 
    if(parameter.hasParameterAnnotation(Valid.class){
                binderFactory.createBinder(webRequest,resolvedObject,"resolvedObjectLogicalName").validate ();
    }
    
    0 讨论(0)
提交回复
热议问题