{"id":1397,"date":"2023-03-19T11:14:20","date_gmt":"2023-03-19T03:14:20","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=1397"},"modified":"2023-04-28T21:10:17","modified_gmt":"2023-04-28T13:10:17","slug":"implement-field-validation-through-annotations-under-javax-validation-constraints","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/03\/19\/implement-field-validation-through-annotations-under-javax-validation-constraints\/","title":{"rendered":"\u901a\u8fc7javax.validation.constraints\u4e0b\u7684\u6ce8\u89e3\u5b9e\u73b0\u5b57\u6bb5\u9a8c\u8bc1"},"content":{"rendered":"<h2>\u6dfb\u52a0maven\u4f9d\u8d56\u5305<\/h2>\n<pre><code class=\"language-xml\">&lt;!-- https:\/\/mvnrepository.com\/artifact\/javax.validation\/validation-api --&gt;\n&lt;dependency&gt;\n  &lt;groupId&gt;javax.validation&lt;\/groupId&gt;\n  &lt;artifactId&gt;validation-api&lt;\/artifactId&gt;\n&lt;\/dependency&gt;<\/code><\/pre>\n<p><!-- more --><\/p>\n<h2>\u5728\u6821\u9a8c\u5b57\u6bb5\u4e0a\u6dfb\u52a0\u6821\u9a8c\u6ce8\u89e3<\/h2>\n<pre><code class=\"language-java\">class Person {\n    @NotNull(message = &quot;\u5b57\u6bb5\u503c\u4e0d\u80fd\u4e3a\u7a7a&quot;)\n    private String name;\n\n    @NotNull\n    private String sex;\n\n    @Max(value = 20, message = &quot;\u6700\u5927\u957f\u5ea6\u4e3a20&quot;)\n    private String address;\n\n    @NotNull\n    @Size(max = 10, min = 5, message = &quot;\u5b57\u6bb5\u957f\u5ea6\u8981\u57285-10\u4e4b\u95f4&quot;)\n    private String fileName;\n\n    @Pattern(regexp = &quot;^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\\\\.[a-zA-Z0-9-]+)*\\\\.[a-zA-Z0-9]{2,6}$&quot;, message = &quot;\u4e0d\u6ee1\u8db3\u90ae\u7bb1\u6b63\u5219\u8868\u8fbe\u5f0f&quot;)\n    private String email;\n\n    @AssertTrue(message = &quot;\u5b57\u6bb5\u4e3atrue\u624d\u80fd\u901a\u8fc7&quot;)\n    private boolean isSave;\n\n    @Future(message = &quot;\u65f6\u95f4\u5728\u5f53\u524d\u65f6\u95f4\u4e4b\u540e\u624d\u53ef\u4ee5\u901a\u8fc7&quot;)\n    private Date date;\n\n}<\/code><\/pre>\n<p>\u5728\u63a7\u5236\u5668\u63a5\u6536\u53c2\u6570\u65f6\uff0c\u6dfb\u52a0<code>@Valid<\/code>\u6ce8\u89e3\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-java\">public String index(@Valid Person person, BindingResult result)<\/code><\/pre>\n<p>\u53ef\u4ee5\u901a\u8fc7BindingResult\u5bf9\u8c61\u83b7\u53d6\u76f8\u5173\u7684\u9519\u8bef\u63d0\u793a\u3002<\/p>\n<p>\u4e5f\u53ef\u4ee5\u81ea\u5b9a\u4e49\u9a8c\u8bc1\u65b9\u5f0f<\/p>\n<pre><code class=\"language-java\">@Component\npublic class ValidatorHelper {\n\n    @Autowired\n    private Validator validator;\n\n    public &lt;T&gt; Pair&lt;Boolean, String&gt; validate(T request) {\n        if (request == null) {\n            return Pair.of(Boolean.FALSE, &quot;request is null&quot;);\n        }\n\n        Set&lt;ConstraintViolation&lt;T&gt;&gt; r = validator.validate(request);\n        Iterator&lt;ConstraintViolation&lt;T&gt;&gt; it = r.iterator();\n        if (it.hasNext()) {\n            ConstraintViolation&lt;T&gt; c = it.next();\n            return Pair.of(Boolean.FALSE, String.format(&quot;[%s]%s&quot;, c.getPropertyPath(), c.getMessage()));\n        }\n        return Pair.of(Boolean.TRUE, &quot;validate success&quot;);\n    }\n\n}<\/code><\/pre>\n<h2>\u9a8c\u8bc1\u7684\u6ce8\u89e3<\/h2>\n<p>\uff081\uff09<code>@NotNull<\/code><\/p>\n<p>\u4f7f\u7528\u8be5\u6ce8\u89e3\u7684\u5b57\u6bb5\u7684\u503c\u4e0d\u80fd\u4e3anull\uff0c\u5426\u5219\u9a8c\u8bc1\u65e0\u6cd5\u901a\u8fc7<\/p>\n<p>\uff082\uff09<code>@Null<\/code><\/p>\n<p>\u4fee\u9970\u7684\u5b57\u6bb5\u5728\u9a8c\u8bc1\u65f6\u5fc5\u987b\u662fnull\uff0c\u5426\u5219\u9a8c\u8bc1\u65e0\u6cd5\u901a\u8fc7<\/p>\n<p>\uff083\uff09<code>@Size<\/code><\/p>\n<p>\u5982\u4e0b\u4ee3\u7801\u8868\u793a\uff0c\u4fee\u9970\u7684\u5b57\u6bb5\u957f\u5ea6\u4e0d\u80fd\u8d85\u8fc75\u6216\u8005\u4f4e\u4e8e1<\/p>\n<pre><code class=\"language-java\">@Size(min = 1, max = 5)\nprivate String name;<\/code><\/pre>\n<p>\uff084\uff09<code>@Max<\/code><\/p>\n<p>\u5982\u4e0b\u4ee3\u7801\u8868\u793a\uff0c\u8be5\u5b57\u6bb5\u7684\u6700\u5927\u503c\u4e3a19\uff0c\u5426\u5219\u65e0\u6cd5\u901a\u8fc7\u9a8c\u8bc1<\/p>\n<pre><code class=\"language-java\">@Max(value = 19)\nprivate Integer age;<\/code><\/pre>\n<p>\uff085\uff09<code>@Min<\/code><\/p>\n<p>\u540c\u7406\uff0c\u88ab\u8be5\u6ce8\u89e3\u4fee\u9970\u7684\u5b57\u6bb5\u7684\u6700\u5c0f\u503c\uff0c\u4e0d\u80fd\u4f4e\u4e8e\u67d0\u4e2a\u503c<\/p>\n<p>\uff086\uff09<code>@AssertFalse<\/code><\/p>\n<p>\u8be5\u5b57\u6bb5\u503c\u4e3afalse\u65f6\uff0c\u9a8c\u8bc1\u624d\u80fd\u901a\u8fc7<\/p>\n<p>\uff087\uff09`@AssertTrue<\/p>\n<p>\u8be5\u5b57\u6bb5\u503c\u4e3atrue\u65f6\uff0c\u9a8c\u8bc1\u624d\u80fd\u901a\u8fc7<\/p>\n<p>\uff088\uff09<code>@DecimalMax<\/code><\/p>\n<p>\u9a8c\u8bc1\u5c0f\u6570\u7684\u6700\u5927\u503c<\/p>\n<pre><code class=\"language-java\">@DecimalMax(value = &quot;12.35&quot;)\nprivate double money;<\/code><\/pre>\n<p>\uff089\uff09<code>@DecimalMin<\/code><\/p>\n<p>\u9a8c\u8bc1\u5c0f\u6570\u7684\u6700\u5c0f\u503c<\/p>\n<p>\uff0810\uff09<code>@Digits<\/code><\/p>\n<p>\u9a8c\u8bc1\u6570\u5b57\u7684\u6574\u6570\u4f4d\u548c\u5c0f\u6570\u4f4d\u7684\u4f4d\u6570\u662f\u5426\u8d85\u8fc7\u6307\u5b9a\u7684\u957f\u5ea6<\/p>\n<pre><code class=\"language-java\">@Digits(integer = 2, fraction = 2)\nprivate double money;<\/code><\/pre>\n<p>\uff0811\uff09<code>@Future<\/code><\/p>\n<p>\u9a8c\u8bc1\u65e5\u671f\u662f\u5426\u5728\u5f53\u524d\u65f6\u95f4\u4e4b\u540e\uff0c\u5426\u5219\u65e0\u6cd5\u901a\u8fc7\u6821\u9a8c<\/p>\n<pre><code class=\"language-java\">@Future\nprivate Date date;<\/code><\/pre>\n<p>\uff0812\uff09<code>@Past<\/code><\/p>\n<p>\u9a8c\u8bc1\u65e5\u671f\u662f\u5426\u5728\u5f53\u524d\u65f6\u95f4\u4e4b\u524d\uff0c\u5426\u5219\u65e0\u6cd5\u901a\u8fc7\u6821\u9a8c<\/p>\n<p>\uff0813\uff09<code>@Pattern<\/code><\/p>\n<p>\u7528\u4e8e\u9a8c\u8bc1\u5b57\u6bb5\u662f\u5426\u4e0e\u7ed9\u5b9a\u7684\u6b63\u5219\u76f8\u5339\u914d<\/p>\n<pre><code class=\"language-java\">@Pattern(regexp = &quot;[abc]&quot;)\nprivate String name;<\/code><\/pre>\n<h2>@NotEmpty, @NotNull, @NotBlank \u7684\u533a\u522b<\/h2>\n<p>\uff081\uff09<code>@NotEmpty<\/code><\/p>\n<p>The annotated element must not be {@code null} nor empty.<br \/>\n\u4e0d\u80fd\u662fnull<br \/>\n\u4e0d\u80fd\u662f\u7a7a\u5b57\u7b26<br \/>\n\u96c6\u5408\u6846\u67b6\u4e2d\u7684\u5143\u7d20\u4e0d\u80fd\u4e3a\u7a7a<\/p>\n<p>\uff082\uff09<code>@NotNul<\/code><\/p>\n<p>\u88ab\u4fee\u9970\u5143\u7d20\u4e0d\u80fd\u4e3anull\uff0c\u53ef\u7528\u5728\u57fa\u672c\u7c7b\u578b\u4e0a<\/p>\n<p>\uff083\uff09<code>@NotBlank<\/code><\/p>\n<p>The annotated element must not be {@code null} and must contain at least one non-whitespace character.<\/p>\n<p>\u8be5\u6ce8\u89e3\u7528\u6765\u5224\u65ad\u5b57\u7b26\u4e32\u6216\u8005\u5b57\u7b26\uff0c\u53ea\u7528\u5728String\u4e0a\u9762<\/p>\n<p>\u5982\u679c\u5728\u57fa\u672c\u7c7b\u578b\u4e0a\u9762\u7528<code>@NotEmpty<\/code>\u6216\u8005<code>@NotBlank<\/code>\uff0c\u4f1a\u51fa\u73b0\u4ee5\u4e0b\u9519\u8bef<\/p>\n<pre><code>org.springframework.web.util.NestedServletException: Request processing failed; nested exception is javax.validation.UnexpectedTypeException: HV000030: No validator could be found for constraint &#039;javax.validation.constraints.NotBlank&#039; validating type &#039;java.lang.Long&#039;. Check configuration for &#039;offset&#039;<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u6dfb\u52a0maven\u4f9d\u8d56\u5305 &lt;!&#8211; https:\/\/mvnrepository.com\/artifact\/j [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[43],"tags":[],"class_list":["post-1397","post","type-post","status-publish","format-standard","hentry","category-java-basic"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1397","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=1397"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1397\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=1397"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=1397"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=1397"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}