{"id":10020391,"date":"2024-11-11T15:25:40","date_gmt":"2024-11-11T07:25:40","guid":{"rendered":"https:\/\/itmax.cn\/?p=10020391"},"modified":"2024-11-11T15:25:40","modified_gmt":"2024-11-11T07:25:40","slug":"java%e6%b5%85%e6%8b%b7%e8%b4%9dbeanutils-copyproperties%e5%bc%95%e5%8f%91%e7%9a%84rpc%e5%bc%82%e5%b8%b8","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10020391","title":{"rendered":"java\u6d45\u62f7\u8d1dBeanUtils.copyProperties\u5f15\u53d1\u7684RPC\u5f02\u5e38"},"content":{"rendered":"<p>\u4f5c\u8005\uff1a\u4eac\u4e1c\u7269\u6d41 \u5434\u4e49<\/p>\n<h2>\u80cc\u666f<\/h2>\n<p>\u8fd1\u671f\u53c2\u4e0e\u4e86\u4e00\u4e2a\u653b\u575a\u9879\u76ee\uff0c\u524d\u671f\u56e0\u4e3a\u5176\u4ed6\u6d41\u7a0b\u539f\u56e0\uff0c\u6d4b\u8bd5\u65f6\u95f4\u5df2\u7ecf\u803d\u6401\u4e86\u597d\u51e0\u5929\u4e86\uff0c\u672c\u4ee5\u4e3a\u5df2\u7ecf\u89e3\u51b3\u4e86\u5361\u70b9\uff0c\u540e\u7eed\u6d41\u7a0b\u5e94\u8be5\u987a\u987a\u5229\u5229\u7684\uff0c\u6ca1\u60f3\u5230 \u4eba\u5728\u5730\u94c1\u4e0a\uff0cbug\u4ece\u549a\u549a\u6765\/~<\/p>\n<p>\u6ca1\u6709\u4efb\u4f55\u4fee\u6539\u7684\u670d\u52a1\u63a5\u53e3\uff0c\u629b\u51fa\u5f02\u5e38\uff1a<\/p>\n<p>java.lang.ClassCastException: java.util.HashMap cannot be cast to cn.xxx.xxx.xxx.xxx.BatchInfo<\/p>\n<h2>\u6392\u67e5\u8fc7\u7a0b<\/h2>\n<p>1\u3001\u4f5c\u4e3a\u8d44\u6df1\u5199bug\u7684\u8001\u53f8\u673a\uff0c\u7b2c\u4e00\u611f\u89c9\u662f\u4f20\u53c2\u7684\u62a5\u6587\u683c\u5f0f\u6709\u95ee\u9898\u4e86\uff0c\u53ef\u4ee5\u901a\u8fc7\u6a21\u62df\u62a5\u6587\u6392\u67e5\u3002\u4e8e\u662f\u4e4e\uff0c\u5728\u7fa4\u91cc\u5708\u4e86\u670d\u52a1\u63d0\u4f9b\u65b9\u540c\u5b66B\u770b\u4e0b\uff0cBG\u5feb\u901f\u7684\u7528\u6d4b\u8bd5\u5de5\u5177+\u672c\u5730debug\u7684\u65b9\u5f0f\uff0c\u9a8c\u8bc1\u4e86\u4e0b\u62a5\u6587\u683c\u5f0f\uff0c\u53d1\u73b0\u5c45\u7136\u90fd\u8c03\u7528\u6210\u529f\u4e86\u3002\u3002\u3002<\/p>\n<p>2\u3001\u540c\u6b65\u670d\u52a1\u8c03\u7528\u540c\u5b66L\uff0c\u91cd\u70b9\u5173\u6ce8\uff1a1\uff09\u3001\u8c03\u7528\u65b9\u7684\u5e8f\u5217\u5316\u65b9\u5f0f\uff1b2\uff09\u3001\u6700\u8fd1\u4ee3\u7801\u6539\u52a8\u903b\u8f91\u662f\u5426\u6709\u95ee\u9898\u3002L\u540c\u5b66\u786e\u8ba4\u81ea\u5df1\u903b\u8f91\u6ca1\u6709\u95ee\u9898\u540e\uff0c\u540c\u6b65B\u540c\u5b66\u548cS\u540c\u5b66\uff0c\u770b\u5185\u90e8\u662f\u5426\u6709\u4ec0\u4e48\u5904\u7406\u903b\u8f91\u3002\u3002\u3002<\/p>\n<p>3\u3001\u7b2c\u4e8c\u5929\u65e9\u4e0a\u4e00\u6765\uff0c\u5feb\u901f\u5199\u4e86\u5355\u6d4b\uff0c\u786e\u8ba4\u670d\u52a1\u7aef\u6536\u5230\u7684\u62a5\u6587\u683c\u5f0f\uff0c\u7684\u786e\u6ca1\u6709\u95ee\u9898\u3002\u4e8e\u662f\u4e4e\uff0c\u5f00\u59cb\u6252\u4ee3\u7801\u3002\u3002\u3002\u53d1\u73b0\u53ef\u7591\u7684\u4ee3\u7801\uff1a<\/p>\n<p>BeanUtils.copyProperties(item,cargoInfo)<\/p>\n<pre><code>private List&lt;CargoInfo&gt; convertToCargoInfo(OutboundEventCallbackRequest outboundEventCallbackRequest) {\n        return outboundEventCallbackRequest.getCargos().stream().map(item -&gt; {\n            CargoInfo cargoInfo = new CargoInfo();\n            BeanUtils.copyProperties(item, cargoInfo);\n            return cargoInfo;\n    }).collect(Collectors.toList());\n}\n<\/code><\/pre>\n<p>PS\uff1a\u5ba2\u6237\u7aef&#038;\u670d\u52a1\u7aef\u7c7b\u5173\u7cfb<\/p>\n<p><img decoding=\"async\"   referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000045463569\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" title=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\"><\/p>\n<\/p>\n<p>\u56e0\u4e3aBeanUtils.copyProperties\u5c5e\u4e8e\u6d45\u62f7\u8d1d\uff0c\u800c\u6d45\u62f7\u8d1d\u53ea\u662f\u8c03\u7528\u5b50\u5bf9\u8c61\u7684set\u65b9\u6cd5\uff0c\u5e76\u6ca1\u6709\u5c06\u6240\u6709\u5c5e\u6027\u62f7\u8d1d(\u5f15\u7528\u7684\u4e00\u4e2a\u5185\u5b58\u5730\u5740)\u3002\u6240\u4ee5\u5c06\u5728\u8fdb\u884c\u8c03\u7528\u65f6\uff0cJSF\u4f1a\u56e0\u4e3a\u53cd\u5e8f\u5217\u5316\u65f6\u627e\u4e0d\u5230\u5bf9\u5e94\u7684\u7c7b\uff0c\u5c31\u4f1a\u5c06\u5176\u8f6c\u6362\u4e3aMap\u3002<\/p>\n<p>\u76f4\u89c2\u56fe\u5982\u4e0b\uff1a<br \/><img decoding=\"async\"   referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000045463570\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" title=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" loading=\"lazy\"><\/p>\n<p>\u4ee5\u4e0a\uff0c\u521d\u6b65\u5b9a\u4f4d\u539f\u56e0\uff0c\u89e3\u51b3\u65b9\u5f0f\u4e5f\u5c31\u6e05\u6670\u4e86\u3002<\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p>\u53bb\u6389BeanUtils.copyProperties\uff0c\u8fdb\u884c\u624b\u52a8\u8d4b\u503c\u3002\u6700\u7ec8\u89e3\u51b3\u4e86\u8fd9\u4e2a\u95ee\u9898\u3002<\/p>\n<\/p>\n<h2>\u540e\u7eed\u53cd\u601d<\/h2>\n<p>1\u3001\u60f3\u8d77\u738b\u4e1c\u5cb3\u8001\u5e08\u7684\u90a3\u53e5\u8bdd\uff0c\u8d8a\u539f\u59cb\u7684\u8d8a\u7a33\u5b9a\/~<\/p>\n<p>2\u3001\u5982\u679c\u8fd9\u79cd\u8f6c\u6362\u6bd4\u8f83\u591a\uff0c\u5efa\u8bae\u4f7f\u7528MapStruct<\/p>\n<p>3\u3001\u8c28\u614e\u4f7f\u7528BeanUtils.copyProperties\uff0c\u8bf7\u770b\uff1a<\/p>\n<p><img decoding=\"async\"   referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000045463571\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" title=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" loading=\"lazy\"><\/p>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000045463567\">https:\/\/segmentfault.com\/a\/1190000045463567<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4f5c\u8005\uff1a\u4eac\u4e1c\u7269\u6d41 \u5434\u4e49 \u80cc\u666f \u8fd1\u671f\u53c2\u4e0e\u4e86\u4e00\u4e2a\u653b\u575a\u9879\u76ee\uff0c\u524d\u671f\u56e0\u4e3a\u5176\u4ed6\u6d41\u7a0b\u539f\u56e0\uff0c\u6d4b\u8bd5\u65f6\u95f4\u5df2\u7ecf\u803d\u6401\u4e86\u597d\u51e0\u5929\u4e86\uff0c\u672c\u4ee5\u4e3a\u5df2 [&hellip;]<\/p>\n","protected":false},"author":2,"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-10020391","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\/10020391","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\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10020391"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10020391\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10020391"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10020391"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10020391"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}