{"id":4908,"date":"2024-10-06T20:01:03","date_gmt":"2024-10-06T12:01:03","guid":{"rendered":""},"modified":"2024-10-06T20:01:03","modified_gmt":"2024-10-06T12:01:03","slug":"java\u53c2\u6570\u6821\u9a8c_\u6b63\u5219\u8868\u8fbe\u5f0f\u5728\u7ebf\u6d4b\u8bd5\u5de5\u5177","status":"publish","type":"post","link":"https:\/\/mushiming.com\/4908.html","title":{"rendered":"java\u53c2\u6570\u6821\u9a8c_\u6b63\u5219\u8868\u8fbe\u5f0f\u5728\u7ebf\u6d4b\u8bd5\u5de5\u5177"},"content":{"rendered":"

\n <\/path> \n<\/svg> <\/p>\n

Validator \u7ed3\u5408 @InitBinder \u6ce8\u89e3<\/h2>\n

https:\/\/blog.csdn.net\/_\/article\/details\/<\/p>\n

1 \u5b9e\u73b0\u4e00\u4e2aValidator<\/strong><\/p>\n

\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u9700\u8981\u6211\u4eec\u7684bean\u91cc\u9762\u6709\u4efb\u4f55\u6ce8\u89e3\u4e4b\u7c7b\u7684\u4e1c\u897f<\/p>\n

\u5b9e\u73b0Validator<\/span> \u91cd\u5199\u4e24\u4e2a\u65b9\u6cd5 @Component<\/span> public<\/span> class<\/span> UserValidator<\/span> implements<\/span> Validator<\/span> { \n   <\/span> @Override<\/span> public<\/span> boolean<\/span> supports<\/span>(<\/span>Class<\/span> clazz)<\/span> { \n   <\/span> return<\/span> User2<\/span>.<\/span>class<\/span>.<\/span>equals<\/span>(<\/span>clazz)<\/span>;<\/span> }<\/span> @Override<\/span> public<\/span> void<\/span> validate<\/span>(<\/span>Object<\/span> target,<\/span> Errors<\/span> errors)<\/span> { \n   <\/span> ValidationUtils<\/span>.<\/span>rejectIfEmpty<\/span>(<\/span>errors,<\/span> \"name\"<\/span>,<\/span> \"name.empty\"<\/span>)<\/span>;<\/span> User2<\/span> p =<\/span> (<\/span>User2<\/span>)<\/span> target;<\/span> if<\/span> (<\/span>p.<\/span>getId<\/span>(<\/span>)<\/span> ==<\/span> 0<\/span>)<\/span> { \n   <\/span> errors.<\/span>rejectValue<\/span>(<\/span>\"id\"<\/span>,<\/span> \"can not be zero\"<\/span>)<\/span>;<\/span> }<\/span> }<\/span> }<\/span> <\/code><\/pre>\n

2 \u4fee\u6539Controller\u4ee3\u7801<\/strong><\/p>\n

@InitBinder<\/span> \u6ce8\u89e3\u6dfb\u52a0\u5728initBinder \u65b9\u6cd5 initBinder \u65b9\u6cd5 binder \u52a0\u5165 validator \u6ce8\u5165\u4e0a\u9762\u7684UserValidator<\/span>\u5b9e\u4f8b \u5e76\u7ed9Controller<\/span>\u7684\u65b9\u6cd5\u53c2\u6570\u52a0\u4e0a@Validated<\/span>\u6ce8\u89e3 @RestController<\/span> public<\/span> class<\/span> UserController<\/span> { \n   <\/span> @Autowired<\/span> UserValidator<\/span> validator;<\/span> @InitBinder<\/span> public<\/span> void<\/span> initBinder<\/span>(<\/span>WebDataBinder<\/span> binder)<\/span> { \n   <\/span> binder.<\/span>setValidator<\/span>(<\/span>validator)<\/span>;<\/span> }<\/span> @RequestMapping<\/span>(<\/span>value =<\/span> \"\/user\/post\"<\/span>,<\/span> method =<\/span> RequestMethod<\/span>.<\/span>POST)<\/span> public<\/span> ServiceResponse<\/span> handValidatePost<\/span>(<\/span>@Validated<\/span> @RequestBody<\/span> User<\/span> user)<\/span> { \n   <\/span> ServiceResponse<\/span> serviceResponse =<\/span> new<\/span> ServiceResponse<\/span>(<\/span>)<\/span>;<\/span> serviceResponse.<\/span>setCode<\/span>(<\/span>0<\/span>)<\/span>;<\/span> serviceResponse.<\/span>setMessage<\/span>(<\/span>\"test\"<\/span>)<\/span>;<\/span> return<\/span> serviceResponse;<\/span> }<\/span> }<\/span> <\/code><\/pre>\n

\u6ce8\u89e3\u6821\u9a8c<\/h2>\n

1 \u5bf9\u4e8e\u7b80\u5355\u7c7b\u578b\u53c2\u6570\uff08\u975eBean\uff09<\/code>\uff0c\u76f4\u63a5\u5728\u53c2\u6570\u524d\uff0c\u4f7f\u7528\u6ce8\u89e3\u6dfb\u52a0\u7ea6\u675f\u89c4\u5219<\/code>\u3002\u6bd4\u5982 @NotNull @Length \u7b49
2 \u5728\u7c7b\u540d\u524d\u8ffd\u52a0 @Validated \u6ce8\u89e3\uff0c\u5426\u5219\u6dfb\u52a0\u7684\u7ea6\u675f\u89c4\u5219\u4e0d\u751f\u6548<\/code>\u3002
3 \u65b9\u6cd5\u88ab\u8c03\u7528\u65f6\uff0c\u5982\u679c\u4f20\u5165\u7684\u5b9e\u9645\u53c2\u6570\u4e0e\u7ea6\u675f\u89c4\u5219\u4e0d\u7b26<\/code>\uff0c\u4f1a\u76f4\u63a5\u629b\u51fa ConstraintViolationException \uff0c\u8868\u660e\u53c2\u6570\u6821\u9a8c\u5931\u8d25<\/code>
4 \u5bf9\u4e8eBean\u7c7b\u578b\u7684\u53c2\u6570<\/code>\uff0c\u5728Bean\u5185\u90e8\u7684\u5404\u4e2a\u5b57\u6bb5\u4e0a\u9762\u8ffd\u52a0\u7ea6\u675f\u6ce8\u89e3<\/code>\uff0c\u7136\u540e\u5728\u65b9\u6cd5\u7684\u53c2\u6570\u524d\u9762\u6dfb\u52a0 @Valid \u6ce8\u89e3<\/code>\u5373\u53ef\u3002
5 \u5bf9\u4e8eBean\u91cc\u9762\u5957Bean\u7684\uff0c\u540c\u6837\u5728\u5916\u5c42Bean\u91cc\u9762\u5199@Valid\u5373\u53ef<\/code>\u3002<\/p>\n

public<\/span> class<\/span> CreateProjectReqVO<\/span> extends<\/span> BaseVO<\/span> { \n   <\/span> \/** * \u8bf7\u6c42\u5e8f\u5217\u53f7 *\/<\/span> @NotNull<\/span>(<\/span>message =<\/span> \"\u8bf7\u6c42\u5e8f\u5217\u53f7\u4e0d\u53ef\u4e3a\u7a7a\"<\/span>)<\/span> private<\/span> Integer<\/span> requestNo;<\/span> \/** * \u9879\u76ee\u540d\u79f0 *\/<\/span> @NotNull<\/span>(<\/span>message =<\/span> \"\u9879\u76ee\u540d\u79f0\u4e0d\u53ef\u4e3a\u7a7a\"<\/span>)<\/span> private<\/span> String<\/span> projectName;<\/span> \u2026\u2026 }<\/span> <\/code><\/pre>\n
 \u6ce8\u610f \u53c2\u6570\u524d\u9762\u7684 @Valid<\/span> \u6ce8\u89e3 public<\/span> CreateProjectRespVO<\/span> createProject<\/span>(<\/span>@Valid<\/span> CreateProjectReqVO<\/span> reqVO)<\/span> { \n   <\/span> \u2026\u2026 <\/code><\/pre>\n

\u5e38\u7528\u6821\u9a8c\u6ce8\u89e3<\/h3>\n
@AssertTrue<\/span> \/<\/span> @AssertFalse<\/span> \u9a8c\u8bc1\u9002\u7528\u5b57\u6bb5\uff1aboolean<\/span> \u6ce8\u89e3\u8bf4\u660e\uff1a\u9a8c\u8bc1\u503c\u662f\u5426\u4e3atrue<\/span> \/<\/span> false<\/span> @DecimalMax<\/span> \/<\/span> @DecimalMin<\/span> \u9a8c\u8bc1\u9002\u7528\u5b57\u6bb5\uff1aBigDecimal<\/span>,<\/span>BigInteger<\/span>,<\/span>String<\/span>,<\/span>byte<\/span>,<\/span>short<\/span>,<\/span>int<\/span>,<\/span>long<\/span> \u6ce8\u89e3\u8bf4\u660e\uff1a\u9a8c\u8bc1\u503c\u662f\u5426\u5c0f\u4e8e\u6216\u8005\u7b49\u4e8e\u6307\u5b9a\u7684\u5c0f\u6570\u503c\uff0c\u8981\u6ce8\u610f\u5c0f\u6570\u5b58\u5728\u7cbe\u5ea6\u95ee\u9898 @Digits<\/span> \u9a8c\u8bc1\u9002\u7528\u5b57\u6bb5\uff1aBigDecimal<\/span>,<\/span>BigInteger<\/span>,<\/span>String<\/span>,<\/span>byte<\/span>,<\/span>short<\/span>,<\/span>int<\/span>,<\/span>long<\/span> \u6ce8\u89e3\u8bf4\u660e\uff1a\u9a8c\u8bc1\u503c\u7684\u6570\u5b57\u6784\u6210\u662f\u5426\u5408\u6cd5 \u5c5e\u6027\u8bf4\u660e\uff1ainteger:<\/span>\u6307\u5b9a\u6574\u6570\u90e8\u5206\u7684\u6570\u5b57\u7684\u4f4d\u6570\u3002fraction:<\/span> \u6307\u5b9a\u5c0f\u6570\u90e8\u5206\u7684\u6570\u5b57\u7684\u4f4d\u6570\u3002 @Future<\/span> \/<\/span> @Past<\/span> \u9a8c\u8bc1\u9002\u7528\u5b57\u6bb5\uff1aDate<\/span>,<\/span>Calendar<\/span> \u6ce8\u89e3\u8bf4\u660e\uff1a\u9a8c\u8bc1\u503c\u662f\u5426\u5728\u5f53\u524d\u65f6\u95f4\u4e4b\u540e \/<\/span> \u4e4b\u524d \u5c5e\u6027\u8bf4\u660e\uff1a\u516c\u5171 @Max<\/span> \/<\/span> @Min<\/span> \u9a8c\u8bc1\u9002\u7528\u5b57\u6bb5\uff1aBigDecimal<\/span>,<\/span>BigInteger<\/span>,<\/span>String<\/span>,<\/span>byte<\/span>,<\/span>short<\/span>,<\/span>int<\/span>,<\/span>long<\/span> \u6ce8\u89e3\u8bf4\u660e\uff1a\u9a8c\u8bc1\u503c\u662f\u5426\u5c0f\u4e8e\u6216\u8005\u7b49\u4e8e\u6307\u5b9a\u7684\u6574\u6570\u503c \u5c5e\u6027\u8bf4\u660e\uff1a\u516c\u5171 \u6ce8\u610f\u4e8b\u9879\uff1a\u5efa\u8bae\u4f7f\u7528\u5728Stirng<\/span>,<\/span>Integer<\/span>\u7c7b\u578b\uff0c\u4e0d\u5efa\u8bae\u4f7f\u7528\u5728int<\/span>\u7c7b\u578b\u4e0a\uff0c\u56e0\u4e3a\u8868\u5355\u63d0\u4ea4\u7684\u503c\u4e3a\u201c\u201d\u65f6\u65e0\u6cd5\u8f6c\u6362\u4e3aint<\/span> @NotNull<\/span> \/<\/span> @Null<\/span> \u9a8c\u8bc1\u9002\u7528\u5b57\u6bb5\uff1a\u5f15\u7528\u6570\u636e\u7c7b\u578b \u6ce8\u89e3\u8bf4\u660e\uff1a\u9a8c\u8bc1\u503c\u662f\u5426\u4e3a\u975e\u7a7a \/<\/span> \u7a7a \u5c5e\u6027\u8bf4\u660e\uff1a\u516c\u5171 @NotBlank<\/span> \u68c0\u67e5\u7ea6\u675f\u5b57\u7b26\u4e32\u662f\u4e0d\u662fNull<\/span>\u8fd8\u6709\u88abTrim<\/span>\u7684\u957f\u5ea6\u662f\u5426\u5927\u4e8e0<\/span>,<\/span>\u53ea\u5bf9\u5b57\u7b26\u4e32,<\/span>\u4e14\u4f1a\u53bb\u6389\u524d\u540e\u7a7a\u683c.<\/span> @NotEmpty<\/span> \u68c0\u67e5\u7ea6\u675f\u5143\u7d20\u662f\u5426\u4e3aNull<\/span>\u6216\u8005\u662fEMPTY.<\/span> @NotBlank<\/span> \u4e0e @NotEmpty<\/span> \u7684\u533a\u522b\uff1a\u7a7a\u683c\uff08\" \"<\/span>\uff09\u5bf9\u4e8e NotEmpty<\/span> \u662f\u5408\u6cd5\u7684\uff0c\u800c NotBlank<\/span> \u4f1a\u629b\u51fa\u6821\u9a8c\u5f02\u5e38 @Pattern<\/span> \u9a8c\u8bc1\u9002\u7528\u5b57\u6bb5\uff1aString<\/span> \u6ce8\u89e3\u8bf4\u660e\uff1a\u9a8c\u8bc1\u503c\u662f\u5426\u914d\u5907\u6b63\u5219\u8868\u8fbe\u5f0f \u5c5e\u6027\u8bf4\u660e\uff1aregexp:<\/span>\u6b63\u5219\u8868\u8fbe\u5f0fflags:<\/span> \u6307\u5b9aPattern.<\/span>Flag<\/span> \u7684\u6570\u7ec4\uff0c\u8868\u793a\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u76f8\u5173\u9009\u9879\u3002 @Size<\/span> \u9a8c\u8bc1\u9002\u7528\u5b57\u6bb5\uff1aString<\/span>,<\/span>Collection<\/span>,<\/span>Map<\/span>,<\/span>\u6570\u7ec4 \u6ce8\u89e3\u8bf4\u660e\uff1a\u9a8c\u8bc1\u503c\u662f\u5426\u6ee1\u8db3\u957f\u5ea6\u8981\u6c42 \u5c5e\u6027\u8bf4\u660e\uff1amax:<\/span>\u6307\u5b9a\u6700\u5927\u957f\u5ea6\uff0cmin:<\/span>\u6307\u5b9a\u6700\u5c0f\u957f\u5ea6\u3002 @Length<\/span>(<\/span>min=<\/span>,<\/span> max=<\/span>)<\/span>\uff1a\u4e13\u95e8\u5e94\u7528\u4e8eString<\/span>\u7c7b\u578b @Valid<\/span> \u9a8c\u8bc1\u9002\u7528\u5b57\u6bb5\uff1a\u9012\u5f52\u7684\u5bf9\u5173\u8054\u5bf9\u8c61\u8fdb\u884c\u6821\u9a8c \u6ce8\u89e3\u8bf4\u660e\uff1a\u5982\u679c\u5173\u8054\u5bf9\u8c61\u662f\u4e2a\u96c6\u5408\u6216\u8005\u6570\u7ec4,<\/span>\u90a3\u4e48\u5bf9\u5176\u4e2d\u7684\u5143\u7d20\u8fdb\u884c\u9012\u5f52\u6821\u9a8c,<\/span>\u5982\u679c\u662f\u4e00\u4e2amap,<\/span>\u5219\u5bf9\u5176\u4e2d\u7684\u503c\u90e8\u5206\u8fdb\u884c\u6821\u9a8c(<\/span>\u662f\u5426\u8fdb\u884c\u9012\u5f52\u9a8c\u8bc1)<\/span> \u5c5e\u6027\u8bf4\u660e\uff1a\u65e0 @Range<\/span>(<\/span>min=<\/span>,<\/span> max=<\/span>)<\/span> \u88ab\u6307\u5b9a\u7684\u5143\u7d20\u5fc5\u987b\u5728\u5408\u9002\u7684\u8303\u56f4\u5185 @CreditCardNumber<\/span>\u4fe1\u7528\u5361\u9a8c\u8bc1 @Email<\/span> \u9a8c\u8bc1\u662f\u5426\u662f\u90ae\u4ef6\u5730\u5740\uff0c\u5982\u679c\u4e3anull<\/span>,<\/span>\u4e0d\u8fdb\u884c\u9a8c\u8bc1\uff0c\u7b97\u901a\u8fc7\u9a8c\u8bc1\u3002 @URL<\/span>(<\/span>protocol=<\/span>,<\/span>host=<\/span>,<\/span> port=<\/span>,<\/span>regexp=<\/span>,<\/span> flags=<\/span>)<\/span> <\/code><\/pre>\n

\u6ce8\u610f<\/h3>\n

1 \u7ea6\u675f\u7528\u7684\u6ce8\u89e3\uff0c\u4e00\u822c\u9700\u8981\u5e26\u4e0amessage\u53c2\u6570<\/code>\uff0c\u65b9\u4fbf\u81ea\u5b9a\u4e49\u9519\u8bef\u4fe1\u606f<\/code>\uff0c\u6bd4\u5982\u524d\u9762\u4f8b\u5b50\u4e2d\u7684\u201c@NotNull(message = \u201c\u9879\u76ee\u540d\u79f0\u4e0d\u53ef\u4e3a\u7a7a\u201d)<\/code>\u201d\u3002\u800c\u8fd9\u4e2amessage\u53c2\u6570\u662f\u652f\u6301EL\u8868\u8fbe\u5f0f\u7684<\/strong><\/p>\n

@NotNull<\/span>(<\/span>message =<\/span> \"${member.id.null}\"<\/span>)<\/span> \u518d\u5b9a\u4e49\u4e00\u4e2a\u6bd4\u5982\u53eb\u505a messages.<\/span>properties \u7684\u914d\u7f6e\u6587\u4ef6\u6765\u7edf\u4e00\u7ba1\u7406\u9519\u8bef\u4fe1\u606f member.<\/span>id.<\/span>null<\/span>=<\/span>\u7528\u6237\u7f16\u53f7\u4e0d\u80fd\u4e3a\u7a7a <\/code><\/pre>\n

2 \u7ea6\u675f\u89c4\u5219\u652f\u6301\u6b63\u5219\u8868\u8fbe\u5f0f<\/strong>
3 \u6ce8\u89e3\u4e0d\u80fd\uff08\u53ea\uff09\u653e\u5728\u5b9e\u73b0\u7c7b\u4e0a<\/strong><\/p>\n

public<\/span> interface<\/span> IProjectService<\/span> { \n   <\/span> \/** * \u9879\u76ee\u521b\u5efa *\/<\/span> CreateProjectRespVO<\/span> createProject<\/span>(<\/span>CreateProjectReqVO<\/span> reqVO)<\/span>;<\/span> }<\/span> @Service<\/span> public<\/span> class<\/span> ProjectServiceImpl<\/span> implements<\/span> IProjectService<\/span> { \n   <\/span> \/** * \u9879\u76ee\u521b\u5efa *\/<\/span> @Override<\/span> public<\/span> CreateProjectRespVO<\/span> createProject<\/span>(<\/span>@Valid<\/span> CreateProjectReqVO<\/span> reqVO)<\/span> { \n   <\/span> \u2026\u2026 }<\/span> <\/code><\/pre>\n

\u5728\u8fdb\u884c\u6821\u9a8c\u65f6\u4f1a\u53d1\u751f\u201cjavax.validation.ConstraintDeclarationException\u201d\u5f02\u5e38\uff08\u6ce8\u610f\u8ddf\u6821\u9a8c\u4e0d\u901a\u8fc7\u53d1\u751f\u7684\u5f02\u5e38\u4e0d\u662f\u4e00\u4e2a\uff09<\/p>\n

\u89e3\u51b3\u65b9\u6cd5\uff1a<\/strong><\/p>\n

@Override\u7236\u7c7b\/\u63a5\u53e3\u7684\u65b9\u6cd5\uff0c\u5165\u53c2\u7ea6\u675f\u53ea\u80fd\u5199\u5728\u7236\u7c7b\/\u63a5\u53e3\u4e0a\u9762\u3002 \u6216\u8005\u4e24\u8fb9\u90fd\u5199\u4e0a\u4e5f\u53ef\uff08\u4f46\u662f\u8fd9\u6837\u7ef4\u62a4\u65f6\u5bb9\u6613\u51fa\u95ee\u9898\uff0c\u4e0d\u63a8\u8350\uff09\u3002 \u53e6\u5916 @Validated \u8fd9\u4e2a\u6ce8\u89e3\u5199\u5728\u54ea\u8fb9\u90fd\u53ef\u4ee5\u3002 <\/code><\/pre>\n

\u6548\u9a8c\u5b9e\u9a8c<\/h2>\n

\u4f7f\u7528spring validation\u5b8c\u6210\u6570\u636e\u540e\u7aef\u6821\u9a8c<\/p>\n

public<\/span> class<\/span> Foo<\/span> { \n   <\/span> @NotBlank<\/span> private<\/span> String<\/span> name;<\/span> @Min<\/span>(<\/span>18<\/span>)<\/span> private<\/span> Integer<\/span> age;<\/span> @Pattern<\/span>(<\/span>regexp =<\/span> \"^1(3|4|5|7|8)\\\\d{9}$\"<\/span>,<\/span>message =<\/span> \"\u624b\u673a\u53f7\u7801\u683c\u5f0f\u9519\u8bef\"<\/span>)<\/span> @NotBlank<\/span>(<\/span>message =<\/span> \"\u624b\u673a\u53f7\u7801\u4e0d\u80fd\u4e3a\u7a7a\"<\/span>)<\/span> private<\/span> String<\/span> phone;<\/span> @Email<\/span>(<\/span>message =<\/span> \"\u90ae\u7bb1\u683c\u5f0f\u9519\u8bef\"<\/span>)<\/span> private<\/span> String<\/span> email;<\/span> \/\/... getter setter<\/span> }<\/span> <\/code><\/pre>\n
@Controller<\/span> public<\/span> class<\/span> FooController<\/span> { \n   <\/span> @RequestMapping<\/span>(<\/span>\"\/foo\"<\/span>)<\/span> public<\/span> String<\/span> foo<\/span>(<\/span>@Validated<\/span> Foo<\/span> foo <<\/span>1><\/span><\/span>,<\/span> BindingResult<\/span> bindingResult <<\/span>2><\/span><\/span>)<\/span> { \n   <\/span> if<\/span>(<\/span>bindingResult.<\/span>hasErrors<\/span>(<\/span>)<\/span>)<\/span>{ \n   <\/span> for<\/span> (<\/span>FieldError<\/span> fieldError :<\/span> bindingResult.<\/span>getFieldErrors<\/span>(<\/span>)<\/span>)<\/span> { \n   <\/span> \/\/...<\/span> }<\/span> return<\/span> \"fail\"<\/span>;<\/span> }<\/span> return<\/span> \"success\"<\/span>;<\/span> }<\/span> }<\/span> <\/code><\/pre>\n
<<\/span>1><\/span><\/span> \u53c2\u6570Foo<\/span>\u524d\u9700\u8981\u52a0\u4e0a @Validated<\/span> \u6ce8\u89e3\uff0c\u8868\u660e\u9700\u8981spring\u5bf9\u5176\u8fdb\u884c\u6821\u9a8c\uff0c\u800c\u6821\u9a8c\u7684\u4fe1\u606f\u4f1a\u5b58\u653e\u5230\u5176\u540e\u7684BindingResult<\/span>\u4e2d\u3002 \u6ce8\u610f\uff0c\u5fc5\u987b\u76f8\u90bb\uff0c\u5982\u679c\u6709\u591a\u4e2a\u53c2\u6570\u9700\u8981\u6821\u9a8c\uff0c\u5f62\u5f0f\u53ef\u4ee5\u5982\u4e0b foo<\/span>(<\/span>@Validated<\/span> Foo<\/span> foo,<\/span> BindingResult<\/span> fooBindingResult \uff0c@Validated<\/span> Bar<\/span> bar,<\/span> BindingResult<\/span> barBindingResult)<\/span>;<\/span> \u5373\u4e00\u4e2a\u6821\u9a8c\u7c7b\u5bf9\u5e94\u4e00\u4e2a\u6821\u9a8c\u7ed3\u679c\u3002 <<\/span>2><\/span><\/span> \u6821\u9a8c\u7ed3\u679c\u4f1a\u88ab\u81ea\u52a8\u586b\u5145\uff0c\u5728controller\u4e2d\u53ef\u4ee5\u6839\u636e\u4e1a\u52a1\u903b\u8f91\u6765\u51b3\u5b9a\u5177\u4f53\u7684\u64cd\u4f5c\uff0c\u5982\u8df3\u8f6c\u5230\u9519\u8bef\u9875\u9762\u3002 <\/code><\/pre>\n

\u8bbf\u95ee http:\/\/localhost:8080\/foo?name=xujingfeng&email=000&age=19 <\/code>\u53ef\u4ee5\u5f97\u5230\u5982\u4e0b\u7684debug\u4fe1\u606f\uff1a
\"java\u53c2\u6570\u6821\u9a8c_\u6b63\u5219\u8868\u8fbe\u5f0f\u5728\u7ebf\u6d4b\u8bd5\u5de5\u5177<\/p>\n

\u5f53\u53d1\u751f\u591a\u4e2a\u9519\u8bef\uff0cspring validation\u4e0d\u4f1a\u5728\u7b2c\u4e00\u4e2a\u9519\u8bef\u53d1\u751f\u540e\u7acb\u5373\u505c\u6b62\uff0c\u800c\u662f\u7ee7\u7eed\u8bd5\u9519\uff0c\u544a\u8bc9\u6211\u4eec\u6240\u6709\u7684\u9519\u8bef <\/code><\/pre>\n

\u5206\u7ec4\u6548\u9a8c\u521d\u6b65- Not\u6ce8\u89e3 \u4e2d groups \u5c5e\u6027<\/h3>\n

Not\u6ce8\u89e3<\/p>\n

\u4f5c\u7528<\/h4>\n

groups<\/code>\u662f\u9a8c\u8bc1\u5206\u7ec4<\/code>\uff0c\u6bd4\u5982\u6211\u6709\u7684\u9a8c\u8bc1\u53ea\u6709\u66f4\u65b0\u7684\u65f6\u5019\u505a<\/code>\uff0c\u6709\u7684\u53ea\u6709\u6dfb\u52a0\u7684\u65f6\u5019\u505a<\/code>\uff0c\u5c31\u7528\u8fd9\u4e2a<\/p>\n

javax.validate<\/code>\u91cc\u6709\u63d0\u4f9b\u4e00\u4e2a\u9ed8\u8ba4\u5206\u7ec4Default.class\u662f\u4e2a\u63a5\u53e3<\/code>\uff0c\u4e0d\u6307\u5b9a\u5206\u7ec4\u65f6\u90fd\u4f1a\u6267\u884c\u8fd9\u4e2a<\/code><\/p>\n

\u5206\u7ec4 class<\/code> \u662f\u4e2a\u7a7a\u7684\u63a5\u53e3<\/code><\/p>\n

\u5728\u5b57\u6bb5\u4e0a\u9762\u6307\u5b9a\u4ed6\u4eec\u7684\u5206\u7ec4<\/code><\/p>\n

@NotNull<\/span>(<\/span>message =<\/span> \"primary is not null\"<\/span>,<\/span>groups =<\/span> { \n   <\/span>GroupInterface1<\/span>.<\/span>class<\/span>}<\/span>)<\/span> private<\/span> Long<\/span> id;<\/span> public<\/span> @interface<\/span> GroupInterface1<\/span>(<\/span>)<\/span>{ \n   <\/span>}<\/span> <\/code><\/pre>\n

\u6b64\u65f6controller\u5e94\u8be5\u8981\u52a0\u4e0a@Valid<\/code> , \u5426\u5219\u4e0d\u4f1a\u9a8c\u8bc1<\/code><\/p>\n

@RequestMapping<\/span>(<\/span>value =<\/span> \"\/test\"<\/span>)<\/span> public<\/span> void<\/span> test<\/span>(<\/span>@Validated<\/span>(<\/span>GroupInterface1<\/span>.<\/span>class<\/span>)<\/span> User<\/span> user)<\/span> { \n   <\/span> }<\/span> <\/code><\/pre>\n

\u57fa\u672c\u5199\u6cd5<\/h4>\n
Class<\/span> Foo<\/span>{ \n   <\/span> \/\/\u53ea\u6709\u5728Adult\u5206\u7ec4\u4e0b\uff0c18\u5c81\u7684\u9650\u5236\u624d\u4f1a\u8d77\u4f5c\u7528<\/span> @Min<\/span>(<\/span>value =<\/span> 18<\/span>,<\/span>groups =<\/span> { \n   <\/span>Adult<\/span>.<\/span>class<\/span>}<\/span>)<\/span> private<\/span> Integer<\/span> age;<\/span> public<\/span> interface<\/span> Adult<\/span>{ \n   <\/span>}<\/span> public<\/span> interface<\/span> Minor<\/span>{ \n   <\/span>}<\/span> }<\/span> <\/code><\/pre>\n
@RequestMapping<\/span>(<\/span>\"\/drink\"<\/span>)<\/span> public<\/span> String<\/span> drink<\/span>(<\/span>@Validated<\/span>(<\/span>{ \n   <\/span>Foo.<\/span>Adult<\/span>.<\/span>class<\/span>}<\/span>)<\/span> Foo<\/span> foo,<\/span> BindingResult<\/span> bindingResult)<\/span> { \n   <\/span> if<\/span>(<\/span>bindingResult.<\/span>hasErrors<\/span>(<\/span>)<\/span>)<\/span>{ \n   <\/span> for<\/span> (<\/span>FieldError<\/span> fieldError :<\/span> bindingResult.<\/span>getFieldErrors<\/span>(<\/span>)<\/span>)<\/span> { \n   <\/span> \/\/...<\/span> }<\/span> return<\/span> \"fail\"<\/span>;<\/span> }<\/span> return<\/span> \"success\"<\/span>;<\/span> }<\/span> @RequestMapping<\/span>(<\/span>\"\/live\"<\/span>)<\/span> public<\/span> String<\/span> live<\/span>(<\/span>@Validated<\/span> Foo<\/span> foo,<\/span> BindingResult<\/span> bindingResult)<\/span> { \n   <\/span> if<\/span>(<\/span>bindingResult.<\/span>hasErrors<\/span>(<\/span>)<\/span>)<\/span>{ \n   <\/span> for<\/span> (<\/span>FieldError<\/span> fieldError :<\/span> bindingResult.<\/span>getFieldErrors<\/span>(<\/span>)<\/span>)<\/span> { \n   <\/span> \/\/...<\/span> }<\/span> return<\/span> \"fail\"<\/span>;<\/span> }<\/span> return<\/span> \"success\"<\/span>;<\/span> }<\/span> <\/code><\/pre>\n

drink\u65b9\u6cd5<\/code>\u9650\u5b9a\u9700\u8981\u8fdb\u884cAdult\u6821\u9a8c<\/code>\uff0c\u800clive\u65b9\u6cd5\u5219\u4e0d\u505a\u9650\u5236<\/code><\/p>\n

\u4f7f\u7528JSR\u63d0\u4f9b\u7684@GroupSequence\u6ce8\u89e3\u63a7\u5236\u6821\u9a8c\u987a\u5e8f<\/h4>\n

\u4e0a\u9762\u662fHibernate Validation<\/code>\u63d0\u4f9b\u7684\u80fd\u529b\uff0c\u800c\u4e0d\u662fJSR\u6807\u51c6\u63d0\u4f9b<\/code>\u7684\u3002@GroupSequence<\/code> \u5b83\u662fJSR\u6807\u51c6\u63d0\u4f9b\u7684<\/code>\u6ce8\u89e3\uff08\u53ea\u662f\u6ca1\u6709provider\u5f3a\u5927\u800c\u5df2\uff0c\u4f46\u4e5f\u6709\u5f88\u9002\u5408\u5b83\u7684\u4f7f\u7528\u573a\u666f\uff09<\/p>\n

\/\/ Defines group sequence. \u5b9a\u4e49\u7ec4\u5e8f\u5217\uff08\u5e8f\u5217\uff1a\u987a\u5e8f\u6267\u884c\u7684\uff09<\/span> @Target<\/span>(<\/span>{ \n   <\/span> TYPE }<\/span>)<\/span> @Retention<\/span>(<\/span>RUNTIME)<\/span> @Documented<\/span> public<\/span> @interface<\/span> GroupSequence<\/span> { \n   <\/span> Class<\/span><<\/span>?<\/span>><\/span><\/span>[<\/span>]<\/span> value<\/span>(<\/span>)<\/span>;<\/span> }<\/span> <\/code><\/pre>\n

\u987e\u540d\u601d\u4e49\uff0c\u5b83\u8868\u793aGroup\u7ec4\u5e8f\u5217<\/code>\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u4e0d\u540c\u7ec4\u522b\u7684\u7ea6\u675f\u9a8c\u8bc1\u662f\u65e0\u5e8f\u7684<\/code>\uff0c\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u7ea6\u675f\u9a8c\u8bc1\u7684\u987a\u5e8f\u662f\u975e\u5e38\u7684\u91cd\u8981\u7684\uff0c\u6bd4\u5982\u5982\u4e0b\u4e24\u4e2a\u573a\u666f\uff1a<\/p>\n

    \n
  • \u7b2c\u4e8c\u4e2a\u7ec4\u7684\u7ea6\u675f<\/code>\u9a8c\u8bc1\u4f9d\u8d56\u4e8e<\/code> \u7b2c\u4e00\u4e2a\u7ea6\u675f\u6267\u884c\u5b8c\u6210\u7684\u7ed3\u679c<\/code>\uff08\u5fc5\u987b\u7b2c\u4e00\u4e2a\u7ea6\u675f\u6b63\u786e\u4e86\uff0c\u7b2c\u4e8c\u4e2a\u7ea6\u675f\u6267\u884c\u624d\u6709\u610f\u4e49<\/code>\uff09<\/li>\n
  • \u67d0\u4e2aGroup\u7ec4\u7684\u6821\u9a8c\u975e\u5e38\u8017\u65f6<\/code>\uff0c\u5e76\u4e14\u4f1a\u6d88\u8017\u6bd4\u8f83\u5927\u7684CPU\/\u5185\u5b58\u3002\u90a3\u4e48\u6211\u4eec\u7684\u505a\u6cd5\u5e94\u8be5\u662f\u628a\u8fd9\u79cd\u6821\u9a8c\u653e\u5230\u6700\u540e<\/code>\uff0c\u6240\u4ee5\u5bf9\u987a\u5e8f\u63d0\u51fa\u4e86\u8981\u6c42<\/li>\n<\/ul>\n

    \u4e00\u4e2a\u7ec4\u53ef\u4ee5\u5b9a\u4e49\u4e3a\u5176\u4ed6\u7ec4\u7684\u5e8f\u5217<\/code>\uff0c\u4f7f\u7528\u5b83\u8fdb\u884c\u9a8c\u8bc1\u7684\u65f6\u5019\u5fc5\u987b\u7b26\u5408\u8be5\u5e8f\u5217\u89c4\u5b9a\u7684\u987a\u5e8f<\/code>\u3002\u5728\u4f7f\u7528\u7ec4\u5e8f\u5217\u9a8c\u8bc1\u7684\u65f6\u5019\uff0c\u5982\u679c\u5e8f\u5217\u524d\u8fb9\u7684\u7ec4\u9a8c\u8bc1\u5931\u8d25<\/code>\uff0c\u5219\u540e\u9762\u7684\u7ec4\u5c06\u4e0d\u518d\u7ed9\u4e88\u9a8c\u8bc1<\/code>\u3002<\/p>\n

    public<\/span> class<\/span> User<\/span> { \n   <\/span> @NotEmpty<\/span>(<\/span>message =<\/span> \"firstname may be empty\"<\/span>)<\/span> private<\/span> String<\/span> firstname;<\/span> @NotEmpty<\/span>(<\/span>message =<\/span> \"middlename may be empty\"<\/span>,<\/span> groups =<\/span> Default<\/span>.<\/span>class<\/span>)<\/span> private<\/span> String<\/span> middlename;<\/span> @NotEmpty<\/span>(<\/span>message =<\/span> \"lastname may be empty\"<\/span>,<\/span> groups =<\/span> GroupA<\/span>.<\/span>class<\/span>)<\/span> private<\/span> String<\/span> lastname;<\/span> @NotEmpty<\/span>(<\/span>message =<\/span> \"country may be empty\"<\/span>,<\/span> groups =<\/span> GroupB<\/span>.<\/span>class<\/span>)<\/span> private<\/span> String<\/span> country;<\/span> public<\/span> interface<\/span> GroupA<\/span> { \n   <\/span> }<\/span> public<\/span> interface<\/span> GroupB<\/span> { \n   <\/span> }<\/span> \/\/ \u7ec4\u5e8f\u5217<\/span> @GroupSequence<\/span>(<\/span>{ \n   <\/span>Default<\/span>.<\/span>class<\/span>,<\/span> GroupA<\/span>.<\/span>class<\/span>,<\/span> GroupB<\/span>.<\/span>class<\/span>}<\/span>)<\/span> public<\/span> interface<\/span> Group<\/span> { \n   <\/span> }<\/span> }<\/span> <\/code><\/pre>\n

    \u6d4b\u8bd5\u4e00<\/p>\n

    public<\/span> static<\/span> void<\/span> main<\/span>(<\/span>String<\/span>[<\/span>]<\/span> args)<\/span> { \n   <\/span> User<\/span> user =<\/span> new<\/span> User<\/span>(<\/span>)<\/span>;<\/span> \/\/ \u6b64\u5904\u6307\u5b9a\u4e86\u6821\u9a8c\u7ec4\u662f\uff1aUser.Group.class<\/span> Set<\/span><<\/span>ConstraintViolation<\/span><<\/span>User<\/span>><\/span>><\/span><\/span> result =<\/span> Validation<\/span>.<\/span>buildDefaultValidatorFactory<\/span>(<\/span>)<\/span>.<\/span>getValidator<\/span>(<\/span>)<\/span>.<\/span>validate<\/span>(<\/span>user,<\/span> User.<\/span>Group<\/span>.<\/span>class<\/span>)<\/span>;<\/span> \/\/ \u5bf9\u7ed3\u679c\u8fdb\u884c\u904d\u5386\u8f93\u51fa<\/span> result.<\/span>stream<\/span>(<\/span>)<\/span>.<\/span>map<\/span>(<\/span>v -><\/span> v.<\/span>getPropertyPath<\/span>(<\/span>)<\/span> +<\/span> \" \"<\/span> +<\/span> v.<\/span>getMessage<\/span>(<\/span>)<\/span> +<\/span> \": \"<\/span> +<\/span> v.<\/span>getInvalidValue<\/span>(<\/span>)<\/span>)<\/span>.<\/span>forEach<\/span>(<\/span>System<\/span>.<\/span>out::<\/span>println<\/span>)<\/span>;<\/span> }<\/span> <\/code><\/pre>\n
    middlename middlename may be empty:<\/span> null<\/span> firstname firstname may be empty:<\/span> null<\/span> \u53ea\u6709Default<\/span>\u8fd9\u4e2aGroup<\/span>\u7684\u6821\u9a8c\u4e86\uff0c\u5e8f\u5217\u4e0a\u5176\u5b83\u7ec4\u5e76\u6ca1\u6709\u6267\u884c\u6821\u9a8c <\/code><\/pre>\n

    \u6d4b\u8bd5\u4e8c<\/p>\n

     User<\/span> user =<\/span> new<\/span> User<\/span>(<\/span>)<\/span>;<\/span> user.<\/span>setFirstname<\/span>(<\/span>\"f\"<\/span>)<\/span>;<\/span> user.<\/span>setMiddlename<\/span>(<\/span>\"s\"<\/span>)<\/span>;<\/span> <\/code><\/pre>\n
    lastname lastname may be empty:<\/span> null<\/span> Default<\/span>\u7ec4\u90fd\u6821\u9a8c\u901a\u8fc7\u540e\uff0c\u6267\u884c\u4e86GroupA<\/span>\u7ec4\u7684\u6821\u9a8c\u3002 \u4f46GroupA<\/span>\u7ec4\u6821\u9a8c\u6728\u6709\u901a\u8fc7\uff0cGroupB<\/span>\u7ec4\u7684\u6821\u9a8c\u4e5f\u5c31\u4e0d\u6267\u884c\u4e86 @GroupSequence<\/span>\u63d0\u4f9b\u7684\u7ec4\u5e8f\u5217\u987a\u5e8f\u6267\u884c\u4ee5\u53ca\u77ed\u8def\u80fd\u529b\uff0c\u5728\u5f88\u591a\u573a\u666f\u4e0b\u662f\u975e\u5e38\u975e\u5e38\u597d\u7528\u7684 <\/code><\/pre>\n

    \u6ce8\u610f<\/strong>
    \u987a\u5e8f<\/code>\u53ea\u80fd\u63a7\u5236\u5728\u5206\u7ec4\u7ea7\u522b<\/code>\uff0c\u65e0\u6cd5\u63a7\u5236<\/code>\u5728\u7ea6\u675f\u6ce8\u89e3\u7ea7\u522b<\/code>\u3002\u56e0\u4e3a\u4e00\u4e2a\u7c7b\u5185\u7684\u7ea6\u675f<\/code>\uff08\u540c\u4e00\u5206\u7ec4\u5185\uff09\uff0c\u5b83\u7684\u987a\u5e8f\u662f<Set> metaConstraints\u6765\u4fdd\u8bc1<\/code>\u7684\uff0c\u6240\u4ee5\u53ef\u4ee5\u8ba4\u4e3a\u540c\u4e00\u5206\u7ec4\u5185\u7684\u6821\u9a8c\u5668<\/code>\u662f\u6728\u6709\u6267\u884c\u7684\u5148\u540e\u987a\u5e8f\u7684<\/code>\uff08\u4e0d\u7ba1\u662f\u7c7b\u3001\u5c5e\u6027\u3001\u65b9\u6cd5\u3001\u6784\u9020\u5668\u2026\uff09<\/p>\n

    \u6821\u9a8c\u5206\u7ec4\u5904\u7406\u5668 @DefaultGroupSequenceProvider<\/h4>\n

    DefaultGroupSequenceProvider<\/code>\u7528\u6765\u505a\u591a\u5b57\u6bb5\u8054\u5408\u6821\u9a8c<\/code><\/p>\n

    \u67d0\u4e2a\u5609\u5e74\u534e\u4ff1\u4e50\u90e8\u95e8\u7968\u5bf9\u4e0d\u540c\u4eba\u7fa4\u5b58\u5728\u4e0d\u540c\u7684\u5b9a\u4ef7<\/strong><\/p>\n

    \u5e74\u9f84\u5fc5\u987b\u5927\u4e8e 12<\/span> \u5c81\uff1b 12<\/span> ~<\/span> 18<\/span> \u5c81\u534a\u4ef7\uff1b 18<\/span> \u5c81\u4ee5\u4e0a\u5168\u4ef7\uff1b coser \u5219\u53ef\u4ee5\u514d\u8d39\uff1b <\/code><\/pre>\n

    \u6362\u53e5\u8bdd\u6765\u8bf4\uff0c\u4efb\u4f55\u5e74\u9f84\u5927\u4e8e 12 \u5c81\u7684\u4eba\u90fd\u53ef\u4ee5\u8d2d\u4e70\u5168\u4ef7\uff0c\u5bf9\u4e8e\u534a\u4ef7\u7968\u9700\u8981\u6821\u9a8c\u5e74\u9f84\u662f\u5426\u6ee1\u8db3\u672a\u6210\u5e74\uff0c\u5bf9\u4e8e\u514d\u8d39\u7968\u9700\u8981\u6821\u9a8c\u662f\u5426\u662f coser\u3002<\/p>\n

    @Getter<\/span> @Setter<\/span> @GroupSequenceProvider<\/span>(<\/span>Person.<\/span>FareTypeGroupSequenceProvider<\/span>.<\/span>class<\/span>)<\/span> public<\/span> class<\/span> Person<\/span> { \n   <\/span> \/** * \u5e74\u9f84 *\/<\/span> @Min<\/span>(<\/span>value =<\/span> 12<\/span>,<\/span> message =<\/span> \"\u5e74\u9f84\u6700\u5c0f\u4e0d\u4f4e\u4e8e 12 \u5c81\"<\/span>)<\/span> @Max<\/span>(<\/span>value =<\/span> 18<\/span>,<\/span> message =<\/span> \"\u5e74\u9f84\u5927\u4e8e 18 \u5c81\u9700\u8981\u8d2d\u4e70\u5168\u4ef7\u95e8\u7968\"<\/span>,<\/span> groups=<\/span>WhenIsHalf<\/span>.<\/span>class<\/span>)<\/span> private<\/span> Integer<\/span> age;<\/span> \/** * \u7968\u4ef7[0=\u514d\u8d39,1=\u534a\u4ef7,2=\u5168\u4ef7] *\/<\/span> @NotNull<\/span>(<\/span>message =<\/span> \"\u7968\u4ef7\u7c7b\u578b\u4e0d\u80fd\u4e3a\u7a7a\"<\/span>)<\/span> private<\/span> Integer<\/span> fareType;<\/span> \/** * \u662f\u5426\u662f coser *\/<\/span> @AssertTrue<\/span>(<\/span>message =<\/span> \"\u53ea\u6709 coser \u624d\u53ef\u4ee5\u514d\u8d39\u8fdb\u53bb\"<\/span>,<\/span> groups =<\/span> WhenIsFree<\/span>.<\/span>class<\/span>)<\/span> private<\/span> Boolean<\/span> coser;<\/span> public<\/span> interface<\/span> WhenIsFree<\/span> { \n   <\/span>}<\/span> public<\/span> interface<\/span> WhenIsHalf<\/span> { \n   <\/span>}<\/span> \/** * \u6821\u9a8c\u5206\u7ec4\u5904\u7406\u5668 *\/<\/span> public<\/span> static<\/span> class<\/span> FareTypeGroupSequenceProvider<\/span> implements<\/span> DefaultGroupSequenceProvider<\/span><<\/span>Person<\/span>><\/span><\/span> { \n   <\/span> @Override<\/span> public<\/span> List<\/span><<\/span>Class<\/span><<\/span>?<\/span>><\/span>><\/span><\/span> getValidationGroups<\/span>(<\/span>Person<\/span> person)<\/span> { \n   <\/span> ArrayList<\/span><<\/span>Class<\/span><<\/span>?<\/span>><\/span>><\/span><\/span> list =<\/span> Lists<\/span>.<\/span>newArrayList<\/span>(<\/span>)<\/span>;<\/span> list.<\/span>add<\/span>(<\/span>Person<\/span>.<\/span>class<\/span>)<\/span>;<\/span> \/\/ \u5224\u7a7a<\/span> if<\/span> (<\/span>Objects<\/span>.<\/span>nonNull<\/span>(<\/span>person)<\/span>)<\/span> { \n   <\/span> \/\/ \u5f53 fareType = 0 \u65f6<\/span> if<\/span> (<\/span>Objects<\/span>.<\/span>equals<\/span>(<\/span>person.<\/span>getFareType<\/span>(<\/span>)<\/span>,<\/span> 0<\/span>)<\/span>)<\/span> { \n   <\/span> list.<\/span>add<\/span>(<\/span>WhenIsFree<\/span>.<\/span>class<\/span>)<\/span>;<\/span> }<\/span> else<\/span> (<\/span>Objects<\/span>.<\/span>equals<\/span>(<\/span>person.<\/span>getFareType<\/span>(<\/span>)<\/span>,<\/span> 1<\/span>)<\/span>)<\/span> { \n   <\/span> list.<\/span>add<\/span>(<\/span>WhenIsHalf<\/span>.<\/span>class<\/span>)<\/span>;<\/span> }<\/span> }<\/span> return<\/span> list;<\/span> }<\/span> }<\/span> }<\/span> <\/code><\/pre>\n

    \u5206\u5f00\u5199<\/p>\n

    ```java @Getter<\/span> @Setter<\/span> @GroupSequenceProvider<\/span>(<\/span>FareTypeGroupSequenceProvider<\/span>.<\/span>class<\/span>)<\/span> public<\/span> class<\/span> Person<\/span> { \n   <\/span> \/** * \u5e74\u9f84 *\/<\/span> @Min<\/span>(<\/span>value =<\/span> 12<\/span>,<\/span> message =<\/span> \"\u5e74\u9f84\u6700\u5c0f\u4e0d\u4f4e\u4e8e 12 \u5c81\"<\/span>)<\/span> @Max<\/span>(<\/span>value =<\/span> 18<\/span>,<\/span> message =<\/span> \"\u5e74\u9f84\u5927\u4e8e 18 \u5c81\u9700\u8981\u8d2d\u4e70\u5168\u4ef7\u95e8\u7968\"<\/span>,<\/span> groups=<\/span>WhenIsHalf<\/span>.<\/span>class<\/span>)<\/span> private<\/span> Integer<\/span> age;<\/span> \/** * \u7968\u4ef7[0=\u514d\u8d39,1=\u534a\u4ef7,2=\u5168\u4ef7] *\/<\/span> @NotNull<\/span>(<\/span>message =<\/span> \"\u7968\u4ef7\u7c7b\u578b\u4e0d\u80fd\u4e3a\u7a7a\"<\/span>)<\/span> private<\/span> Integer<\/span> fareType;<\/span> \/** * \u662f\u5426\u662f coser *\/<\/span> @AssertTrue<\/span>(<\/span>message =<\/span> \"\u53ea\u6709 coser \u624d\u53ef\u4ee5\u514d\u8d39\u8fdb\u53bb\"<\/span>,<\/span> groups =<\/span> WhenIsFree<\/span>.<\/span>class<\/span>)<\/span> private<\/span> Boolean<\/span> coser;<\/span> public<\/span> interface<\/span> WhenIsFree<\/span> { \n   <\/span>}<\/span> public<\/span> interface<\/span> WhenIsHalf<\/span> { \n   <\/span>}<\/span> }<\/span> <\/code><\/pre>\n
    \/** * \u6821\u9a8c\u5206\u7ec4\u5904\u7406\u5668 *\/<\/span> public<\/span> class<\/span> FareTypeGroupSequenceProvider<\/span> implements<\/span> DefaultGroupSequenceProvider<\/span><<\/span>Person<\/span>><\/span><\/span> { \n   <\/span> @Override<\/span> public<\/span> List<\/span><<\/span>Class<\/span><<\/span>?<\/span>><\/span>><\/span><\/span> getValidationGroups<\/span>(<\/span>Person<\/span> person)<\/span> { \n   <\/span> ArrayList<\/span><<\/span>Class<\/span><<\/span>?<\/span>><\/span>><\/span><\/span> list =<\/span> Lists<\/span>.<\/span>newArrayList<\/span>(<\/span>)<\/span>;<\/span> list.<\/span>add<\/span>(<\/span>Person<\/span>.<\/span>class<\/span>)<\/span>;<\/span> \/\/ \u5224\u7a7a<\/span> if<\/span> (<\/span>Objects<\/span>.<\/span>nonNull<\/span>(<\/span>person)<\/span>)<\/span> { \n   <\/span> \/\/ \u5f53 fareType = 0 \u65f6<\/span> if<\/span> (<\/span>Objects<\/span>.<\/span>equals<\/span>(<\/span>person.<\/span>getFareType<\/span>(<\/span>)<\/span>,<\/span> 0<\/span>)<\/span>)<\/span> { \n   <\/span> list.<\/span>add<\/span>(<\/span>WhenIsFree<\/span>.<\/span>class<\/span>)<\/span>;<\/span> }<\/span> else<\/span> (<\/span>Objects<\/span>.<\/span>equals<\/span>(<\/span>person.<\/span>getFareType<\/span>(<\/span>)<\/span>,<\/span> 1<\/span>)<\/span>)<\/span> { \n   <\/span> list.<\/span>add<\/span>(<\/span>WhenIsHalf<\/span>.<\/span>class<\/span>)<\/span>;<\/span> }<\/span> }<\/span> return<\/span> list;<\/span> }<\/span> }<\/span> <\/code><\/pre>\n

    \u6ce8\u610f<\/strong><\/p>\n

    DefaultGroupSequenceProvider<\/span> \u7c7b\u578b\u662f\u6cdb\u578b\u7c7b\uff0c\u5fc5\u987b\u6307\u5b9a\u4e3a\u9700\u8981\u53c2\u6570\u6821\u9a8c\u7684 Bean<\/span> \u7c7b\u578b\uff1b DefaultGroupSequenceProvider<\/span>.<\/span>getValidationGroups \u65b9\u6cd5\u8fd4\u56de\u7684 list \u4e2d\u5fc5\u987b\u5305\u542b Bean<\/span> \u7c7b\u578b;<\/span> DefaultGroupSequenceProvider<\/span> \u53ea\u80fd\u589e\u5f3a Default<\/span>.<\/span>class<\/span> \u5206\u7ec4;<\/span> <\/code><\/pre>\n

    \u81ea\u5b9a\u4e49\u6548\u9a8c<\/h3>\n

    \u6dfb\u52a0\u4e00\u4e2a\u201c\u5b57\u7b26\u4e32\u4e0d\u80fd\u5305\u542b\u7a7a\u683c\u201d\u7684\u9650\u5236<\/p>\n

    @Target<\/span>(<\/span>{ \n   <\/span>METHOD,<\/span> FIELD,<\/span> ANNOTATION_TYPE,<\/span> CONSTRUCTOR,<\/span> PARAMETER}<\/span>)<\/span> @Retention<\/span>(<\/span>RUNTIME)<\/span> @Documented<\/span> @Constraint<\/span>(<\/span>validatedBy =<\/span> { \n   <\/span>CannotHaveBlankValidator<\/span>.<\/span>class<\/span>}<\/span>)<\/span><<\/span>1><\/span><\/span> public<\/span> @interface<\/span> CannotHaveBlank<\/span> { \n   <\/span> \/\/\u9ed8\u8ba4\u9519\u8bef\u6d88\u606f<\/span> String<\/span> message<\/span>(<\/span>)<\/span> default<\/span> \"\u4e0d\u80fd\u5305\u542b\u7a7a\u683c\"<\/span>;<\/span> \/\/\u5206\u7ec4<\/span> Class<\/span><<\/span>?<\/span>><\/span><\/span>[<\/span>]<\/span> groups<\/span>(<\/span>)<\/span> default<\/span> { \n   <\/span>}<\/span>;<\/span> \/\/\u8d1f\u8f7d<\/span> Class<\/span><<\/span>?<\/span> extends<\/span> Payload<\/span>><\/span><\/span>[<\/span>]<\/span> payload<\/span>(<\/span>)<\/span> default<\/span> { \n   <\/span>}<\/span>;<\/span> \/\/\u6307\u5b9a\u591a\u4e2a\u65f6\u4f7f\u7528<\/span> @Target<\/span>(<\/span>{ \n   <\/span>FIELD,<\/span> METHOD,<\/span> PARAMETER,<\/span> ANNOTATION_TYPE}<\/span>)<\/span> @Retention<\/span>(<\/span>RUNTIME)<\/span> @Documented<\/span> @interface<\/span> List<\/span> { \n   <\/span> CannotHaveBlank<\/span>[<\/span>]<\/span> value<\/span>(<\/span>)<\/span>;<\/span> }<\/span> }<\/span> <\/code><\/pre>\n

    \u6211\u4eec\u4e0d\u9700\u8981\u5173\u6ce8\u592a\u591a\u4e1c\u897f\uff0c\u4f7f\u7528spring validation\u7684\u539f\u5219\u4fbf\u662f\u4fbf\u6377\u6211\u4eec\u7684\u5f00\u53d1\uff0c\u4f8b\u5982payload\uff0cList \uff0cgroups\uff0c\u90fd\u53ef\u4ee5\u5ffd\u7565\u3002<\/code><\/p>\n

    <1> \u81ea\u5b9a\u4e49\u6ce8\u89e3\u4e2d<\/code>\u6307\u5b9a\u4e86\u8fd9\u4e2a\u6ce8\u89e3\u771f\u6b63\u7684\u9a8c\u8bc1\u8005\u7c7b<\/code><\/p>\n

    public<\/span> class<\/span> CannotHaveBlankValidator<\/span> implements<\/span> <<\/span>1><\/span><\/span> ConstraintValidator<\/span><<\/span>CannotHaveBlank<\/span>,<\/span> String<\/span>><\/span><\/span> { \n   <\/span> @Override<\/span> public<\/span> void<\/span> initialize<\/span>(<\/span>CannotHaveBlank<\/span> constraintAnnotation)<\/span> { \n   <\/span> }<\/span> @Override<\/span> public<\/span> boolean<\/span> isValid<\/span>(<\/span>String<\/span> value,<\/span> ConstraintValidatorContext<\/span> context <<\/span>2><\/span><\/span>)<\/span> { \n   <\/span> \/\/null\u65f6\u4e0d\u8fdb\u884c\u6821\u9a8c<\/span> if<\/span> (<\/span>value !=<\/span> null<\/span> &&<\/span> value.<\/span>contains<\/span>(<\/span>\" \"<\/span>)<\/span>)<\/span> { \n   <\/span> <<\/span>3><\/span><\/span> \/\/\u83b7\u53d6\u9ed8\u8ba4\u63d0\u793a\u4fe1\u606f<\/span> String<\/span> defaultConstraintMessageTemplate =<\/span> context.<\/span>getDefaultConstraintMessageTemplate<\/span>(<\/span>)<\/span>;<\/span> System<\/span>.<\/span>out.<\/span>println<\/span>(<\/span>\"default message :\"<\/span> +<\/span> defaultConstraintMessageTemplate)<\/span>;<\/span> \/\/\u7981\u7528\u9ed8\u8ba4\u63d0\u793a\u4fe1\u606f<\/span> context.<\/span>disableDefaultConstraintViolation<\/span>(<\/span>)<\/span>;<\/span> \/\/\u8bbe\u7f6e\u63d0\u793a\u8bed<\/span> context.<\/span>buildConstraintViolationWithTemplate<\/span>(<\/span>\"can not contains blank\"<\/span>)<\/span>.<\/span>addConstraintViolation<\/span>(<\/span>)<\/span>;<\/span> return<\/span> false<\/span>;<\/span> }<\/span> return<\/span> true<\/span>;<\/span> }<\/span> }<\/span> <\/code><\/pre>\n

    \u4e5f\u53ef\u4ee5\u5408\u5e76\u8d77\u6765\u5199<\/p>\n

    @Target<\/span>(<\/span>{ \n   <\/span>METHOD,<\/span> FIELD,<\/span> ANNOTATION_TYPE,<\/span> CONSTRUCTOR,<\/span> PARAMETER}<\/span>)<\/span> @Retention<\/span>(<\/span>RUNTIME)<\/span> @Documented<\/span> @Constraint<\/span>(<\/span>validatedBy =<\/span> { \n   <\/span>CannotHaveBlank.<\/span>CannotHaveBlankValidator<\/span>.<\/span>class<\/span>}<\/span>)<\/span><<\/span>1><\/span><\/span> public<\/span> @interface<\/span> CannotHaveBlank<\/span> { \n   <\/span> \/\/\u9ed8\u8ba4\u9519\u8bef\u6d88\u606f<\/span> String<\/span> message<\/span>(<\/span>)<\/span> default<\/span> \"\u4e0d\u80fd\u5305\u542b\u7a7a\u683c\"<\/span>;<\/span> \/\/\u5206\u7ec4<\/span> Class<\/span><<\/span>?<\/span>><\/span><\/span>[<\/span>]<\/span> groups<\/span>(<\/span>)<\/span> default<\/span> { \n   <\/span>}<\/span>;<\/span> \/\/\u8d1f\u8f7d<\/span> Class<\/span><<\/span>?<\/span> extends<\/span> Payload<\/span>><\/span><\/span>[<\/span>]<\/span> payload<\/span>(<\/span>)<\/span> default<\/span> { \n   <\/span>}<\/span>;<\/span> class<\/span> CannotHaveBlankValidator<\/span> implements<\/span> <<\/span>1><\/span><\/span> ConstraintValidator<\/span><<\/span>CannotHaveBlank<\/span>,<\/span> String<\/span>><\/span><\/span> { \n   <\/span> @Override<\/span> public<\/span> void<\/span> initialize<\/span>(<\/span>CannotHaveBlank<\/span> constraintAnnotation)<\/span> { \n   <\/span> }<\/span> @Override<\/span> public<\/span> boolean<\/span> isValid<\/span>(<\/span>String<\/span> value,<\/span> ConstraintValidatorContext<\/span> context <<\/span>2><\/span><\/span>)<\/span> { \n   <\/span> .<\/span>.<\/span>.<\/span> }<\/span> }<\/span> }<\/span> <\/code><\/pre>\n
    public<\/span> class<\/span> User<\/span>{ \n   <\/span> @CannotHaveBlank<\/span>(<\/span>groups=<\/span>ValidCannotHaveBlank<\/span>.<\/span>class<\/span>)<\/span> private<\/span> String<\/span> name;<\/span> public<\/span> interface<\/span> ValidCannotHaveBlank<\/span> { \n   <\/span> }<\/span> }<\/span> <\/code><\/pre>\n

    \u81ea\u5b9a\u4e49\u6548\u9a8c\u7684\u9a8c\u8bc1\u8005\u7f16\u5199\u89c4\u5219<\/strong><\/p>\n

    \u6240\u6709\u7684\u9a8c\u8bc1\u8005\u90fd\u9700\u8981\u5b9e\u73b0ConstraintValidator\u63a5\u53e3<\/code>\uff0c\u5b83\u7684\u63a5\u53e3\u4e5f\u5f88\u5f62\u8c61\uff0c\u5305\u542b\u4e00\u4e2a\u521d\u59cb\u5316\u4e8b\u4ef6\u65b9\u6cd5\uff0c\u548c\u4e00\u4e2a\u5224\u65ad\u662f\u5426\u5408\u6cd5\u7684\u65b9\u6cd5<\/code><\/p>\n

    public<\/span> interface<\/span> ConstraintValidator<\/span><<\/span>A<\/span> extends<\/span> Annotation<\/span>,<\/span> T<\/span>><\/span><\/span> { \n   <\/span> void<\/span> initialize<\/span>(<\/span>A<\/span> constraintAnnotation)<\/span>;<\/span> boolean<\/span> isValid<\/span>(<\/span>T<\/span> value,<\/span> ConstraintValidatorContext<\/span> context)<\/span>;<\/span> }<\/span> <\/code><\/pre>\n

    <2><\/strong> ConstraintValidatorContext<\/code> \u8fd9\u4e2a\u4e0a\u4e0b\u6587<\/code> \u5305\u542b\u4e86\u8ba4\u8bc1\u4e2d\u6240\u6709\u7684\u4fe1\u606f<\/code>\uff0c\u6211\u4eec\u53ef\u4ee5\u5229\u7528\u8fd9\u4e2a\u4e0a\u4e0b\u6587\u5b9e\u73b0\u83b7\u53d6\u9ed8\u8ba4\u9519\u8bef\u63d0\u793a\u4fe1\u606f<\/code>\uff0c\u7981\u7528\u9519\u8bef\u63d0\u793a\u4fe1\u606f<\/code>\uff0c\u6539\u5199\u9519\u8bef\u63d0\u793a\u4fe1\u606f\u7b49\u64cd\u4f5c<\/code>\u3002<\/p>\n

    <3><\/strong> \u4e00\u4e9b\u5178\u578b\u6821\u9a8c\u64cd\u4f5c<\/code>\uff0c\u6216\u8bb8\u53ef\u4ee5\u5bf9\u4f60\u4ea7\u751f\u542f\u793a\u4f5c\u7528\u3002<\/p>\n

    \u503c\u5f97\u6ce8\u610f\u7684\u4e00\u70b9\u662f\uff0c\u81ea\u5b9a\u4e49\u6ce8\u89e3<\/code> \u53ef\u4ee5\u7528\u5728METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER<\/code>\u4e4b\u4e0a\uff0cConstraintValidator\u7684\u7b2c\u4e8c\u4e2a\u6cdb\u578b\u53c2\u6570T\uff0c\u662f\u9700\u8981\u88ab\u6821\u9a8c\u7684\u7c7b\u578b<\/code><\/p>\n

    @DefaultGroupSequenceProvider + \u81ea\u5b9a\u4e49\u6548\u9a8c<\/h3>\n

    \u89e3\u51b3\u591a\u5b57\u6bb5\u8054\u5408\u903b\u8f91\u6821\u9a8c\u95ee\u9898\u3010\u4eab\u5b66Spring MVC\u3011<\/p>\n

    \u89c1 \u767e\u5ea6\u7f51\u76d8 AopDemo
    \"java\u53c2\u6570\u6821\u9a8c_\u6b63\u5219\u8868\u8fbe\u5f0f\u5728\u7ebf\u6d4b\u8bd5\u5de5\u5177<\/p>\n

    \u95ee\u9898<\/h2>\n

    @NotNull @NotBlank \u6ce8\u89e3\u65e0\u6548<\/h3>\n

    1 \u5916\u90e8\u8c03\u7528\u7c7b<\/code>\u6ca1\u7528@Valid \u6216@Validated\u6ce8\u89e3<\/code>\uff08\u6bd4\u5982controller\u65b9\u6cd5\u53c2\u6570\u4e2d\u6ca1\u52a0<\/code>\uff09
    \"java\u53c2\u6570\u6821\u9a8c_\u6b63\u5219\u8868\u8fbe\u5f0f\u5728\u7ebf\u6d4b\u8bd5\u5de5\u5177
    \"java\u53c2\u6570\u6821\u9a8c_\u6b63\u5219\u8868\u8fbe\u5f0f\u5728\u7ebf\u6d4b\u8bd5\u5de5\u5177
    \"java\u53c2\u6570\u6821\u9a8c_\u6b63\u5219\u8868\u8fbe\u5f0f\u5728\u7ebf\u6d4b\u8bd5\u5de5\u51772 \u5c1d\u8bd5hibnerate\u7684\u5305<\/code><\/p>\n

    @javax.validation.constraints.NotBlank<\/span> \u5c1d\u8bd5\u4e0b\u9762 @org.hibernate.validator.constraints.NotBlank<\/span> <\/code><\/pre>\n

    \u6ce8\u610f<\/h2>\n

    \u63a5\u53e3\u8981\u6548\u9a8c\u7684\u53c2\u6570\u524d @Valid<\/h3>\n
    \u6ce8\u89e3\u6548\u9a8c\u7684\u7c7b \u4f7f\u7528\u65f6 controller \u63a5\u53e3\uff0c\u8981\u6548\u9a8c\u7684\u53c2\u6570\u524d @Valid<\/span> <\/code><\/pre>\n
    @PostMapping<\/span>(<\/span>\"\/register\"<\/span>)<\/span> public<\/span> String<\/span> handlePostRequest<\/span>(<\/span>@RequestBody<\/span> @Valid<\/span> UserInfo<\/span> user,<\/span> BindingResult<\/span> bindingResult)<\/span> { \n   <\/span> .<\/span>.<\/span>.<\/span> }<\/span> <\/code><\/pre>\n

    controller<\/code>\u4e0a\u52a0@Validated <\/code>\u5c31\u80fd\u62a5\u9519<\/p>\n

    @Slf4j<\/span> @RestController<\/span> @Api<\/span>(<\/span>description =<\/span> \"\u7528\u6237\u7ba1\u7406\"<\/span>)<\/span> @Validated<\/span> @RequestMapping<\/span>(<\/span>\"user\"<\/span>)<\/span> public<\/span> class<\/span> UserController<\/span> { \n   <\/span> @PostMapping<\/span>(<\/span>\"\/register\"<\/span>)<\/span> public<\/span> String<\/span> handlePostRequest<\/span>(<\/span>@RequestBody<\/span> @Valid<\/span> UserInfo<\/span> user,<\/span> BindingResult<\/span> bindingResult)<\/span> { \n   <\/span> if<\/span> (<\/span>bindingResult.<\/span>hasErrors<\/span>(<\/span>)<\/span>)<\/span> { \n   <\/span> return<\/span> \"register fail\"<\/span>;<\/span> }<\/span> log.<\/span>info<\/span>(<\/span>\"user: {}\"<\/span>,<\/span> user.<\/span>toString<\/span>(<\/span>)<\/span>)<\/span>;<\/span> return<\/span> \"register success\"<\/span>;<\/span> }<\/span> }<\/span> <\/code><\/pre>\n

    \u6548\u9a8c\u7684\u6267\u884c\u987a\u5e8f<\/h3>\n

    \"java\u53c2\u6570\u6821\u9a8c_\u6b63\u5219\u8868\u8fbe\u5f0f\u5728\u7ebf\u6d4b\u8bd5\u5de5\u5177<\/p>\n","protected":false},"excerpt":{"rendered":"java\u53c2\u6570\u6821\u9a8c_\u6b63\u5219\u8868\u8fbe\u5f0f\u5728\u7ebf\u6d4b\u8bd5\u5de5\u5177Validator\u7ed3\u5408@InitBinder\u6ce8\u89e3https:\/\/blog.csdn.net\/_\/article\/details\/\u5b9e\u73b0\u4e00\u4e2aValida...","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"_links":{"self":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/4908"}],"collection":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/comments?post=4908"}],"version-history":[{"count":0,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/4908\/revisions"}],"wp:attachment":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/media?parent=4908"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/categories?post=4908"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/tags?post=4908"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}