{"id":317258,"date":"2021-02-04T15:23:30","date_gmt":"2021-02-04T07:23:30","guid":{"rendered":"http:\/\/4563.org\/?p=317258"},"modified":"2021-02-04T15:23:30","modified_gmt":"2021-02-04T07:23:30","slug":"spring-boot-2-4-1-restcontroller-%e8%bf%94%e5%9b%9e%e7%9a%84-json-%e6%a0%bc%e5%bc%8f%e4%b8%8d%e5%af%b9","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=317258","title":{"rendered":"Spring boot 2.4.1 RestController \u8fd4\u56de\u7684 JSON \u683c\u5f0f\u4e0d\u5bf9"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  Spring boot 2.4.1 RestController \u8fd4\u56de\u7684 JSON \u683c\u5f0f\u4e0d\u5bf9               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : NULL2020 <\/span>  <span><i><\/i> 10<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>\u81ea\u5b9a\u4e49\u7684\u8fd4\u56de\u7ed3\u6784\u4f53\uff1a<\/p>\n<pre><code>public class JsonResult&lt;T&gt; {     private int code;     private String message;     private T data; } <\/code><\/pre>\n<p>\u4e4b\u524d\u4f7f\u7528 Springboot 2.1.4\uff0c\u63a5\u53e3\u8fd4\u56de\u7684\u683c\u5f0f\u4e3a<\/p>\n<pre><code>{   \"code\": 200,   \"message\": \"\u64cd\u4f5c\u6210\u529f\",   \"data\": null } <\/code><\/pre>\n<p>\u6700\u8fd1\u8003\u8651\u5347\u7ea7\u4e0b\u7248\u672c\uff0c\u6d4b\u8bd5 Springboot 2.4.1\uff0c\u8fd4\u56de\u683c\u5f0f\u4e3a\uff1a<\/p>\n<pre><code>[   \"com.xxx.support.JsonResult\",   {     \"code\": 200,     \"message\": \"\u64cd\u4f5c\u6210\u529f\",     \"data\": [       \"java.util.ArrayList\",       []     ]   } ] <\/code><\/pre>\n<p>\u662f\u8981\u4fee\u6539\u4ec0\u4e48\u914d\u7f6e\u5417\uff1f\u770b release note \u4e5f\u6ca1\u770b\u5230\u6709\u76f8\u5173\u7684\u8bf4\u660e\u3002<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>10<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"5018736\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : chendy <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u76ee\u6d4b\u81ea\u5b9a\u4e49\u4e86\u5e8f\u5217\u5316\u4e4b\u7c7b\u7684\uff0c\u68c0\u67e5\u4e00\u4e0b\u76f8\u5173\u4ee3\u7801\u5427\uff0c\u9ed8\u8ba4\u914d\u7f6e\u4e0d\u662f\u8fd9\u6837\u7684                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5018737\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : JamesMackerel <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u8fd9\u4e2a\u770b\u4e0a\u53bb\u50cf\u662f Jackson \u7684 DefaultTyping\uff0cSpring \u9ed8\u8ba4\u4f7f\u7528 Jackson \u505a\u5e8f\u5217\u5316\u548c\u53cd\u5e8f\u5217\u5316\uff0c\u5efa\u8bae\u7528\u8fd9\u4e9b\u5173\u952e\u5b57\u67e5\u4e00\u4e0b\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5018738\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : arthas2234 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u6211\u4e4b\u524d\u4e5f\u9047\u5230\u8fc7\u8fd9\u4e2a\u95ee\u9898<br \/>\u540e\u9762\u901a\u8fc7 git \u5bf9\u6bd4\u53d1\u73b0\u662f\u56e0\u4e3a\u81ea\u5df1\u6ce8\u5165\u4e86 ObjectMapper \uff08 com.fasterxml.jackson.databind.ObjectMapper \uff09\u7c7b\u5f15\u8d77\u7684\uff0c\u73b0\u5728\u6539\u6210\u901a\u8fc7 new \u6765\u521b\u5efa<br \/>\u5bfc\u81f4\u95ee\u9898\u7684\u6839\u672c\u539f\u56e0\u8fd8\u6ca1\u627e\u5230\uff0c\u73b0\u5728\u6682\u65f6\u8fd8\u6ca1\u65f6\u95f4\u6392\u67e5<br \/>\u53ef\u80fd\u662f\u56e0\u4e3a\u81ea\u5df1\u6ce8\u5165\u540e\u8986\u76d6\u4e86 spring boot \u7684\u9ed8\u8ba4\u914d\u7f6e                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5018739\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : 0x666666 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u770b\u6837\u5b50 \u4f60\u8fd9\u662f\u505a\u4e86\u5e8f\u5217\u5316\u5427\uff1f\u68c0\u67e5\u4f60\u7684\u4ee3\u7801\u91cc\u9762\u6709\u6ca1\u6709\u7edf\u4e00\u53c2\u6570\u8fd4\u56de\u914d\u7f6e\u505a\u4e86\u5e8f\u5217\u5316\u7684\u4e1c\u897f\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5018740\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : NULL2020 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @chendy #1 <br \/>@JamesMackerel #2 <br \/>@arthas2234 #3 <br \/>@0x666666 #4 <\/p>\n<p>\u4e24\u4e2a\u7248\u672c\u90fd\u914d\u7f6e\u4e86 ObjectMapper bean\uff0c\u914d\u7f6e\u4e00\u6837\uff0c\u6d4b\u8bd5\u8fc7\u628a bean \u6ce8\u91ca\u6389\uff0c\u6548\u679c\u4e00\u6837\uff0c\u5373\u65e7\u7248\u672c\u4ecd\u7136\u662f\u6b63\u5e38\u7684\uff0c\u65b0\u7248\u672c\u4ecd\u7136\u662f\u4e0d\u6b63\u5e38\uff0c\u5c31\u5f88\u5947\u602a\u3002                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5018741\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : jorneyr <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u4ece Spring Boot starter \u521b\u5efa\u7684\u7f51\u5740 http:\/\/start.spring.io \u4e0b\u8f7d\u4e86\u6700\u5c0f\u7684 web \u5305\uff0c\u53ea\u5199\u4e00\u4e2a\u6700\u7b80\u5355\u7684 Controller \u8fd4\u56de\u4e00\u4e2a\u666e\u901a\u5bf9\u8c61\uff0c\u5e8f\u5217\u5316\u6ca1\u95ee\u9898\uff0c\u5e94\u8be5\u662f\u4f60\u7684\u914d\u7f6e\u95ee\u9898\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5018742\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : NULL2020 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @jorneyr #6 \u6211\u4e5f\u8bd5\u4e86\u4e0b\uff0c\u771f\u662f\u6b63\u5e38\u7684\uff0c\u4f46\u6211\u4e5f\u6ca1\u914d\u7f6e\u5565\u554a\u3002\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5018743\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : arthas2234 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @NULL2020 \u516b\u6210\u662f\u914d\u7f6e\u54ea\u91cc\u51b2\u7a81\u4e86\uff0c\u6211\u4e4b\u524d\u4e5f\u641e\u4e86\u597d\u4e45\uff0c\u53ea\u80fd\u7528 git \u6bd4\u8f83\u4ee3\u7801\uff0c\u4e00\u4e2a\u4e00\u4e2a\u7684\u6392\u67e5                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5018744\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : NULL2020 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @arthas2234 #8 \u627e\u5230\u539f\u56e0\u4e86\uff0c\u770b\u9644\u8a00                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5018745\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : arthas2234 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @NULL2020 \u54c8\u54c8\uff0c\u89e3\u51b3\u4e86\u5c31\u597d\u3002\u4f60\u7684\u95ee\u9898\u548c\u6211\u7684\u5dee\u4e0d\u591a\uff0c\u4e5f\u662f\u86ee\u8be1\u5f02\u7684\uff0c\u540e\u9762\u6211\u627e\u4e00\u4e0b\u5177\u4f53\u7684\u539f\u56e0                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>Spring boot 2.4.1&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/317258"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=317258"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/317258\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=317258"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=317258"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=317258"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}