{"id":5893,"date":"2022-05-12T14:29:48","date_gmt":"2022-05-12T06:29:48","guid":{"rendered":"http:\/\/123.57.164.21\/?p=5893"},"modified":"2022-05-12T14:29:48","modified_gmt":"2022-05-12T06:29:48","slug":"springboot-%e5%ae%9e%e7%8e%b0json%e6%95%b0%e6%8d%ae%e7%9a%84%e8%bf%94%e5%9b%9e%ef%bc%88%e5%b0%86%e6%a8%a1%e5%9e%8b%e8%bd%ac%e6%88%90json%e5%ad%97%e7%ac%a6%e4%b8%b2%ef%bc%89","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=5893","title":{"rendered":"SpringBoot &#8211; \u5b9e\u73b0JSON\u6570\u636e\u7684\u8fd4\u56de\uff08\u5c06\u6a21\u578b\u8f6c\u6210JSON\u5b57\u7b26\u4e32\uff09"},"content":{"rendered":"\n<p><strong>JSON<\/strong> \u662f\u76ee\u524d\u4e3b\u6d41\u7684\u524d\u540e\u7aef\u6570\u636e\u4f20\u8f93\u65b9\u5f0f\u3002\u5728 <strong>Spring Boot <\/strong>\u9879\u76ee\u4e2d\uff0c\u53ea\u8981\u6dfb\u52a0\u4e86 <strong>Web<\/strong> \u4f9d\u8d56\uff08<strong>spring-boot-starter-web<\/strong>\uff09\uff0c\u5c31\u53ef\u4ee5\u5f88\u65b9\u4fbf\u5730\u5b9e\u73b0 <strong>JSON<\/strong> \u8f6c\u6362\u3002\u4e0b\u9762\u6211\u4eec\u901a\u8fc7\u6837\u4f8b\u8fdb\u884c\u6f14\u793a\u3002<br>Web \u4f9d\u8d56\u9ed8\u8ba4\u52a0\u5165\u4e86 jackson-databind \u4f5c\u4e3a JSON \u5904\u7406\u5668\uff0c\u6211\u4eec\u4e0d\u9700\u8981\u8981\u6dfb\u52a0\u989d\u5916\u7684 JSON \u5904\u7406\u5668\u5c31\u53ef\u4ee5\u8fd4\u56de\u4e00\u6bb5 JSON\u3002<\/p>\n\n\n\n<p><strong>1\uff0c\u5b9e\u4f53\u7c7b\u521b\u5efa<\/strong><\/p>\n\n\n\n<p>\u9996\u5148\u6211\u4eec\u521b\u5efa\u4e00\u4e2a <strong>Book<\/strong> \u5b9e\u4f53\u7c7b\uff0c\u6ce8\u610f\u8fd9\u91cc\u7528\u5230\u4e86\u4e24\u4e2a\u6ce8\u89e3\uff1a<\/p>\n\n\n\n<ul><li>@JsonIgnore\uff1ajson \u5e8f\u5217\u5316\u65f6\u5c06 java bean \u4e2d\u7684\u4e00\u4e9b\u5c5e\u6027\u5ffd\u7565\u6389\u3002\u5373\u751f\u6210 json \u65f6\u4e0d\u751f\u6210\u5176\u6807\u6ce8\u7684\u5c5e\u6027\u3002<\/li><li>@JsonFormat\uff1a\u5c06\u65e5\u671f\u7c7b\u578b\u7684\u6570\u636e\u683c\u5f0f\u5316\u6210\u6307\u5b9a\u683c\u5f0f\u7684\u5b57\u7b26\u4e32\u3002<\/li><\/ul>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">package com.example.demo;\n \nimport com.fasterxml.jackson.annotation.JsonFormat;\nimport com.fasterxml.jackson.annotation.JsonIgnore;\n \nimport java.util.Date;\n \npublic class Book {\n    private String name;\n \n    private String author;\n \n    @JsonIgnore\n    private Float price;\n \n    @JsonFormat(pattern = \"yyyy-MM-dd\")\n    private Date publicationDate;\n \n    public String getName() {\n        return name;\n    }\n \n    public void setName(String name) {\n        this.name = name;\n    }\n \n    public String getAuthor() {\n        return author;\n    }\n \n    public void setAuthor(String author) {\n        this.author = author;\n    }\n \n    public Float getPrice() {\n        return price;\n    }\n \n    public void setPrice(Float price) {\n        this.price = price;\n    }\n \n    public Date getPublicationDate() {\n        return publicationDate;\n    }\n \n    public void setPublicationDate(Date publicationDate) {\n        this.publicationDate = publicationDate;\n    }\n}<\/pre>\n\n\n\n<p><strong>2\uff0c\u521b\u5efa Contoller<\/strong><\/p>\n\n\n\n<p>\u5728\u8fd9\u4e2a <strong>Contoller<\/strong> \u4e2d\u6211\u4eec\u521d\u59cb\u5316\u4e00\u4e2a <strong>Book<\/strong> \u5bf9\u8c61\uff0c\u7136\u540e\u5c06\u5176\u76f4\u63a5\u8fd4\u56de\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">package com.example.demo;\n \nimport org.springframework.web.bind.annotation.RestController;\nimport org.springframework.web.bind.annotation.GetMapping;\nimport java.util.Date;\n \n@RestController\npublic class HelloController {\n    @GetMapping(\"\/hello\")\n    public Book hello() {\n        Book book = new Book();\n        book.setName(\"Spring Boot\u5f00\u53d1\u6307\u5357\");\n        book.setAuthor(\"hangge\");\n        book.setPrice(50f);\n        book.setPublicationDate(new Date());\n        return book;\n    }\n}<\/pre>\n\n\n\n<p><strong>3\uff0c\u8bbf\u95ee\u6d4b\u8bd5<\/strong><\/p>\n\n\n\n<p>\u5728\u6d4f\u89c8\u5668\u8bbf\u95ee <strong>\/hello<\/strong> \u8fd9\u4e2a\u63a5\u53e3\uff0c\u53ef\u4ee5\u770b\u5230 <strong>JSON<\/strong> \u6570\u636e\u5df2\u7ecf\u6210\u529f\u8fd4\u56de\u4e86\u3002<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2022\/05\/\u56fe\u7247-94-1024x209.png\" alt=\"\" class=\"wp-image-5894\" width=\"609\" height=\"124\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2022\/05\/\u56fe\u7247-94-1024x209.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2022\/05\/\u56fe\u7247-94-300x61.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2022\/05\/\u56fe\u7247-94-768x157.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2022\/05\/\u56fe\u7247-94-830x169.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2022\/05\/\u56fe\u7247-94-230x47.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2022\/05\/\u56fe\u7247-94-350x71.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2022\/05\/\u56fe\u7247-94-480x98.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2022\/05\/\u56fe\u7247-94.png 1254w\" sizes=\"(max-width: 609px) 100vw, 609px\" \/><\/figure><\/div>\n\n\n\n<p><br><\/p>\n","protected":false},"excerpt":{"rendered":"<p>JSON \u662f\u76ee\u524d\u4e3b\u6d41\u7684\u524d\u540e\u7aef\u6570\u636e\u4f20\u8f93\u65b9\u5f0f\u3002\u5728 Spring Boot \u9879\u76ee\u4e2d\uff0c\u53ea\u8981\u6dfb\u52a0\u4e86 Web \u4f9d\u8d56\uff08spr [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[27],"tags":[],"_links":{"self":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/5893"}],"collection":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5893"}],"version-history":[{"count":1,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/5893\/revisions"}],"predecessor-version":[{"id":5895,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/5893\/revisions\/5895"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5893"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5893"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5893"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}