How do I customize default error message from spring @Valid validation?

前端 未结 10 877
孤街浪徒
孤街浪徒 2020-12-22 23:04

DTO:

public class User {

    @NotNull
    private String name;

    @NotNull
    private String password;

    //..
}

Controller:

10条回答
  •  情书的邮戳
    2020-12-22 23:39

    Add some information too. If you use just @Valid, you need to catch BindException. If you use @Valid @RequestBody catch MethodArgumentNotValidException

    Some sources:
    HandlerMethodArgumentResolverComposite.getArgumentResolver(MethodParameter parameter):129 - search which HandlerMethodArgumentResolver support such parameter RequestResponseBodyMethodProcessor.supportsParameter(MethodParameter parameter) - return true if parameter has annotation @RequestBody

    RequestResponseBodyMethodProcessor:139 - throw MethodArgumentNotValidException ModelAttributeMethodProcessor:164 - throw BindException

提交回复
热议问题