{"id":2795,"date":"2020-06-22T08:29:29","date_gmt":"2020-06-21T23:29:29","guid":{"rendered":"https:\/\/133.242.23.245\/blog\/?p=2795"},"modified":"2020-06-22T08:35:51","modified_gmt":"2020-06-21T23:35:51","slug":"jackson%e3%82%92%e4%bd%bf%e3%81%a3%e3%81%a6%e3%81%bf%e3%82%8b","status":"publish","type":"post","link":"https:\/\/www.kara-kuri.jp\/blog\/?p=2795","title":{"rendered":"Jackson\u3092\u4f7f\u3063\u3066\u307f\u308b"},"content":{"rendered":"\n<p>\u3053\u3093\u306b\u3061\u306f\u3002\u30cd\u30c3\u30c8\u4e0a\u3067\u306f\u3082\u3046\u4f55\u756a\u714e\u3058\u304b\u308f\u304b\u308a\u307e\u305b\u3093\u304c\u3001Jackson\u3092\u4f7f\u7528\u3059\u308b\u6a5f\u4f1a\u304c\u3042\u3063\u305f\u306e\u3067\u3001\u81ea\u5206\u306e\u30ea\u30de\u30a4\u30f3\u30c9\u3092\u517c\u306d\u3066\u6295\u7a3f\u3057\u3066\u307f\u307e\u3059\u3002<br \/>\u3068\u308a\u3042\u3048\u305a\u4eca\u56de\u306fPOJO\/JSON\u9593\u306e\u5909\u63db\u306b\u3064\u3044\u3066\u8ff0\u3079\u305f\u3044\u3068\u601d\u3044\u307e\u3059\u3002\u5229\u7528\u74b0\u5883\u306fSpring Boot + Java11 + Lombok\u3068\u306a\u3063\u3066\u3044\u307e\u3059\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">build.gradle\u306e\u8a2d\u5b9a\uff08\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\uff09<\/h3>\n\n\n\n<p>\u307e\u305a\u4f7f\u7528\u3059\u308b\u305f\u3081\u306b\u3001\u4ee5\u4e0b\u306e\u8a2d\u5b9a\u3092\u884c\u3044\u307e\u3059\u3002<br \/>\u203b\u6700\u65b0\u306e\u30d0\u30fc\u30b8\u30e7\u30f3\u306f\u4ee5\u4e0b\u306e\u30da\u30fc\u30b8\u3067\u8abf\u3079\u3089\u308c\u308b\u304b\u3068\u3001\u3001\u3001\u3002<br \/><a href=\"https:\/\/mvnrepository.com\/artifact\/com.fasterxml.jackson.core\/jackson-databind\">https:\/\/mvnrepository.com\/artifact\/com.fasterxml.jackson.core\/jackson-databind<\/a><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code><strong>dependencies<\/strong> {\n    compile 'com.fasterxml.jackson.core:jackson-databind:2.11.0'\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">POJO\u30af\u30e9\u30b9\u306e\u7528\u610f<\/h3>\n\n\n\n<p>\u3068\u308a\u3042\u3048\u305a\u4ee5\u4e0b\u306e\u611f\u3058\u3067\u7528\u610f\u3057\u307e\u3059\u3002<br \/>@JsonProperty\u306f\u30d7\u30ed\u30d1\u30c6\u30a3\u306e\u540d\u524d\u3092\u5909\u66f4\u3057\u305f\u3044\u5834\u5408\u306b\u4f7f\u7528\u3059\u308b\u3082\u306e\u3067\u3059\u3002\uff08\u5f93\u3063\u3066\u3001\u5909\u6570\u540d\u3068\u30d7\u30ed\u30d1\u30c6\u30a3\u540d\u304c\u540c\u3058\u306b\u306a\u308b&#8221;id&#8221;\u306b\u3064\u3044\u3066\u306f\u672c\u5f53\u306f\u5fc5\u8981\u3067\u306f\u306a\u3044\u306e\u3067\u3059\u304c\u5f62\u3092\u5408\u308f\u305b\u308b\u305f\u3081\u306b\u4ed8\u3051\u3066\u3044\u307e\u3059\u3002\uff09<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">@Data\n@Builder\npublic class Product {\n    @JsonProperty(\"id\")\n    public String id;\n    @JsonProperty(\"productname\")\n    public String name;\n}<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">POJO\u2192JSON\u306e\u5909\u63db<\/h3>\n\n\n\n<p>\u4ee5\u4e0b\u306e\u611f\u3058\u3067\u30b3\u30fc\u30c7\u30a3\u30f3\u30b0\u3057\u3066\u307f\u307e\u3059\u3002<br \/>\u5909\u63db\u3057\u3066\u3044\u308b\u90e8\u5206\u306fmapper.writeValueAsString(product)\u306e\u3068\u3053\u308d\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">public static void main(String[] args) throws IOException {\n    Product product = Product.builder()\n            .id(AAA12345)\n            .name(test)\n            .build();\n\n    ObjectMapper mapper = new ObjectMapper();\n    String json = mapper.writeValueAsString(product);\n    System.out.println(json);\n}<\/pre>\n\n\n\n<p>\u5b9f\u884c\u7d50\u679c\u306f{&#8220;id&#8221;:AAA12345,&#8221;productname&#8221;:&#8221;test&#8221;}<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">JSON\u2192POJO\u306e\u5909\u63db<\/h3>\n\n\n\n<p>\u4ee5\u4e0b\u306e\u611f\u3058\u3067\u30b3\u30fc\u30c7\u30a3\u30f3\u30b0\u3057\u3066\u307f\u307e\u3059\u3002\uff08\u4f55\u3068\u306a\u304f\u5148\u7a0b\u3068\u9006\u306e\u30a4\u30e1\u30fc\u30b8\uff09<br \/>\u5909\u63db\u3057\u3066\u3044\u308b\u90e8\u5206\u306fmapper.readValue(json, Product.class)\u306e\u3068\u3053\u308d\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">public static void main(String[] args) throws IOException {\n    String json = \"{\\\"id\\\":AAAAA12345, \\\"productname\\\":\\\"test\\\"}\";\n\n    ObjectMapper mapper = new ObjectMapper();\n    Product product = mapper.readValue(json, Product.class);\n    System.out.println(product);\n}<\/pre>\n\n\n\n<p>\u5b9f\u884c\u7d50\u679c\u306fProduct [id=AAAAA12345, name=test]<\/p>\n\n\n\n<p>\u3068\u308a\u3042\u3048\u305a\u4ee5\u4e0a\u306b\u306a\u308a\u307e\u3059\u3002<br \/>\u4ed6\u306b\u3082List\u3084Map\u306e\u30af\u30e9\u30b9\u5909\u6570\u3092\u6301\u305f\u305b\u3066\u5909\u63db\u3084NULL\u9805\u76ee\u306e\u975e\u51fa\u529b\u7b49\u3082\u884c\u3048\u307e\u3059\u304c\u3001\u6b21\u56de\u306eXML\u5f62\u5f0f\u3078\u306e\u5909\u63db\u306e\u6642\u306b\u307e\u305f\u8ff0\u3079\u305f\u3044\u3068\u601d\u3044\u307e\u3059\u3002\u305d\u308c\u3067\u306f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u3053\u3093\u306b\u3061\u306f\u3002\u30cd\u30c3\u30c8\u4e0a\u3067\u306f\u3082\u3046\u4f55\u756a\u714e\u3058\u304b\u308f\u304b\u308a\u307e\u305b\u3093\u304c\u3001Jackson\u3092\u4f7f\u7528\u3059\u308b\u6a5f\u4f1a\u304c\u3042\u3063\u305f\u306e\u3067\u3001\u81ea\u5206\u306e\u30ea\u30de\u30a4\u30f3\u30c9\u3092\u517c\u306d\u3066\u6295\u7a3f\u3057\u3066\u307f\u307e\u3059\u3002\u3068\u308a\u3042\u3048\u305a\u4eca\u56de\u306fPOJO\/JSON\u9593\u306e\u5909\u63db\u306b\u3064\u3044\u3066\u8ff0\u3079\u305f\u3044\u3068\u601d\u3044\u307e\u3059\u3002\u5229\u7528\u74b0\u5883\u306fSp [&hellip;]<\/p>\n","protected":false},"author":10,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[50,49,52],"_links":{"self":[{"href":"https:\/\/www.kara-kuri.jp\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2795"}],"collection":[{"href":"https:\/\/www.kara-kuri.jp\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kara-kuri.jp\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kara-kuri.jp\/blog\/index.php?rest_route=\/wp\/v2\/users\/10"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kara-kuri.jp\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2795"}],"version-history":[{"count":5,"href":"https:\/\/www.kara-kuri.jp\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2795\/revisions"}],"predecessor-version":[{"id":2849,"href":"https:\/\/www.kara-kuri.jp\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2795\/revisions\/2849"}],"wp:attachment":[{"href":"https:\/\/www.kara-kuri.jp\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2795"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kara-kuri.jp\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2795"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kara-kuri.jp\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2795"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}