{"id":201068,"date":"2025-05-06T11:07:03","date_gmt":"2025-05-06T03:07:03","guid":{"rendered":"https:\/\/server.hk\/cnblog\/201068\/"},"modified":"2025-05-06T11:07:03","modified_gmt":"2025-05-06T03:07:03","slug":"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8-spring-boot-%e5%92%8c-jackson-%e5%9c%a8%e4%b8%8d%e4%bf%ae%e6%94%b9%e6%95%b0%e6%8d%ae%e5%ba%93%e7%9a%84%e6%83%85%e5%86%b5%e4%b8%8b%ef%bc%8c%e5%ae%9e%e7%8e%b0","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/201068\/","title":{"rendered":"\u5982\u4f55\u4f7f\u7528 Spring Boot \u548c Jackson \u5728\u4e0d\u4fee\u6539\u6570\u636e\u5e93\u7684\u60c5\u51b5\u4e0b\uff0c\u5b9e\u73b0\u4e0d\u540c\u56fd\u5bb6\/\u5730\u533a\u5ba2\u6237\u7aef\u8bbf\u95ee\u540c\u4e00\u6570\u636e\u5e93\u65f6\uff0c\u8fd4\u56de\u4e0d\u540c\u65f6\u533a\u65f6\u95f4\uff1f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>\u5982\u4f55\u4f7f\u7528 Spring Boot \u548c Jackson \u5728\u4e0d\u4fee\u6539\u6570\u636e\u5e93\u7684\u60c5\u51b5\u4e0b\uff0c\u5b9e\u73b0\u4e0d\u540c\u56fd\u5bb6\/\u5730\u533a\u5ba2\u6237\u7aef\u8bbf\u95ee\u540c\u4e00\u6570\u636e\u5e93\u65f6\uff0c\u8fd4\u56de\u4e0d\u540c\u65f6\u533a\u65f6\u95f4\uff1f<\/h1>\n<p>\u5927\u5bb6\u597d\uff0c\u6211\u4eec\u53c8\u89c1\u9762\u4e86\u554a~\u672c\u6587\u7684\u5185\u5bb9\u4e2d\u5c06\u4f1a\u6d89\u53ca\u5230\u7b49\u7b49\u3002\u5982\u679c\u4f60\u6b63\u5728\u5b66\u4e60\u76f8\u5173\u77e5\u8bc6\uff0c\u6b22\u8fce\u5173\u6ce8\u6211\uff0c\u4ee5\u540e\u4f1a\u7ed9\u5927\u5bb6\u5e26\u6765\u66f4\u591a\u76f8\u5173\u6587\u7ae0\uff0c\u5e0c\u671b\u6211\u4eec\u80fd\u4e00\u8d77\u8fdb\u6b65\uff01\u4e0b\u9762\u5c31\u5f00\u59cb\u672c\u6587\u7684\u6b63\u5f0f\u5185\u5bb9~<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241129\/1732854264674941f876f8a.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>\u5982\u4f55\u6307\u5b9a mysql \u6570\u636e\u5e93 datetime \u6570\u636e\u7684\u663e\u793a\u65f6\u533a<\/strong><\/p>\n<p>\u5f53\u4f7f\u7528 java \u8bed\u8a00\u548c spring boot \u6846\u67b6\u4e0e mysql \u6570\u636e\u5e93\u4ea4\u4e92\u65f6\uff0cdatetime \u6570\u636e\u7c7b\u578b\u7684\u9ed8\u8ba4\u65f6\u533a\u4f1a\u8f6c\u6362\u4e3a\u5e94\u7528\u7a0b\u5e8f\u670d\u52a1\u5668\u7684\u65f6\u533a\u3002\u5728\u90e8\u7f72\u573a\u666f\u4e2d\uff0c\u9700\u8981\u9488\u5bf9\u4e0d\u540c\u65f6\u533a\u7684\u5ba2\u6237\u7aef\u8fdb\u884c\u76f8\u5e94\u7684\u65f6\u533a\u8c03\u6574\u3002<\/p>\n<p><strong>\u95ee\u9898\uff1a<\/strong><\/p>\n<p>\u5df2\u6709\u4e00\u6b3e\u90e8\u7f72\u5728\u4e1c\u516b\u533a\u7684 spring boot \u5e94\u7528\uff0c\u9700\u8981\u4e3a\u5370\u5ea6\uff08\u4e1c\u4e94\u533a\uff09\u548c\u8d8a\u5357\uff08\u4e1c\u4e03\u533a\uff09\u4e24\u56fd\u7684\u5ba2\u6237\u7aef\u63d0\u4f9b\u7edf\u4e00\u7684\u8bbf\u95ee\u3002\u5e94\u7528\u7a0b\u5e8f\u5df2\u91c7\u7528\u591a\u6570\u636e\u6e90\u6a21\u5f0f\uff0c\u4e0d\u540c\u56fd\u5bb6\u7684\u6570\u636e\u5e93\u5728\u6570\u636e\u5e93\u7ed3\u6784\u4e0a\u5b8c\u5168\u76f8\u540c\uff0c\u5b57\u6bb5\u7c7b\u578b\u5747\u4e3a datetime\u3002\u76ee\u6807\u662f\u5b9e\u73b0\u4ee5\u4e0b\u9700\u6c42\uff1a<\/p>\n<ul>\n<li>\u5370\u5ea6\u7528\u6237\u67e5\u8be2\u65f6\uff0c\u8fd4\u56de\u4e1c\u4e94\u533a\u7684\u65f6\u95f4\u3002<\/li>\n<li>\u8d8a\u5357\u7528\u6237\u67e5\u8be2\u65f6\uff0c\u8fd4\u56de\u4e1c\u4e03\u533a\u7684\u65f6\u95f4\u3002<\/li>\n<\/ul>\n<p><strong>\u9650\u5236\u6761\u4ef6\uff1a<\/strong><\/p>\n<ul>\n<li>\u5e94\u7528\u7a0b\u5e8f\u4ee3\u7801\u56fa\u5b9a\uff0c\u65e0\u6cd5\u8fdb\u884c\u5927\u8303\u56f4\u7684\u4fee\u6539\u3002<\/li>\n<li>\u7531\u4e8e\u4e1a\u52a1\u903b\u8f91\u7684\u590d\u6742\u6027\uff0c\u65e0\u6cd5\u5168\u5c40\u914d\u7f6e spring.jackson.time-zone\u3002<\/li>\n<\/ul>\n<p><strong>\u89e3\u51b3\u601d\u8def\uff1a<\/strong><\/p>\n<p>\u7531\u4e8e\u6570\u636e\u5e93\u5b57\u6bb5\u7c7b\u578b\u65e0\u6cd5\u4fee\u6539\uff0c\u56e0\u6b64\u9700\u8981\u4ece controller \u5c42\u8fdb\u884c\u65f6\u533a\u8c03\u6574\u3002controller \u4e2d\u53ef\u4ee5\u6839\u636e session \u4fe1\u606f\u5224\u65ad\u51fa\u7528\u6237\u7684\u6240\u5c5e\u56fd\u5bb6\/\u5730\u533a\uff0c\u518d\u901a\u8fc7\u9002\u5f53\u7684\u65f6\u533a\u8f6c\u6362\u64cd\u4f5c\u5b9e\u73b0\u9700\u6c42\u3002<\/p>\n<p><strong>\u5177\u4f53\u6b65\u9aa4\uff1a<\/strong><\/p>\n<p><strong>1. \u521b\u5efa jackson2objectmapperbuilder bean<\/strong><\/p>\n<pre>@bean\n@primary\npublic jackson2objectmapperbuilder jackson2objectmapperbuilder() {\n    return new jackson2objectmapperbuilder().serializerbytype(date.class, new dateserializer());\n}<\/pre>\n<p><strong>2. \u81ea\u5b9a\u4e49 date \u5e8f\u5217\u5316\u5668 dateserializer<\/strong><\/p>\n<pre>public class dateserializer extends stdserializer&lt;date&gt; {\n\n    protected dateserializer() {\n        super(date.class);\n    }\n\n    @override\n    public void serialize(date value, jsongenerator gen, serializerprovider provider) throws ioexception {\n        \/\/ \u6839\u636e session \u4fe1\u606f\u83b7\u53d6\u76ee\u6807\u65f6\u533a\n        timezone targettimezone = gettargettimezone();\n\n        \/\/ \u5c06 date \u5bf9\u8c61\u8f6c\u6362\u4e3a\u6307\u5b9a\u65f6\u533a\u7684\u5b57\u7b26\u4e32\n        gen.writestring(new simpledateformat(\"yyyy-mm-dd hh:mm:ss\").format(value));\n    }\n}<\/pre>\n<p><strong>3. \u83b7\u53d6\u76ee\u6807\u65f6\u533a<\/strong><\/p>\n<pre>private TimeZone getTargetTimeZone() {\n    \/\/ \u6839\u636e Session \u4fe1\u606f\u5224\u65ad\u7528\u6237\u6240\u5c5e\u56fd\u5bb6\/\u5730\u533a\uff0c\u5e76\u8fd4\u56de\u5bf9\u5e94\u7684\u65f6\u533a\n    if (isIndia()) {\n        return TimeZone.getTimeZone(\"Asia\/Kolkata\");\n    } else if (isVietnam()) {\n        return TimeZone.getTimeZone(\"Asia\/Ho_Chi_Minh\");\n    }\n\n    \/\/ \u9ed8\u8ba4\u8fd4\u56de\u670d\u52a1\u5668\u65f6\u533a\n    return TimeZone.getDefault();\n}<\/pre>\n<p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\uff0c\u53ef\u4ee5\u5728\u4e0d\u4fee\u6539\u6570\u636e\u5e93\u5b57\u6bb5\u7c7b\u578b\u7684\u60c5\u51b5\u4e0b\uff0c\u5b9e\u73b0\u4e0d\u540c\u56fd\u5bb6\/\u5730\u533a\u5ba2\u6237\u7aef\u8bbf\u95ee\u540c\u4e00\u6570\u636e\u5e93\u4e0d\u540c\u65f6\u533a\u65f6\u95f4\u7684\u9700\u6c42\u3002<\/p>\n<p>\u4eca\u5929\u5173\u4e8e\u300a\u5982\u4f55\u4f7f\u7528 Spring Boot \u548c Jackson \u5728\u4e0d\u4fee\u6539\u6570\u636e\u5e93\u7684\u60c5\u51b5\u4e0b\uff0c\u5b9e\u73b0\u4e0d\u540c\u56fd\u5bb6\/\u5730\u533a\u5ba2\u6237\u7aef\u8bbf\u95ee\u540c\u4e00\u6570\u636e\u5e93\u65f6\uff0c\u8fd4\u56de\u4e0d\u540c\u65f6\u533a\u65f6\u95f4\uff1f\u300b\u7684\u5185\u5bb9\u5c31\u4ecb\u7ecd\u5230\u8fd9\u91cc\u4e86\uff0c\u662f\u4e0d\u662f\u5b66\u8d77\u6765\u4e00\u76ee\u4e86\u7136\uff01\u60f3\u8981\u4e86\u89e3\u66f4\u591a\u5173\u4e8e\u7684\u5185\u5bb9\u8bf7\u5173\u6ce8\u4e3b\u673a\u5b9d\u8d1d\u516c\u4f17\u53f7\uff01<\/p>\n<dl>\n<dt>\n <\/dt>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u4f7f\u7528 Spring Boot&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-201068","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/201068","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=201068"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/201068\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=201068"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=201068"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=201068"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}