{"id":10007995,"date":"2023-06-26T16:00:23","date_gmt":"2023-06-26T08:00:23","guid":{"rendered":"https:\/\/ityet.com\/?p=10007995"},"modified":"2023-06-26T16:00:23","modified_gmt":"2023-06-26T08:00:23","slug":"%e8%bf%99%e9%97%ae%e9%a2%98%e5%b7%a7%e4%ba%86%ef%bc%8cspringmvc-%e4%b8%8d%e5%90%8c%e5%8f%82%e6%95%b0%e5%a4%84%e7%90%86%e6%9c%ba%e5%88%b6%e5%bc%95%e5%8f%91%e7%9a%84%e6%80%9d%e8%80%83-%e4%ba%ac","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10007995","title":{"rendered":"\u8fd9\u95ee\u9898\u5de7\u4e86\uff0cSpringMVC \u4e0d\u540c\u53c2\u6570\u5904\u7406\u673a\u5236\u5f15\u53d1\u7684\u601d\u8003 | \u4eac\u4e1c\u4e91\u6280\u672f\u56e2\u961f"},"content":{"rendered":"<p>\u8fd9\u4e2a\u95ee\u9898\u975e\u5e38\u6709\u8da3\uff0c\u4e0d\u662fSpringMVC \u7684\u95ee\u9898\uff0c\u662f\u5b9e\u9645\u5f00\u53d1\u4e2d\u6df7\u5408\u4f7f\u7528\u4e86\u4e24\u79cd\u8bf7\u6c42\u65b9\u5f0f\u66b4\u9732\u51fa\u6765\u7684\u3002<\/p>\n<h2>\u95ee\u9898\u573a\u666f<\/h2>\n<p>\u529f\u80fd\u6a21\u5757\u4e2d\uff0c\u63d0\u4f9b\u4e24\u4e2a Http \u670d\u52a1\u3002\u4e00\u4e2a\u662f\u5217\u8868\u67e5\u8be2\uff08application\/json \u8bf7\u6c42\uff09\uff0c\u4e00\u4e2a\u662f\u5217\u8868\u5bfc\u51fa\uff08\u8868\u5355\u8bf7\u6c42\uff09\u3002\u8fd0\u884c\u73af\u5883\u53d1\u73b0\u4e2a\u95ee\u9898\uff1aMVC model \u65b0\u6dfb\u52a0\u7684\u5c5e\u6027\uff0c\u7c7b\u4f3c\u7684 Http \u8bf7\u6c42\uff0c\u4e00\u4e2a\u6709\u503c\uff0c\u4e00\u4e2a\u6ca1\u6709<\/p>\n<p>\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code>\/**\n * application\/json \u8bf7\u6c42\u3002 \u8fd9\u79cd\u60c5\u51b5 param.field2 \u6709\u503c \u2714\n * @param param RequestResponseBodyMethodProcessr \u5904\u7406 HttpServletRequest \u53c2\u6570\n *\/\n@PostMapping(value = &quot;query&quot;)\npublic ResponseResult&lt;Page&lt;SomeData&gt;&gt; queryByCondition(@RequestBody SomeParam param){\n    \/\/ \u4e1a\u52a1\u903b\u8f91...\n}\n\n\/**\n * application\/x-www-form-urlencoded \u8bf7\u6c42 \u8fd9\u79cd\u60c5\u51b5 param.field2 \u6ca1\u6709\u6709\u8d4b\u503c \u274c\n * @param param ServletModelAttributeMethodProcessor \u5904\u7406 HttpServletRequest \u53c2\u6570\n *\/\n@PostMapping(value = &quot;export&quot;)\npublic void exportExcel(SomeParam param) {\n    \/\/ \u4e1a\u52a1\u903b\u8f91...\n}\n\n\npublic class SomeParam {\n\n    \/\/ \u8fd9\u4e2a\u662f\u539f\u6709\u7684\uff0c\u6709 get set \u65b9\u6cd5\n    private String field1;\n\n    \/\/ \u8fd9\u4e2a\u662f\u65b0\u589e\u7684\uff0c\u6ca1\u6709get set \u65b9\u6cd5 \uff08\u8fd9\u662f\u4e00\u4e2a\u5de7\u5408\u3001\u610f\u5916\uff09\u3002 \u95ee\u9898\u5c31\u51fa\u5728\u8fd9\u91cc\u3002\n    private String field2;\n\n}<\/code><\/pre>\n<p>\u2753 \u6839\u636e\u4ee3\u7801\u5206\u6790\uff0c\u90a3\u5e94\u8be5\u662f SpringMVC \u9488\u5bf9\u8fd9\u4e24\u79cd\u53c2\u6570\u5904\u7406\u7684\u673a\u5236\u4e0d\u540c\u3002  <br \/>\u9488\u5bf9\u4e0a\u8ff0\u7684\u53c2\u6570\u5904\u7406\uff0c\u53ef\u4ee5\u53c2\u8003\uff1a  <br \/>RequestResponseBodyMethodProcessor\u3001\u00a0ServletModelAttributeMethodProcessor<\/p>\n<h2>Insight RequestResponseBodyMethodProcessor<\/h2>\n<p>\u5904\u7406 Http Body \u7684\u6570\u636e\u3002\u89e3\u6790\u6ce8\u89e3 RequestBody \u7684\u53c2\u6570\u3002<\/p>\n<p>\u9488\u5bf9 MimeType \u4e3a application\/json \u7684\u8bf7\u6c42\uff0c\u6309\u7167json \u683c\u5f0f\u8fdb\u884c\u53cd\u5e8f\u5217\u5316\u3002<\/p>\n<p>\u9ed8\u8ba4\u53c2\u6570\u5904\u7406\u5668  <br \/>MappingJackson2HttpMessageConverter<\/p>\n<p>string \u53cd\u5e8f\u5217\u5316\u4e3a\u5bf9\u8c61\uff0c\u4f7f\u7528\u7684\u662f  <br \/>com.fasterxml.jackson.databind.ObjectMapper\u3002<\/p>\n<p>\u4e0a\u8ff0\u5de5\u7a0b\u4e2d\uff0c\u5bf9 ObjectMapper \u5f00\u542f private \u5c5e\u6027\u68c0\u6d4b\u3002\u65b0\u589e\u7684\u5c5e\u6027\u53ef\u4ee5\u6b63\u5e38\u53cd\u5e8f\u5217\u5316\u3002<\/p>\n<pre><code>ObjectMapper mapper = new ObjectMapper();\n\/\/ \u8fd9\u53c8\u662f\u4e00\u4e2a\u5de7\u5408\u3001\u610f\u5916  \u548b\u8fd8\u6709\u8fd9\u4e2a\u7528\u6cd5 \nmapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);<\/code><\/pre>\n<p>Visibility \u5177\u4f53\u7684\u7528\u6cd5\u793a\u4f8b\u53c2\u8003\uff1a Jackson &#8211; Decide What Fields Get (De)Serialized | Baeldung<\/p>\n<p>\u539f\u7406\uff1a \u5982\u679c\u6ca1\u6709 setter \u65b9\u6cd5\uff0cjackson \u4f1a\u64cd\u4f5c field \u6765\u5b8c\u6210\u8d4b\u503c\u3002<\/p>\n<pre><code>\/**\n * This concrete sub-class implements property that is set directly assigning to a Field.\n *\/\npublic final static class FieldProperty extends SettableBeanProperty {\n\n    @Override\n    public final void set(Object instance, Object value) throws IOException {\n        try {\n            _field.set(instance, value);\n        } catch (Exception e) {\n            _throwAsIOE(e, value);\n        }\n    }\n}<\/code><\/pre>\n<h2>Insight ServletModelAttributeMethodProcessor<\/h2>\n<p>\u81ea\u5b9a\u4e49 Class \u53c2\u6570\u89e3\u6790<\/p>\n<p>\u901a\u8fc7\u89e3\u6790 request parameters, \u7528\u6765\u6784\u9020\u548c\u521d\u59cb\u5316\u5bf9\u5e94\u7684\u65b9\u6cd5\u5165\u53c2\u3002<\/p>\n<p>\u4e3b\u8981\u901a\u8fc7  <br \/>ServletRequestDataBinder.bind(request) \u6765\u5b8c\u6210\u3002<\/p>\n<pre><code>\/**\n * Apply given property values to the target object.\n * By default, unknown fields will be ignored.\n * \n * @see org.springframework.validation.DataBinder#applyPropertyValues\n *\/\nprotected void applyPropertyValues(MutablePropertyValues mpvs) {\n    try {\n        \/\/ Bind request parameters onto target object.\n        \/\/ \u9ed8\u8ba4\u4f7f\u7528 BeanWrapperImpl.setPropertyValue()\n        getPropertyAccessor().setPropertyValues(mpvs, isIgnoreUnknownFields(), isIgnoreInvalidFields());\n    }\n    catch (PropertyBatchUpdateException ex) {\n        \/\/ Use bind error processor to create FieldErrors.\n    }\n}<\/code><\/pre>\n<pre><code>public void setPropertyValues(PropertyValues pvs, boolean ignoreUnknown, boolean ignoreInvalid) throws BeansException {\n    \/\/ \u901a\u8fc7\u904d\u5386 request parameters \u6765\u5c1d\u8bd5\u5bf9 target \u8fdb\u884c\u8d4b\u503c\n    List&lt;PropertyValue&gt; propertyValues = (pvs instanceof MutablePropertyValues ?             ((MutablePropertyValues) pvs).getPropertyValueList() : Arrays.asList(pvs.getPropertyValues()));\n    for (PropertyValue pv : propertyValues) {\n        try {\n            \/\/ etPropertyValue \u4f7f\u7528 JDK \u7684 Introspector \u6765\u8fdb\u884c\u5e8f\u5217\u5316\u64cd\u4f5c\u3002\n            \/\/ \u6ca1\u6709setter \u65b9\u6cd5\uff0c\u81ea\u7136\u6ca1\u6cd5\u8d4b\u503c\u3002\n            setPropertyValue(pv);\n        }\n    }\n}<\/code><\/pre>\n<h2>\u603b\u7ed3<\/h2>\n<ul>\n<li>\u4e00\u4ef6\u4e8b\u60c5\u51fa\u9519\uff0c\u4e0d\u662f\u4e00\u5904\u95ee\u9898\u9020\u6210\u7684\u3002<\/li>\n<li>\u5de5\u7a0b\u5f00\u53d1\u8981\u89c4\u8303\uff0c\u7528\u6700\u5e38\u89c4\u3001\u6700\u7a33\u5b9a\u7684\u529e\u6cd5\u6765\u5b9e\u73b0\u3002\u9047\u5230\u7a00\u5947\u53e4\u602a\u7684\u95ee\u9898\u5c31\u662f\u51b7\u95e8\u7528\u6cd5\u5e26\u6765\u7684\u3002<\/li>\n<li>\u5bf9\u4e8e\u5e38\u7528\u7684\u6846\u67b6\u548c\u5de5\u5177\u5e93\u719f\u6089\u5176\u5e95\u5c42\u539f\u7406\uff0c\u9047\u5230\u95ee\u9898\u53ef\u4ee5\u5f88\u5feb\u5b9a\u4f4d\u3002<\/li>\n<\/ul>\n<blockquote>\n<p>\u4f5c\u8005\uff1a\u4eac\u4e1c\u7269\u6d41 \u6768\u6500<\/p>\n<p>\u6765\u6e90\uff1a\u4eac\u4e1c\u4e91\u5f00\u53d1\u8005\u793e\u533a<\/p>\n<\/blockquote>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000043936691\">https:\/\/segmentfault.com\/a\/1190000043936691<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8fd9\u4e2a\u95ee\u9898\u975e\u5e38\u6709\u8da3\uff0c\u4e0d\u662fSpringMVC \u7684\u95ee\u9898\uff0c\u662f\u5b9e\u9645\u5f00\u53d1\u4e2d\u6df7\u5408\u4f7f\u7528\u4e86\u4e24\u79cd\u8bf7\u6c42\u65b9\u5f0f\u66b4\u9732\u51fa\u6765\u7684\u3002 \u95ee\u9898\u573a\u666f \u529f [&hellip;]<\/p>\n","protected":false},"author":1,"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-10007995","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\/10007995","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\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10007995"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10007995\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10007995"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10007995"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10007995"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}