{"id":10006520,"date":"2022-09-19T15:13:37","date_gmt":"2022-09-19T07:13:37","guid":{"rendered":"https:\/\/ityet.com\/?p=10006520"},"modified":"2022-09-19T15:13:37","modified_gmt":"2022-09-19T07:13:37","slug":"%e5%be%ae%e6%9c%8d%e5%8a%a1%e5%bc%80%e5%8f%91%e7%b3%bb%e5%88%97%ef%bc%9a%e8%ae%a4%e8%af%86%e5%88%b0%e5%ba%8f%e5%88%97%e5%8c%96%e7%9a%84%e9%87%8d%e8%a6%81%e6%80%a7","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10006520","title":{"rendered":"\u5fae\u670d\u52a1\u5f00\u53d1\u7cfb\u5217\uff1a\u8ba4\u8bc6\u5230\u5e8f\u5217\u5316\u7684\u91cd\u8981\u6027"},"content":{"rendered":"<p><a href=\"https:\/\/link.segmentfault.com\/?enc=lBGEpmTQ%2F04tzrJKH3jiwA%3D%3D.HrscYPUDeVc%2BVpwqC6D34QvZYq7NkC%2FMuwIk0NXw%2FmAJwP94RW3hvdQCWr9qsGl%2B%2F3kr1hcr313uu51qcGjcow%3D%3D\" rel=\"nofollow\">\u6e90\u7801\u5730\u5740<\/a><\/p>\n<h2>1 \u5148\u8bf4\u7ed3\u8bba<\/h2>\n<p><strong>\u5728\u8be5\u6846\u67b6\u4e2d\uff0c\u4e0d\u518d\u4f7f\u7528 fastjson \u4f5c\u4e3a\u5e8f\u5217\u5316\u5de5\u5177\uff0c\u5168\u9762\u4f7f\u7528 jackson\u3002<\/strong><\/p>\n<p>\u4f5c\u4e3a\u5bf9 fastjson \u7075\u6d3b\u6027\u7684\u8865\u507f\uff0c\u5728 <code>framework:cn\/framework\/common\/jackson<\/code>\u00a0\u8def\u5f84\u4e0b\uff0c\u63d0\u4f9b\u4e86 <code>Jackson<\/code>\u3001 <code>JacksonObject<\/code>\u3001 <code>JacksonArray<\/code>\u00a0\u4e09\u4e2a\u7c7b\u4f5c\u4e3a\u4ee3\u66ff\uff0c\u57fa\u672c\u4fdd\u7559\u7684 fastjson \u7684\u64cd\u4f5c\u4e60\u60ef\uff0c\u4e0d\u7528\u81ea\u5df1\u65b0\u5efa ObjectMapper\uff0c\u800c\u4e14\u6bd4\u539f\u5148\u7684 fastjson \u63d0\u4f9b\u7684\u7c7b\u66f4\u4e3a\u7075\u6d3b\uff0c\u529f\u80fd\u4e5f\u66f4\u52a0\u5f3a\u5927\u3002<\/p>\n<h2>2 \u4e3a\u4ec0\u4e48\u4e0d\u4f7f\u7528 fastjson<\/h2>\n<p>\u5e8f\u5217\u5316\u53ef\u80fd\u5728\u5355\u4e2a\u9879\u76ee\u4e2d\u88ab\u8ba4\u4e3a\u4e0d\u662f\u591a\u4e48\u91cd\u8981\u7684\u4e8b\u60c5\uff0c\u8fd9\u4e5f\u9020\u6210\u5f88\u591a\u5f00\u53d1\u4eba\u5458\u88ab fastjson \u8ff7\u60d1\u4e86\uff0c\u8ba4\u4e3a\u5e8f\u5217\u5316\u4e0d\u5c31\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u901a\u8fc7 get set \u65b9\u6cd5\u53bb\u5904\u7406 json \u6570\u636e\u7684\u65b9\u5f0f\u5417\uff0c\u6700\u591a\u591a\u4e00\u4e2a\u590d\u6742\u7c7b\u5d4c\u5957\u7684\u5904\u7406\u3002<\/p>\n<p>\u4f46\u662f\u5f53\u4f60\u4f7f\u7528\u89e3\u51b3\u8fc7 spring boot \u5bf9\u65e5\u671f\u5904\u7406\u7684\u7c7b\u578b\u95ee\u9898\u65f6\uff0c\u4f60\u4f1a\u53d1\u73b0 fastjson \u4e2d\u7684\u914d\u7f6e\u662f\u4e0d\u751f\u6548\u7684\u3002<\/p>\n<p>\u5f53\u4f60\u4f7f\u7528\u4e86\u4e00\u4e2a\u679a\u4e3e\u7c7b\u5728\u91cc\u9762\u52a0\u4e0a\u4e00\u4e9b\u590d\u6742\u7684\u6784\u9020\u51fd\u6570\u65f6\uff0c\u4f60\u4f1a\u53d1\u73b0 fastjson \u7cdf\u7cd5\u7684\u4f7f\u7528\u4f53\u9a8c\u3002<\/p>\n<p>\u5982\u679c\u4f60\u5e0c\u671b\u4f7f\u7528 fastjson \u4ee3\u66ff spring boot \u5230 cloud \u67b6\u6784\u4e2d\u7684\u6240\u6709\u9700\u8981\u4f7f\u7528\u5e8f\u5217\u5316\u7684\u5730\u65b9\uff0c\u6211\u53ea\u80fd\u8bf4\u57fa\u672c\u4e0a\u4e0d\u592a\u53ef\u80fd\uff0c\u5373\u4f7f\u52c9\u5f3a\u66ff\u6362\u4e0a\u4e86\uff0c\u4e5f\u4e0d\u77e5\u9053\u54ea\u5929\u4f1a\u51fa\u73b0\u95ee\u9898\uff0c\u5177\u4f53\u7684\u60c5\u51b5\u5728\u6587\u7ae0\u300a<a href=\"https:\/\/segmentfault.com\/a\/1190000039785619\">\u4e3a\u4ec0\u4e48\u4e0d\u5e94\u8be5\u518d\u4f7f\u7528FastJson<\/a>\u300b\u4e2d\u3002<\/p>\n<p>\u56e0\u6b64\uff0c\u5728\u8be5\u67b6\u6784\u4e2d\uff0c\u7981\u6b62\u4f7f\u7528\u9664\u4e86 jackson \u4ee5\u5916\u7684\u5e8f\u5217\u5316\u5de5\u5177\u3002<\/p>\n<p>\u5c31\u7b97\u5728 Alibaba \u7684\u9879\u76ee nacos \u91cc\u9762\u4e5f\u662f\u4f7f\u7528\u7684 jackson\u3002<\/p>\n<h2>3 \u5e8f\u5217\u5316\u5728\u5fae\u670d\u52a1\u6846\u67b6\u4e2d\u7684\u4e00\u81f4\u6027<\/h2>\n<p>\u4f5c\u4e3a\u5fae\u670d\u52a1\uff0c\u670d\u52a1\u591a\u662f\u4e0d\u53ef\u907f\u514d\u7684\uff0c\u90a3\u4e48\u670d\u52a1\u4e0e\u670d\u52a1\u4e4b\u95f4\u901a\u8baf\u6570\u636e\u7684\u4e00\u81f4\u6027\u5c24\u4e3a\u5fc5\u8981\uff0c\u603b\u4e0d\u80fd A \u670d\u52a1\u8bf4\u6cd5\u8bed\uff0cB \u670d\u52a1\u8bf4\u5fb7\u8bed\uff0c\u901a\u8baf\u8fd8\u8981\u53eb\u4e0a\u4e00\u4e2a\u7ffb\u8bd1\u5b98\u3002<\/p>\n<p>\u4f60\u80af\u5b9a\u5e0c\u671b\u770b\u5230\u4e00\u4e2a\u6570\u636e\u5728 A \u670d\u52a1\u5e8f\u5217\u5316\u5b8c\u6bd5\u4e4b\u540e\uff0c\u5728 B \u670d\u52a1\u80fd\u591f\u987a\u5229\u7684\u53cd\u5e8f\u5217\u5316\u6210\u76ee\u6807\u5bf9\u8c61\u3002<\/p>\n<p>\u8fd9\u4ec5\u4ec5\u662f\u670d\u52a1\u4e0e\u670d\u52a1\u4e4b\u95f4\uff0c\u8fd8\u6709\u5185\u5b58\u4e0e redis \u4e4b\u95f4\uff0c\u8fd8\u6709\u5185\u5b58 &gt; spring security &gt; redis\uff0c\u8fd8\u6709\u5185\u5b58 &gt; redis &gt; rpc &gt; \u5185\u5b58\uff0c\u7b49\u7b49\u60c5\u51b5\u3002<\/p>\n<p>\u6240\u4ee5\u8bf7\u610f\u8bc6\u5230\u5e8f\u5217\u5316\u4e00\u81f4\u6027\u7684\u91cd\u8981\u6027\uff0c\u4e0d\u8981\u7ed9\u5f00\u53d1\u589e\u52a0\u591a\u4f59\u7684\u8d1f\u62c5\u3002<\/p>\n<p>\u4e3a\u4e86\u505a\u5230\u4e00\u81f4\u6027\uff0c\u6846\u67b6\u5185\u7684\u4e09\u4e2a\u7c7b\u5c31\u80fd\u89e3\u51b3\u5927\u591a\u6570\u95ee\u9898<\/p>\n<ol>\n<li><code>framework:cn.framework.config.jackson.JacksonConfig<\/code><\/li>\n<li><code>framework:cn.framework.config.jackson.RedisSessionConfig<\/code><\/li>\n<li><code>framework:cn.framework.config.redisson.RedissonConfig<\/code><\/li>\n<\/ol>\n<h3>3.1 JacksonConfig<\/h3>\n<p><code>JacksonConfig<\/code> \u5229\u7528\u4e86 spring boot \u6846\u67b6\u4e2d\u7684 <code>Jackson2ObjectMapperBuilder<\/code>\uff0c\u7528\u8fc7 jackson \u7684\u90fd\u77e5\u9053\uff0c\u4f7f\u7528 jackson \u9700\u8981\u751f\u6210 ObjectMapper\uff0c\u8fd9\u4e2a\u7c7b\u5c31\u662f\u5e2e\u52a9\u751f\u6210\u7684\u5de5\u5382\u7c7b\u3002<\/p>\n<p>spring cloud gateway \u521a\u597d\u652f\u6301<code>Jackson2ObjectMapperBuilder<\/code>\uff0c\u6240\u4ee5\u8282\u7701\u4e86\u4e00\u90e8\u5206\u4ee3\u7801\u3002<\/p>\n<p>\u5728\u8fd9\u4e2a\u7c7b\u91cc\u9762\uff0c\u8fd8\u914d\u7f6e\u4e86 <code>spring.jackson.date-format<\/code>\uff0c\u4f5c\u4e3a\u7edf\u4e00\u7684\u65f6\u95f4\u683c\u5f0f\u914d\u7f6e\uff0c\u9ed8\u8ba4\u4e3a <code>yyyy-MM-dd HH:mm:ss<\/code>\u3002<\/p>\n<p>\u9488\u5bf9\u65f6\u95f4\u7684\u5904\u7406\uff0c\u5728\u5904\u7406 elasticsearch \u7684\u591a\u65f6\u95f4\u683c\u5f0f\u652f\u6301\u542f\u53d1\uff0c\u8fd8\u5f15\u7533\u51fa\u4e86\u591a\u65f6\u95f4\u683c\u5f0f\u5904\u7406\u7c7b <code>MultipleLocalDateTimeSerializer<\/code>\u3002<\/p>\n<p>\u5b83\u7684\u4f5c\u7528\u662f\u80fd\u591f\u914d\u7f6e\u591a\u4e2a\u65f6\u95f4\u683c\u5f0f\uff0c\u80fd\u591f\u5c06\u65f6\u95f4\u683c\u5f0f\u7684\u5b57\u7b26\u4e32\uff0c\u5bf9\u683c\u5f0f\u8fdb\u884c\u89e3\u6790\uff0c\u5982\u679c\u7b2c\u4e00\u4e2a\u683c\u5f0f\u5931\u8d25\u4e86\uff0c\u5c31\u5c1d\u8bd5\u4e0b\u4e00\u4e2a\u3002<\/p>\n<blockquote><p>\u5982\u679c\u6709\u4ec0\u4e48\u7279\u6b8a\u7684\u7c7b\u9700\u8981\u505a\u53cd\u5e8f\u5217\u5316\u914d\u7f6e\uff0c\u53ef\u4ee5\u5728\u8fd9\u91cc\u589e\u52a0\u3002<\/p><\/blockquote>\n<h3>3.2 RedisSessionConfig<\/h3>\n<p><code>RedisSessionConfig<\/code> \u914d\u7f6e\u4e86 spring security \u4fdd\u5b58 session \u5230 redis \u7684\u5e8f\u5217\u5316\u7c7b <code>RedisSerializer<\/code>\uff0c\u6240\u7528\u5230\u7684 ObjectMapper \u4e5f\u662f\u6765\u81ea\u4e8e <code>JacksonConfig<\/code> \u914d\u7f6e\u7684 <code>Jackson2ObjectMapperBuilder<\/code> \u751f\u6210\u7684\u3002<\/p>\n<p>\u5728\u8fd9\u91cc\u4f60\u53ef\u4ee5\u770b\u5230\u4f7f\u7528\u4e86 <code>SecurityJackson2Modules<\/code> \u8fd9\u4e2a\u7c7b\uff0c\u8fd9\u662f spring security \u9ed8\u8ba4\u63d0\u4f9b\u7684\uff0c\u652f\u6301\u5c06 spring security \u4e2d\u7684\u4e00\u4e9b\u5b89\u5168\u7c7b\u53cd\u5e8f\u5217\u5316\u7684\u6a21\u5757\uff0c\u5f88\u65b9\u4fbf\u3002<\/p>\n<p>\u5728 <code>RedisSessionConfig<\/code> \u4e5f\u6ce8\u518c\u4e86 <code>UserDeserializer<\/code> \u8fd9\u4e2a\u53cd\u5e8f\u5217\u5316\u7c7b\uff0c\u53cd\u5e8f\u5217\u5316\u4e86 <code>User<\/code>\uff0c\u6269\u5c55\u81ea spring security User \u7c7b\uff0c\u53c2\u8003\u81ea <code>org.springframework.security.jackson2.UserDeserializer<\/code>\uff0c\u5982\u679c\u8fd8\u9700\u8981\u6269\u5c55\u7528\u6237\u5c5e\u6027\uff0c\u5728 <code>User<\/code> \u4e0a\u6269\u5c55\uff0c\u5e76\u4e14\u5728 <code>UserDeserializer<\/code> \u4e2d\u505a\u76f8\u5e94\u7684\u8bbe\u7f6e\u5373\u53ef\u3002<\/p>\n<h3>3.3 RedissonConfig<\/h3>\n<p>\u6b64\u7c7b\u662f\u5bf9 redisson \u7684\u914d\u7f6e\u7c7b\u3002<\/p>\n<p>\u5bf9\u5e8f\u5217\u5316\u7684\u914d\u7f6e\u662f\u8fd9\u4e00\u884c\u4ee3\u7801 <code>Codec codecIns = new JsonJacksonCodec(jackson2ObjectMapperBuilder.build());<\/code>\uff0c\u76ee\u7684\u662f\u4f7f\u7528\u7cfb\u7edf\u4e2d\u914d\u7f6e\u7684 ObjectMapper \u8fdb\u884c\u5e8f\u5217\u5316\u7684\u64cd\u4f5c\uff0c\u8fd9\u6837\u5c31\u80fd\u591f\u4fdd\u6301\u7edf\u4e00\u3002<\/p>\n<h2>4 \u5e8f\u5217\u5316\u6cdb\u578b<\/h2>\n<p>\u7531\u4e8e java \u4e2d\u6cdb\u578b\u64e6\u9664\u7684\u95ee\u9898\u5b58\u5728\uff0c\u5728\u5904\u7406\u5d4c\u5957\u7684\u590d\u6742\u7684\u7c7b\u578b\u5bf9\u8c61\u65f6\uff0c\u4e00\u822c\u7684\u624b\u6bb5\u90fd\u4f1a\u5931\u6548\u3002<\/p>\n<p>\u4f8b\u5982\u4e0b\u9762\u8fd9\u6bb5\u4ee3\u7801\uff0c\u5982\u679c\u5c06 test \u8f6c\u6362\u4e3a json string\uff0c\u518d\u8f6c\u6362\u56de <code>List&lt;Map&lt;String, User&gt;&gt;<\/code> \u5c31\u4f1a\u9047\u5230\u64e6\u9664\u7684\u95ee\u9898\u3002<\/p>\n<pre><code class=\"kotlin\">    data class User(\n        val name: String,\n        val age: Int\n    )\n    val test = mutableListOf&lt;Map&lt;String, User&gt;&gt;()\n    test.add(mapOf(&quot;a&quot; to User(&quot;a&quot;, 10)))<\/code><\/pre>\n<p>\u5bf9\u6b64 fastjson \u548c jackson\uff0c\u90fd\u6709\u7740\u76f8\u4f3c\u7684\u89e3\u51b3\u65b9\u6cd5\uff0c\u90a3\u5c31\u662f\u5229\u7528 <code>TypeReference<\/code> \u6765\u8ba9\u6cdb\u578b\u5728\u751f\u6210\u65f6\u5c31\u88ab\u56fa\u5b9a\u4e0b\u6765\uff0c\u6846\u67b6\u4e2d\u5c01\u88c5\u7684 Jackson \u63d0\u4f9b\u4e86\u8fd9\u79cd\u65b9\u6cd5\u3002<\/p>\n<pre><code class=\"kotlin\">Jackson.parseJavaObject(test.jsonString(), object : TypeReference&lt;List&lt;Map&lt;String, User&gt;&gt;&gt;() {})<\/code><\/pre>\n<p>\u53ea\u80fd\u901a\u8fc7\u8fd9\u79cd\u62bd\u8c61\u7c7b\u7684\u65b9\u5f0f\uff0c\u5728\u751f\u6210\u65f6\u786e\u5b9a\u6cdb\u578b\u7684\u7c7b\u578b\u3002<\/p>\n<p>\u4f46\u662f jackson \u9664\u6b64\u4e4b\u5916\uff0c\u8fd8\u63d0\u4f9b\u4e86\u4e00\u79cd\u66f4\u52a0\u7075\u6d3b\u7684\u65b9\u5f0f\uff0c<code>JavaType<\/code><\/p>\n<pre><code class=\"kotlin\">val mapType: JavaType = TypeFactory.defaultInstance().constructParametricType(Map::class.java, String::class.java, User::class.java)\n    val type: JavaType = TypeFactory.defaultInstance().constructParametricType(List::class.java, mapType)\n    Jackson.parseJavaObject(test.jsonString(), type)<\/code><\/pre>\n<p>\u5b83\u80fd\u591f\u8ba9\u4f60\u52a8\u6001\u751f\u6210\u590d\u6742\u6cdb\u578b\u7c7b\u578b\uff0c\u8fd9\u4e5f\u662f jackson \u6bd4 fastjson \u5f3a\u5927\u7684\u5730\u65b9\u4e4b\u4e00\u3002<\/p>\n<p>\u5982\u4eca\u7531\u4e8e\u4f7f\u7528\u4e86 kotlin\uff0c\u6cdb\u578b\u6709\u4e86\u66f4\u597d\u7684\u5904\u7406\u65b9\u5f0f\u3002<\/p>\n<p>\u5728 <code>Jackson<\/code> \u7c7b\u4e2d\uff0c\u6269\u5c55\u4e86 <code>convert<\/code> \u7684\u65b9\u6cd5\uff0c\u8be5\u65b9\u6cd5\u5229\u7528\u4e86 kotlin \u7684 <code>reified<\/code> \u5173\u952e\u5b57\uff0c\u6765\u5904\u7406\u6cdb\u578b\u64e6\u9664\u7684\u95ee\u9898<\/p>\n<pre><code class=\"kotlin\">inline fun &lt;reified T&gt; Any.convert() = Jackson.convert(this, object : TypeReference&lt;T&gt;() {})\n\nfun &lt;T&gt; convert(value: Any, typeReference: TypeReference&lt;T&gt;): T {\n            if (value is String) {\n                return OBJECT_MAPPER.readValue(value, typeReference)\n            }\n            return OBJECT_MAPPER.convertValue(value, typeReference)\n        }<\/code><\/pre>\n<p>\u8fd9\u4e2a\u65b9\u6cd5\u80fd\u591f\u6781\u5927\u7684\u65b9\u4fbf\u6cdb\u578b\u5904\u7406\uff0c\u4eca\u540e\u7684\u7c7b\u578b\u8f6c\u6362\uff0c\u53ea\u9700\u8981\u4e00\u884c\u7b80\u5355\u7684\u4ee3\u7801<\/p>\n<pre><code class=\"kotlin\">val result: List&lt;Map&lt;String, User&gt;&gt; = test.jsonString().convert()<\/code><\/pre>\n<blockquote><p>\u672c\u6587\u53c2\u4e0e\u4e86<a href=\"https:\/\/segmentfault.com\/a\/1190000042453662\">\u601d\u5426\u6280\u672f\u5f81\u6587<\/a>\uff0c\u6b22\u8fce\u6b63\u5728\u9605\u8bfb\u7684\u4f60\u4e5f\u52a0\u5165\u3002<\/p><\/blockquote>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000042510056\">https:\/\/segmentfault.com\/a\/1190000042510056<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6e90\u7801\u5730\u5740 1 \u5148\u8bf4\u7ed3\u8bba \u5728\u8be5\u6846\u67b6\u4e2d\uff0c\u4e0d\u518d\u4f7f\u7528 fastjson \u4f5c\u4e3a\u5e8f\u5217\u5316\u5de5\u5177\uff0c\u5168\u9762\u4f7f\u7528 jackson\u3002 \u4f5c [&hellip;]<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[],"tags":[],"class_list":["post-10006520","post","type-post","status-publish","format-standard","hentry"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10006520","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"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10006520"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10006520\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10006520"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10006520"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10006520"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}