{"id":10020636,"date":"2025-01-20T11:52:49","date_gmt":"2025-01-20T03:52:49","guid":{"rendered":"https:\/\/itmax.cn\/?p=10020636"},"modified":"2025-01-20T11:52:49","modified_gmt":"2025-01-20T03:52:49","slug":"%e3%80%90spring%e3%80%91size-%e6%97%a0%e6%b3%95%e6%8b%a6%e6%88%aanull%e7%9a%84%e5%8e%9f%e5%9b%a0","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10020636","title":{"rendered":"\u3010Spring\u3011@Size \u65e0\u6cd5\u62e6\u622anull\u7684\u539f\u56e0"},"content":{"rendered":"<h3>\u95ee\u9898\u590d\u73b0<\/h3>\n<p>\u5728\u6784\u5efa Web \u670d\u52a1\u65f6\uff0c\u6211\u4eec\u4e00\u822c\u90fd\u4f1a\u5bf9\u4e00\u4e2a HTTP \u8bf7\u6c42\u7684 Body \u5185\u5bb9\u8fdb\u884c\u6821\u9a8c\uff0c\u4f8b\u5982\u6211\u4eec\u6765\u770b\u8fd9\u6837\u4e00\u4e2a\u6848\u4f8b\u53ca\u5bf9\u5e94\u4ee3\u7801\u3002<\/p>\n<p>\u5f53\u5f00\u53d1\u4e00\u4e2a\u5b66\u7c4d\u7ba1\u7406\u7cfb\u7edf\u65f6\uff0c\u6211\u4eec\u4f1a\u63d0\u4f9b\u4e86\u4e00\u4e2a API \u63a5\u53e3\u53bb\u6dfb\u52a0\u5b66\u751f\u7684\u76f8\u5173\u4fe1\u606f\uff0c\u5176\u5bf9\u8c61\u5b9a\u4e49\u53c2\u8003\u4e0b\u9762\u7684\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"java\">import lombok.Data;\nimport javax.validation.constraints.Size;\n@Data\npublic class Student {\n    @Size(min = 1, max = 10)\n    private String name;\n    private short age;\n}<\/code><\/pre>\n<p>\u5982\u4e0a\u4ee3\u7801\u6240\u793a\uff0c\u6211\u4eec\u60f3\u9488\u5bf9\u5b66\u751f\u7684\u59d3\u540d\u8fdb\u884c\u4e00\u4e2a\u5224\u65ad\uff0c\u4e0d\u80fd\u4e3a\u7a7a\uff0c\u5e76\u4e14\u957f\u5ea6\u4e3a1-10\u3002<\/p>\n<p>\u7136\u540e\uff0c\u6211\u4eec\u4ee5\u4e0b\u9762\u7684 JSON Body \u505a\u6d4b\u8bd5\uff1a<\/p>\n<pre><code class=\"json\">{\n  &quot;name&quot;: &quot;&quot;,\n  &quot;age&quot;: 10,\n  &quot;phone&quot;: {&quot;number&quot;:&quot;12306&quot;}\n}<\/code><\/pre>\n<p>\u6d4b\u8bd5\u7ed3\u679c\u7b26\u5408\u6211\u4eec\u7684\u9884\u671f\uff0c\u4f46\u662f\u5047\u8bbe\u66f4\u8fdb\u4e00\u6b65\uff0c\u7528\u4e0b\u9762\u7684 JSON Body\uff08\u53bb\u9664 name \u5b57\u6bb5\uff09\u505a\u6d4b\u8bd5\u5462\uff1f<\/p>\n<pre><code class=\"json\">{\n  &quot;age&quot;: 10,\n  &quot;phone&quot;: {&quot;number&quot;:&quot;12306&quot;}\n}<\/code><\/pre>\n<p>\u6211\u4eec\u4f1a\u53d1\u73b0\u6821\u9a8c\u5931\u8d25\u4e86\u3002\u8fd9\u7ed3\u679c\u96be\u514d\u8ba9\u6211\u4eec\u6709\u4e00\u4e9b\u60ca\u8bb6\uff0c\u4e5f\u500d\u611f\u56f0\u60d1\uff1a@Size(min = 1, max = 10) \u90fd\u5df2\u7ecf\u8981\u6c42\u6700\u5c0f\u957f\u5ea6\u4e3a 1 \u4e86\uff0c\u96be\u9053\u8fd8\u53ea\u80fd\u7ea6\u675f\u7a7a\u5b57\u7b26\u4e32\uff08\u5373\u201c\u201d\uff09\uff0c\u4e0d\u80fd\u7ea6\u675f null?<\/p>\n<h3>\u6848\u4f8b\u5206\u6790<\/h3>\n<p>\u5982\u679c\u6211\u4eec\u7a0d\u5fae\u7559\u5fc3\u70b9\u7684\u8bdd\uff0c\u5c31\u4f1a\u53d1\u73b0\u5176\u5b9e @Size \u7684 Javadoc \u5df2\u7ecf\u660e\u786e\u4e86\u8fd9\u79cd\u60c5\u51b5\uff0c\u53c2\u8003\u4e0b\u56fe\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"544\" height=\"439\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVdhkI5\" alt=\"image.png\" title=\"image.png\"><\/p>\n<p>\u5982\u56fe\u6240\u793a\uff0c&#8221;null elements are considered valid&#8221; \u5f88\u597d\u5730\u89e3\u91ca\u4e86\u7ea6\u675f\u4e0d\u4f4f null \u7684\u539f\u56e0\u3002\u5f53\u7136\u7eb8\u4e0a\u5f97\u6765\u7ec8\u89c9\u6d45\uff0c\u6211\u4eec\u8fd8\u9700\u8981\u4ece\u6e90\u7801\u7ea7\u522b\u89e3\u8bfb\u4e0b @Size \u7684\u6821\u9a8c\u8fc7\u7a0b\u3002<\/p>\n<p>\u8fd9\u91cc\u6211\u4eec\u627e\u5230\u4e86\u5b8c\u6210 @Size \u7ea6\u675f\u7684\u6267\u884c\u65b9\u6cd5\uff0c\u53c2\u8003 SizeValidatorForCharSequence#isValid \u65b9\u6cd5\uff1a<\/p>\n<pre><code class=\"java\">public boolean isValid(CharSequence charSequence, ConstraintValidatorContext constraintValidatorContext) {\n    if ( charSequence == null ) {\n        return true;\n    }\n    int length = charSequence.length();\n    return length &gt;= min &amp;&amp; length &lt;= max;\n}<\/code><\/pre>\n<p>\u5982\u4ee3\u7801\u6240\u793a\uff0c\u5f53\u5b57\u7b26\u4e32\u4e3a null \u65f6\uff0c\u76f4\u63a5\u901a\u8fc7\u4e86\u6821\u9a8c\uff0c\u800c\u4e0d\u4f1a\u505a\u4efb\u4f55\u8fdb\u4e00\u6b65\u7684\u7ea6\u675f\u68c0\u67e5\u3002<\/p>\n<h3>\u95ee\u9898\u4fee\u6b63<\/h3>\n<p>\u5173\u4e8e\u8fd9\u4e2a\u95ee\u9898\u7684\u4fee\u6b63\uff0c\u5176\u5b9e\u5f88\u7b80\u5355\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5176\u4ed6\u7684\u6ce8\u89e3\uff08@NotNull \u6216 @NotEmpty\uff09\u6765\u52a0\u5f3a\u7ea6\u675f\uff0c\u4fee\u6b63\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"java\">@NotEmpty\n@Size(min = 1, max = 10)\nprivate String name;<\/code><\/pre>\n<p>\u5b8c\u6210\u4ee3\u7801\u4fee\u6539\u540e\uff0c\u91cd\u65b0\u6d4b\u8bd5\uff0c\u4f60\u5c31\u4f1a\u53d1\u73b0\u7ea6\u675f\u5df2\u7ecf\u5b8c\u5168\u6ee1\u8db3\u6211\u4eec\u7684\u9700\u6c42\u4e86\u3002<\/p>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000046037910\">https:\/\/segmentfault.com\/a\/1190000046037910<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u95ee\u9898\u590d\u73b0 \u5728\u6784\u5efa Web \u670d\u52a1\u65f6\uff0c\u6211\u4eec\u4e00\u822c\u90fd\u4f1a\u5bf9\u4e00\u4e2a HTTP \u8bf7\u6c42\u7684 Body \u5185\u5bb9\u8fdb\u884c\u6821\u9a8c\uff0c\u4f8b\u5982\u6211\u4eec\u6765\u770b\u8fd9 [&hellip;]<\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4],"tags":[],"class_list":["post-10020636","post","type-post","status-publish","format-standard","hentry","category-share"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10020636","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10020636"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10020636\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10020636"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10020636"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10020636"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}