{"id":9041,"date":"2022-12-09T16:50:13","date_gmt":"2022-12-09T08:50:13","guid":{"rendered":"http:\/\/123.57.164.21\/?p=9041"},"modified":"2022-12-09T16:50:13","modified_gmt":"2022-12-09T08:50:13","slug":"springboot-httpmessageconverter%e6%b6%88%e6%81%af%e8%bd%ac%e6%8d%a2%e5%99%a8","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=9041","title":{"rendered":"SpringBoot\u00a0HttpMessageConverter\u6d88\u606f\u8f6c\u6362\u5668"},"content":{"rendered":"\n<p>\u5728\u6784\u5efaRESTful\u6570\u636e\u670d\u52a1\u8fc7\u7a0b\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86controller\u3001repositories\uff0c\u5e76\u7528\u4e00\u4e9b\u6ce8\u89e3\u4fee\u9970\u5b83\u4eec\uff0c\u4f46\u662f\u5230\u73b0\u5728\u4e3a\u6b62\u6211\u4eec\u8fd8\u6ca1\u6267\u884c\u8fc7\u5bf9\u8c61\u7684\u8f6c\u6362\u2014\u2014\u5c06java\u5b9e\u4f53\u5bf9\u8c61\u8f6c\u6362\u6210HTTP\u7684\u6570\u636e\u8f93\u51fa\u6d41\u3002Spring Boot\u5e95\u5c42\u901a\u8fc7HttpMessageConverters\u4f9d\u9760Jackson\u5e93\u5c06Java\u5b9e\u4f53\u7c7b\u8f93\u51fa\u4e3aJSON\u683c\u5f0f\u3002\u5f53\u6709\u591a\u4e2a\u8f6c\u6362\u5668\u53ef\u7528\u65f6\uff0c\u6839\u636e\u6d88\u606f\u5bf9\u8c61\u7c7b\u578b\u548c\u9700\u8981\u7684\u5185\u5bb9\u7c7b\u578b\u9009\u62e9\u6700\u9002\u5408\u7684\u8f6c\u6362\u5668\u4f7f\u7528\u3002<\/p>\n\n\n\n<p>\u5728\u6574\u4e2a\u6570\u636e\u6d41\u8f6c\u8fc7\u7a0b\u4e2d\uff0c\u524d\u7aef\u7684\u8bf7\u6c42\u62a5\u6587\u8f6c\u5316\u4e3aJava\u5bf9\u8c61\uff0cJava\u5bf9\u8c61\u8f6c\u5316\u4e3a\u54cd\u5e94\u62a5\u6587\uff0c\u8fd9\u91cc\u5c31\u7528\u5230\u4e86\u6d88\u606f\u8f6c\u6362\u5668HttpMessageConverter<\/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\/12\/\u56fe\u7247-61-1024x307.png\" alt=\"\" class=\"wp-image-9042\" width=\"543\" height=\"162\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-61-1024x307.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-61-300x90.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-61-768x230.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-61-1536x461.png 1536w, https:\/\/92it.top\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-61-830x249.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-61-230x69.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-61-350x105.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-61-480x144.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-61.png 1554w\" sizes=\"(max-width: 543px) 100vw, 543px\" \/><figcaption>\u6d88\u606f\u8f6c\u6362\u5668\u7684\u4f4d\u7f6e<\/figcaption><\/figure><\/div>\n\n\n\n<p><strong>\u6d88\u606f\u8f6c\u5316\u5668\u7684\u4f5c\u7528<\/strong><\/p>\n\n\n\n<ul><li>\u5c06\u8bf7\u6c42\u62a5\u6587\u8f6c\u5316\u4e3aJava\u5bf9\u8c61<\/li><li>\u5c06Java\u5bf9\u8c61\u8f6c\u5316\u4e3a\u54cd\u5e94\u62a5\u6587<\/li><\/ul>\n\n\n\n<p><strong>\u6d88\u606f\u8f6c\u5316\u5668\u7684\u4e3b\u8981\u65b9\u6cd5<\/strong><\/p>\n\n\n\n<ul><li>getSupportedMediaTypes\uff1a\u83b7\u53d6\u652f\u6301\u7684MediaType\u96c6\u5408\uff08\u5982\uff1atext\/html\uff0ctext\/plain\uff0capplication\/json\uff09<\/li><li>canRead\uff1a\u5224\u65ad\u662f\u5426\u80fd\u8bfb\uff08\u8bf7\u6c42\uff09<\/li><li>read\uff1a\u5c06\u8bf7\u6c42\u6570\u636e\u8fdb\u884c\u683c\u5f0f\u8f6c\u6362\uff08canRead\u65b9\u6cd5\u8fd4\u56de\u503c\u4e3atrue\u65f6\u8c03\u7528\uff09<\/li><li>canWrite\uff1a\u5224\u65ad\u662f\u5426\u80fd\u5199\uff08\u54cd\u5e94\uff09<\/li><li>write\uff1a\u5c06\u54cd\u5e94\u6570\u636e\u8fdb\u884c\u683c\u5f0f\u8f6c\u6362\uff08canWrite\u65b9\u6cd5\u8fd4\u56de\u503c\u4e3atrue\u65f6\u8c03\u7528\uff09<\/li><\/ul>\n\n\n\n<p>SpringMVC\u542f\u52a8\u65f6\u4f1a\u81ea\u52a8\u914d\u7f6e\u4e00\u4e9bHttpMessageConverter\uff08WebMvcConfigurationSupport\u7c7b\u7684addDefaultHttpMessageConverters\uff09\u65b9\u6cd5<\/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=\"\">protected final void addDefaultHttpMessageConverters(List&lt;HttpMessageConverter&lt;?>> messageConverters) {\n\u00a0\u00a0\u00a0\u00a0messageConverters.add(new ByteArrayHttpMessageConverter());\n\u00a0\u00a0\u00a0\u00a0messageConverters.add(new StringHttpMessageConverter());\n\u00a0\u00a0\u00a0\u00a0messageConverters.add(new ResourceHttpMessageConverter());\n\u00a0\u00a0\u00a0\u00a0messageConverters.add(new ResourceRegionHttpMessageConverter());\n\u00a0\u00a0\u00a0\u00a0try {\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0messageConverters.add(new SourceHttpMessageConverter&lt;>());\n\u00a0\u00a0\u00a0\u00a0}\n\u00a0\u00a0\u00a0\u00a0catch (Throwable ex) {\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\/\/ Ignore when no TransformerFactory implementation is available...\n\u00a0\u00a0\u00a0\u00a0}\n\u00a0\u00a0\u00a0\u00a0messageConverters.add(new AllEncompassingFormHttpMessageConverter());\n\u00a0\u00a0\u00a0\u00a0if (romePresent) {\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0messageConverters.add(new AtomFeedHttpMessageConverter());\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0messageConverters.add(new RssChannelHttpMessageConverter());\n\u00a0\u00a0\u00a0\u00a0}\n\u00a0\u00a0\u00a0\u00a0if (jackson2XmlPresent) {\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0Jackson2ObjectMapperBuilder builder = Jackson2ObjectMapperBuilder.xml();\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0if (this.applicationContext != null) {\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0builder.applicationContext(this.applicationContext);\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0messageConverters.add(new MappingJackson2XmlHttpMessageConverter(builder.build()));\n\u00a0\u00a0\u00a0\u00a0}\n\u00a0\u00a0\u00a0\u00a0else if (jaxb2Present) {\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0messageConverters.add(new Jaxb2RootElementHttpMessageConverter());\n\u00a0\u00a0\u00a0\u00a0}\n\u00a0\u00a0\u00a0\u00a0if (jackson2Present) {\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0Jackson2ObjectMapperBuilder builder = Jackson2ObjectMapperBuilder.json();\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0if (this.applicationContext != null) {\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0builder.applicationContext(this.applicationContext);\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0messageConverters.add(new MappingJackson2HttpMessageConverter(builder.build()));\n\u00a0\u00a0\u00a0\u00a0}\n\u00a0\u00a0\u00a0\u00a0else if (gsonPresent) {\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0messageConverters.add(new GsonHttpMessageConverter());\n\u00a0\u00a0\u00a0\u00a0}\n\u00a0\u00a0\u00a0\u00a0else if (jsonbPresent) {\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0messageConverters.add(new JsonbHttpMessageConverter());\n\u00a0\u00a0\u00a0\u00a0}\n\u00a0\u00a0\u00a0\u00a0if (jackson2SmilePresent) {\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0Jackson2ObjectMapperBuilder builder = Jackson2ObjectMapperBuilder.smile();\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0if (this.applicationContext != null) {\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0builder.applicationContext(this.applicationContext);\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0messageConverters.add(new MappingJackson2SmileHttpMessageConverter(builder.build()));\n\u00a0\u00a0\u00a0\u00a0}\n\u00a0\u00a0\u00a0\u00a0if (jackson2CborPresent) {\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0Jackson2ObjectMapperBuilder builder = Jackson2ObjectMapperBuilder.cbor();\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0if (this.applicationContext != null) {\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0builder.applicationContext(this.applicationContext);\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0messageConverters.add(new MappingJackson2CborHttpMessageConverter(builder.build()));\n\u00a0\u00a0\u00a0\u00a0}<\/pre>\n\n\n\n<p><strong>\u90e8\u5206\u6d88\u606f\u8f6c\u6362\u5668\u89e3\u6790<\/strong><\/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\/12\/\u56fe\u7247-62-1024x406.png\" alt=\"\" class=\"wp-image-9043\" width=\"554\" height=\"220\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-62-1024x406.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-62-300x119.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-62-768x305.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-62-1536x609.png 1536w, https:\/\/92it.top\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-62-830x329.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-62-230x91.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-62-350x139.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-62-480x190.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-62.png 1654w\" sizes=\"(max-width: 554px) 100vw, 554px\" \/><\/figure><\/div>\n\n\n\n<p><strong>\u6ce8\u610f\u4e8b\u9879<\/strong><\/p>\n\n\n\n<ul><li>\u7cfb\u7edf\u6709\u9ed8\u8ba4\u914d\u7f6e\u7684\u6d88\u606f\u8f6c\u6362\u5668\u96c6\u5408\u3002<\/li><li>\u5904\u7406\u8fc7\u7a0b\u4f1a\u6309\u96c6\u5408\u987a\u5e8f\u5339\u914d\u5408\u9002\u7684\u6d88\u606f\u8f6c\u6362\u5668\uff0c\u5982\u679c\u6709\u5408\u9002\u7684\uff0c\u5c31\u4f1a\u4f7f\u7528\u8be5\u6d88\u606f\u8f6c\u6362\u5668\u5904\u7406\uff08\u8bfb\u3001\u5199\uff09\uff0c\u540e\u7eed\u7684\u6d88\u606f\u8f6c\u6362\u5668\u4e0d\u518d\u6267\u884c\u3002<\/li><li>\u81ea\u5b9a\u4e49\u7684\u6d88\u606f\u8f6c\u6362\u5668\u8981\u60f3\u751f\u6548\uff0c\u5fc5\u987b\u653e\u5230\u96c6\u5408\u4e2d\u76f8\u540c\u7c7b\u578b\u7684\u6d88\u606f\u8f6c\u6362\u5668\u524d\u9762\uff0c\u539f\u56e0\u53c2\u8003\u7b2c\u4e8c\u70b9\u3002<\/li><li>\u601d\u8003\uff1a\u65e2\u7136\u81ea\u5b9a\u4e49\u7684\u6d88\u606f\u8f6c\u6362\u5668\u5fc5\u987b\u653e\u5230\u96c6\u5408\u4e2d\u76f8\u540c\u7c7b\u578b\u7684\u6d88\u606f\u8f6c\u6362\u5668\u524d\u9762\uff0c\u90a3\u662f\u5426\u80fd\u76f4\u63a5\u6539\u52a8\u96c6\u5408\u4e2d\u539f\u6709\u7684\u6d88\u606f\u8f6c\u6362\u5668\u6765\u8fbe\u5230\u81ea\u5b9a\u4e49\u7684\u6548\u679c\uff0c\u800c\u4e0d\u5fc5\u5728\u52a0\u4e00\u4e2a\uff08\u6682\u672a\u6ca1\u7814\u7a76\uff09\u3002<\/li><li>\u6dfb\u52a0\u81ea\u5b9a\u4e49\u6d88\u606f\u8f6c\u6362\u5668\u65f6\u6ce8\u610f\u9ed8\u8ba4\u6d88\u606f\u8f6c\u6362\u5668\u662f\u5426\u751f\u6548<\/li><\/ul>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p><strong>\u4f8b\u5b50\uff1aSpring Boot\u6dfb\u52a0\u6d88\u606f\u8f6c\u6362\u5668HttpMessageConverter<\/strong><\/p>\n\n\n\n<p id=\"slide-0\"><strong>\u95ee\u9898<\/strong><\/p>\n\n\n\n<p>Spring Boot\u9879\u76ee\u5f00\u53d1\u8fc7\u7a0b\u4e2d\uff0c\u524d\u540e\u7aef\u5206\u79bb\u7684\u9879\u76ee\uff0c\u524d\u540e\u7aef\u901a\u8fc7json\u7684\u6570\u636e\u683c\u5f0f\u4ea4\u4e92\uff0c\u63a5\u53e3\u91c7\u7528@ResponseBody\u6ce8\u89e3\u8fd4\u56dejson\u6570\u636e\uff0c\u5982\u679c\u63a5\u53e3\u8fd4\u56de\u7684\u6570\u636e\u7c7b\u578b\u662fString\uff0c\u4f1a\u5bfc\u81f4\u4e2d\u6587\u4e71\u7801\u3002<\/p>\n\n\n\n<p><strong>\u539f\u56e0<\/strong><\/p>\n\n\n\n<p>\u56e0\u4e3a\u6211\u4eec\u7684Http\u6d88\u606f\u8f6c\u6362\u90fd\u662f\u901a\u8fc7spring\u6846\u67b6\u5b9a\u4e49\u7684\u6d88\u606f\u8f6c\u6362\u5668\u8fdb\u884c\u8f6c\u6362\u7684\uff0c\u4e0d\u540c\u7c7b\u578b\u7684\u6d88\u606f\u6709\u4e0d\u540c\u7684\u6d88\u606f\u7c7b\u578b\u8f6c\u6362\u5668\u5904\u7406\u3002\u5927\u6982\u5982\u4e0b\uff1a<\/p>\n\n\n\n<ul><li>StringHttpMessageConverter\u7684\u4f5c\u7528:\u8d1f\u8d23\u8bfb\u53d6\u5b57\u7b26\u4e32\u683c\u5f0f\u7684\u6570\u636e\u548c\u5199\u51fa\u4e8c\u8fdb\u5236\u683c\u5f0f\u7684\u6570\u636e(\u5f53\u8fd4\u56de\u503c\u662f\u6216\u8005\u63a5\u53d7\u503c\u662fString\u7c7b\u578b\u65f6\uff0c\u662f\u7531\u8fd9\u4e2a\u5904\u7406)<\/li><li>MappingJacksonHttpMessageConverter:\u8d1f\u8d23\u8bfb\u53d6\u548c\u5199\u5165json\u683c\u5f0f\u7684\u6570\u636e\uff1b(\u5f53\u8fd4\u56de\u503c\u662f\u5bf9\u8c61\u6216\u8005List\uff0c\u5c31\u7531\u8fd9\u4e2a\u5904\u7406)<\/li><li>ByteArrayHttpMessageConverter:\u8d1f\u8d23\u8bfb\u53d6\u4e8c\u8fdb\u5236\u683c\u5f0f\u7684\u6570\u636e\u548c\u5199\u51fa\u4e8c\u8fdb\u5236\u683c\u5f0f\u7684\u6570\u636e\uff1b<\/li><li>FormHttpMessageConverter:\u8d1f\u8d23\u8bfb\u53d6form\u63d0\u4ea4\u7684\u6570\u636e\uff08\u80fd\u8bfb\u53d6\u7684\u6570\u636e\u683c\u5f0f\u4e3a application\/x-www-form-urlencoded\uff0c\u4e0d\u80fd\u8bfb\u53d6multipart\/form-data\u683c\u5f0f\u6570\u636e\uff09\uff1b\u8d1f\u8d23\u5199\u5165application\/x-www-from-urlencoded\u548cmultipart\/form-data\u683c\u5f0f\u7684\u6570\u636e\uff1bResourceHttpMessageConverter\uff1a\u8d1f\u8d23\u8bfb\u53d6\u8d44\u6e90\u6587\u4ef6\u548c\u5199\u51fa\u8d44\u6e90\u6587\u4ef6\u6570\u636e\uff1b<\/li><li>SourceHttpMessageConverter\uff1a\u8d1f\u8d23\u8bfb\u53d6\u548c\u5199\u5165 xml \u4e2djavax.xml.transform.Source\u5b9a\u4e49\u7684\u6570\u636e\uff1b<\/li><li>Jaxb2RootElementHttpMessageConverter:\u8d1f\u8d23\u8bfb\u53d6\u548c\u5199\u5165xml \u6807\u7b7e\u683c\u5f0f\u7684\u6570\u636e\uff1b<\/li><li>AtomFeedHttpMessageConverter: \u8d1f\u8d23\u8bfb\u53d6\u548c\u5199\u5165Atom\u683c\u5f0f\u7684\u6570\u636e\uff1b<\/li><li>RssChannelHttpMessageConverter: \u8d1f\u8d23\u8bfb\u53d6\u548c\u5199\u5165RSS\u683c\u5f0f\u7684\u6570\u636e\uff1b<\/li><\/ul>\n\n\n\n<p>\u5f53\u6211\u4eec\u7684\u54cd\u5e94\u6570\u636e\u662fstring\u7c7b\u578b\u662f\uff0c\u6846\u67b6\u81ea\u52a8\u8bc6\u522b\u5230\u6d88\u606f\u7c7b\u578b(MediaType)\uff0c\u4f1a\u91c7\u7528StringHttpMessageConverter\u8fdb\u884c\u6d88\u606f\u8f6c\u6362\uff0c\u4f46\u662fStringHttpMessageConverter\u9ed8\u8ba4\u7684\u5b57\u7b26\u96c6\u662f<strong>ISO-8859-1<\/strong>\uff0c\u8fd9\u5c31\u5bfc\u81f4\u4e86\u54cd\u5e94\u5934\u4e2dContent-Type\u4e3a&#8221;xxx;charset=ISO-8859-1&#8243;\u3002\u6240\u4ee5\u5bfc\u81f4\u4e2d\u6587\u4e71\u7801<\/p>\n\n\n\n<p><strong>\u89e3\u51b3<\/strong><\/p>\n\n\n\n<ul><li>@ReqeustMapping\u4e2d\u6307\u5b9aproduces\u5c5e\u6027\uff1aproduces=&#8221;application\/json;charset=UTF-8&#8243;<\/li><li>\u6dfb\u52a0\u6d88\u606f\u8f6c\u6362\u5668StringHttpMessageConverter\uff0c\u81ea\u5df1\u521b\u5efa\u6d88\u606f\u8f6c\u6362\u5668\uff0c\u5e76\u5236\u5b9a\u7f16\u7801\u96c6\u4e3a:UTF-8<\/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=\"\">@Configuration\n\npublic class WebConfig extends WebMvcConfigurationSupport {\n\n    @Bean\n    public HttpMessageConverter&lt;String> responseBodyConverter() {\n\n        \/\/ \u6846\u67b6\u9ed8\u8ba4\u7684StringHttpMessageConverter\u7f16\u7801\u662fISO-8859-1\uff0c@Response\u6ce8\u89e3\u5f53\u63a5\u53e3\u8fd4\u56de\u7684\u662f\u5b57\u7b26\u4e32\u65f6\u4f1a\u4e2d\u6587\u4e71\u7801\n        return new StringHttpMessageConverter(Charset.forName(\"UTF-8\"));\n    }\n\n    \/**\n     * \u6d88\u606f\u8f6c\u6362\u5668\n     * @param converters\n     *\/\n    @Override\n    protected void extendMessageConverters(List&lt;HttpMessageConverter&lt;?>> converters) {\n\n        super.extendMessageConverters(converters);\n\n        \/\/ \u8bf7\u6ce8\u610f\u987a\u5e8f\uff0c\u56e0\u4e3a\u5176\u5b9e\u5e95\u5c42\u5c31\u662f\u7528list\u8fdb\u884c\u5b58\u50a8\u7684\u6240\u6709\u53ef\u4ee5\u901a\u8fc7\u6307\u5b9a\u4e0b\u6807\u6765\u6307\u5b9a\u987a\u5e8f\n        \/\/ \u76ee\u524d\u8fd9\u79cd\u5199\u6cd5\u53ef\u4ee5\u89e3\u51b3\u95ee\u9898\uff0c\u4f46\u662f\u56e0\u4e3a\u6211\u4eec\u6307\u5b9a\u7684StringHttpMessageConverter\n        \/\/ \u6846\u67b6\u4e5f\u6307\u5b9a\u4e86StringHttpMessageConverter\uff0c\u6240\u6709\u6211\u4eec\u8981\u5c06\u81ea\u5df1\u7684\u6392\u5728\u524d\u9762\uff0c\u5426\u8005\u4f9d\u7136\u65e0\u6cd5\u4f7f\u7528\u81ea\u5b9a\u4e49\u7684\u6d88\u606f\u8f6c\u6362\u5668\n        converters.add(0, responseBodyConverter());\n    }\n}<\/pre>\n\n\n\n<p><strong>\u6e90\u7801<\/strong><\/p>\n\n\n\n<p>Spring Boot\u6216\u8005\u8bf4Spring\u6216\u8005\u8bf4SpringMVC\u4e4b\u6240\u4ee5\u80fd\u5c06http\u8bf7\u6c42\u6d88\u606f\u6620\u5c04\u6210\u6211\u4eeccontroller\u63a5\u53e3\u7684\u65b9\u6cd5\u53c2\u6570\u7684\u5b9e\u4f53\uff0c\u4ee5\u53ca\u5c06\u54cd\u5e94\u7ed3\u679c\u8f6c\u6362\u6210http\u6d88\u606f\uff0c\u662f\u56e0\u4e3a\u6846\u67b6Spring\u6846\u67b6\u5b9a\u4e49\u4e86\u5f88\u591a\u7684\u6d88\u606f\u8f6c\u6362\u5668\uff0c\u6d41\u7a0b\u5982\u4e0b\uff1a<\/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\/12\/\u56fe\u7247-63-1024x311.png\" alt=\"\" class=\"wp-image-9045\" width=\"648\" height=\"197\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-63-1024x311.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-63-300x91.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-63-768x233.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-63-830x252.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-63-230x70.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-63-350x106.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-63-480x146.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-63.png 1464w\" sizes=\"(max-width: 648px) 100vw, 648px\" \/><\/figure><\/div>\n\n\n\n<p>\u6d88\u606f\u8f6c\u6362\u5668\u90fd\u662f\u5b9e\u73b0\u4e86HttpMessageConverter\u63a5\u53e3\u7684java\u7c7b\u3002HttpMessageConverter\u5171\u6709\u5982\u4e0b\u51e0\u4e2a\u65b9\u6cd5,\uff0c\u65b9\u6cd5\u7684\u5927\u6982\u610f\u601d\u89c1\u6ce8\u91ca\uff0c\u4e2d\u6587\u6ce8\u91ca\u4ec5\u4f9b\u53c2\u8003\uff0c\u8be6\u60c5\u89c1\u82f1\u6587\u6ce8\u91ca\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=\"\">public interface HttpMessageConverter&lt;T> {\n\n  \/**\n   * Indicates whether the given class can be read by this converter.\n   * @param clazz the class to test for readability\n   * @param mediaType the media type to read (can be {@code null} if not specified);\n   * typically the value of a {@code Content-Type} header.\n   * @return {@code true} if readable; {@code false} otherwise\n   *\/\n\n  \/\/ \u662f\u5426\u652f\u6301\u8bfbmediaType\u7c7b\u578b\u7684\u6d88\u606f\n\n  boolean canRead(Class&lt;?> clazz, @Nullable MediaType mediaType);\n\n  \/**\n   * Indicates whether the given class can be written by this converter.\n   * @param clazz the class to test for writability\n   * @param mediaType the media type to write (can be {@code null} if not specified);\n   * typically the value of an {@code Accept} header.\n   * @return {@code true} if writable; {@code false} otherwise\n   *\/\n\n  \/\/ \u662f\u5426\u652f\u6301\u5199mediaType\u7c7b\u578b\u7684\u6d88\u606f\n\n  boolean canWrite(Class&lt;?> clazz, @Nullable MediaType mediaType);\n\n  \/**\n   * Return the list of {@link MediaType} objects supported by this converter.\n   * @return the list of supported media types, potentially an immutable copy\n   *\/\n\n  \/\/ \u652f\u6301\u6d88\u606f\u7c7b\u578b\u96c6\u5408\n\n  List&lt;MediaType> getSupportedMediaTypes();\n\n  \/**\n   * Read an object of the given type from the given input message, and returns it.\n   * @param clazz the type of object to return. This type must have previously been passed to the\n   * {@link #canRead canRead} method of this interface, which must have returned {@code true}.\n   * @param inputMessage the HTTP input message to read from\n   * @return the converted object\n   * @throws IOException in case of I\/O errors\n   * @throws HttpMessageNotReadableException in case of conversion errors\n   *\/\n\n  \/\/ \u5177\u4f53\u5b9e\u73b0\u8bfb\uff0c\u8fd9\u91cc\u53ef\u4ee5\u4fee\u6539\u6211\u4eec\u7684\u8bf7\u6c42\u6d88\u606f\n\n  T read(Class&lt;? extends T> clazz, HttpInputMessage inputMessage)\n\n      throws IOException, HttpMessageNotReadableException;\n\n  \/**\n\n   * Write an given object to the given output message.\n   * @param t the object to write to the output message. The type of this object must have previously been\n   * passed to the {@link #canWrite canWrite} method of this interface, which must have returned {@code true}.\n   * @param contentType the content type to use when writing. May be {@code null} to indicate that the\n   * default content type of the converter must be used. If not {@code null}, this media type must have\n   * previously been passed to the {@link #canWrite canWrite} method of this interface, which must have\n   * returned {@code true}.\n   * @param outputMessage the message to write to\n   * @throws IOException in case of I\/O errors\n   * @throws HttpMessageNotWritableException in case of conversion errors\n   *\/\n\n  \/\/ \u5177\u4f53\u5b9e\u73b0\u5199\uff0c\u53ef\u4ee5\u4fee\u6539\u6211\u4eec\u7684\u54cd\u5e94\u6d88\u606f\n\n  void write(T t, @Nullable MediaType contentType, HttpOutputMessage outputMessage)\n\n      throws IOException, HttpMessageNotWritableException;\n\n}<\/pre>\n\n\n\n<p>\u5177\u4f53\u6709\u5982\u4e0b\u51e0\u79cd\u5b9e\u73b0\uff1a<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full is-resized\"><img decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-64.png\" alt=\"\" class=\"wp-image-9047\" width=\"-262\" height=\"-59\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-64.png 1022w, https:\/\/92it.top\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-64-300x68.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-64-768x173.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-64-830x187.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-64-230x52.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-64-350x79.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-64-480x108.png 480w\" sizes=\"(max-width: 1022px) 100vw, 1022px\" \/><\/figure><\/div>\n\n\n\n<p>\u4f20\u7edf\u7684\u4e1a\u52a1\u63a5\u53e3\u4ee3\u7801\u5e38\u7528\u7684\u4e24\u79cdHttp\u6d88\u606f\u8f6c\u6362\u5668\u6709\u4e24\u79cd\u4e00\u79cd\u662f\u5b57\u7b26\u4e32\u8f6c\u6362\u5668\u4e00\u79cd\u662fJSON\u8f6c\u6362\u5668\uff0c\u5206\u522b\u5bf9\u5e94<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">StringHttpMessageConverter\u548cMappingJackson2HttpMessageConverter\u3002<\/pre>\n\n\n\n<p>StringHttpMessageConverter\u7ee7\u627fAbstractHttpMessageConverter&lt;string>\uff0c<\/p>\n\n\n\n<p>AbstractHttpMessageConverter&lt;string>\u5b9e\u73b0HttpMessageConverter&lt;T>\u5982\u4e0a\u56fe\u548c\u4e0a\u9762\u7684\u4ee3\u7801<\/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=\"\">public class StringHttpMessageConverter extends AbstractHttpMessageConverter&lt;String> {\n\n    \/\/ ...... \n\n} <\/pre>\n\n\n\n<p>MappingJackson2HttpMessageConverter\u548cHttpMessageConverter\u7684\u5173\u7cfb\u5c31\u6bd4\u8f83\u6df1\u4e00\u4e9b\uff0c\u76f4\u63a5\u4e0a\u56fe\uff1a<\/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\/12\/\u56fe\u7247-65-1024x653.png\" alt=\"\" class=\"wp-image-9048\" width=\"572\" height=\"364\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-65-1024x653.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-65-300x191.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-65-768x490.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-65-830x529.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-65-230x147.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-65-350x223.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-65-480x306.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-65.png 1270w\" sizes=\"(max-width: 572px) 100vw, 572px\" \/><\/figure><\/div>\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=\"\">public class MappingJackson2HttpMessageConverter extends AbstractJackson2HttpMessageConverter {\n\n    \/\/ ......\n\n}<\/pre>\n\n\n\n<p>\u5982\u679c\u6211\u4eec\u8981\u6dfb\u52a0\u81ea\u5df1\u7684\u6d88\u606f\u8f6c\u6362\u5668\u5230\u6846\u67b6\u4e2d\uff0c\u90a3\u4e48\u6211\u4eec\u5c31\u5e94\u8be5\u77e5\u9053\u6d88\u606f\u8f6c\u6362\u5668\u662f\u4ec0\u4e48\u65f6\u5019\u5728\u54ea\u91cc\u88ab\u521b\u5efa\u7684\u3002<\/p>\n\n\n\n<p>\u6d88\u606f\u8f6c\u6362\u5668\u662f\u5728\u9879\u76ee\u542f\u52a8\u7684\u65f6\u5019\u901a\u8fc7WebMvcConfigurationSupport\u8fdb\u884c\u52a0\u8f7d\uff0c\u5f53getMessageConverters\u88ab\u8c03\u7528\u7684\u65f6\u5019\u4f1a\u901a\u8fc7configureMessageConverters\u3001addDefaultHttpMessageConverters\u548cextendMessageConverters\u4e09\u4e2a\u65b9\u6cd5\u8fdb\u884c\u521d\u59cb\u8bdd\u6d88\u606f\u8f6c\u6362\u5668\u3002\u751f\u6210\u7684\u6d88\u606f\u8f6c\u6362\u5668\u653e\u5728 List&lt;HttpMessageConverter&lt;?&gt;&gt; messageConverters\u96c6\u5408\u4e2d\u3002<\/p>\n\n\n\n<p>\u7cfb\u7edf\u9ed8\u8ba4\u52a0\u8f7d\u7684\u6d88\u606f\u8f6c\u6362\u5668\u5c31\u662f\u5728addDefaultHttpMessageConverters\u65b9\u6cd5\u4e2d\u52a0\u8f7d\u7684\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=\"\">public class WebMvcConfigurationSupport implements ApplicationContextAware, ServletContextAware {\n\n  \/\/ ......\n  \/\/ \u521d\u59cb\u8bdd\u6d88\u606f\u8f6c\u6362\u5668\u96c6\u5408\n  protected final List&lt;HttpMessageConverter&lt;?>> getMessageConverters() {\n\n        if (this.messageConverters == null) {\n            this.messageConverters = new ArrayList&lt;>();\n\n      \/\/ 1\u3001\u52a0\u8f7d\u6d88\u606f\u8f6c\u6362\u5668\n            configureMessageConverters(this.messageConverters);\n\n            if (this.messageConverters.isEmpty()) {\n\n        \/\/ 2\u3001\u5982\u679c\u6d88\u606f\u8f6c\u6362\u5668\u96c6\u5408\u4e3a\u7a7a\u90a3\u4e48\u4e45\u7cfb\u7edf\u9ed8\u8ba4\u52a0\u8f7d\n                addDefaultHttpMessageConverters(this.messageConverters);\n\n            }\n\n      \/\/ 3\u3001\u6269\u5c55\u5f00\u53d1\u8005\u81ea\u5df1\u7684\u52a0\u8f7d\u5668\n            extendMessageConverters(this.messageConverters);\n\n        }\n        return this.messageConverters;\n    }\n\n  protected final void addDefaultHttpMessageConverters(List&lt;HttpMessageConverter&lt;?>> messageConverters) {\n\n        messageConverters.add(new ByteArrayHttpMessageConverter());\n        messageConverters.add(new StringHttpMessageConverter());\n        messageConverters.add(new ResourceHttpMessageConverter());\n        messageConverters.add(new ResourceRegionHttpMessageConverter());\n\n        try {\n            messageConverters.add(new SourceHttpMessageConverter&lt;>());\n        }\n\n        catch (Throwable ex) {\n            \/\/ Ignore when no TransformerFactory implementation is available...\n        }\n\n        messageConverters.add(new AllEncompassingFormHttpMessageConverter());\n\n\n        if (romePresent) {\n            messageConverters.add(new AtomFeedHttpMessageConverter())\n            messageConverters.add(new RssChannelHttpMessageConverter());\n        }\n\n\n        if (jackson2XmlPresent) {\n            Jackson2ObjectMapperBuilder builder = Jackson2ObjectMapperBuilder.xml();\n            if (this.applicationContext != null) {\n                builder.applicationContext(this.applicationContext);\n            }\n\n            messageConverters.add(new MappingJackson2XmlHttpMessageConverter(builder.build()));\n        }\n\n        else if (jaxb2Present) {\n            messageConverters.add(new Jaxb2RootElementHttpMessageConverter());\n        }\n\n\n        if (jackson2Present) {\n\n            Jackson2ObjectMapperBuilder builder = Jackson2ObjectMapperBuilder.json();\n\n            if (this.applicationContext != null) {\n                builder.applicationContext(this.applicationContext);\n            }\n\n            messageConverters.add(new MappingJackson2HttpMessageConverter(builder.build()));\n\n        }\n\n        else if (gsonPresent) {\n            messageConverters.add(new GsonHttpMessageConverter());\n        }\n\n        else if (jsonbPresent) {\n            messageConverters.add(new JsonbHttpMessageConverter());\n        }\n\n\n        if (jackson2SmilePresent) {\n            Jackson2ObjectMapperBuilder builder = Jackson2ObjectMapperBuilder.smile();\n            if (this.applicationContext != null) {\n                builder.applicationContext(this.applicationContext);\n            }\n            messageConverters.add(new MappingJackson2SmileHttpMessageConverter(builder.build()));\n        }\n\n        if (jackson2CborPresent) {\n            Jackson2ObjectMapperBuilder builder = Jackson2ObjectMapperBuilder.cbor();\n            if (this.applicationContext != null) {\n                builder.applicationContext(this.applicationContext);\n            }\n            messageConverters.add(new MappingJackson2CborHttpMessageConverter(builder.build()));\n        }\n    }\n\n  \/\/ ......\n}<\/pre>\n\n\n\n<p>\u4e0b\u56fe\u662f\u6211\u4eec\u81ea\u5df1\u6dfb\u52a0\u4e86\u4e00\u4e2a\u6d88\u606f\u8f6c\u6362\u5668\u540e\u6d88\u606f\u8f6c\u6362\u5668\u7684\u96c6\u5408\u548c\u6846\u67b6\u9ed8\u8ba4\u7684\u6d88\u606f\u8f6c\u6362\u5668\u7684\u96c6\u5408\u5bf9\u6bd4<\/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\/12\/\u56fe\u7247-66-1024x546.png\" alt=\"\" class=\"wp-image-9049\" width=\"581\" height=\"308\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-66-300x160.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-66-230x123.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2022\/12\/\u56fe\u7247-66-350x187.png 350w\" sizes=\"(max-width: 581px) 100vw, 581px\" \/><\/figure><\/div>\n\n\n\n<p>\u6839\u636e\u4ee5\u4e0a\u6240\u8ff0\uff0c\u77e5\u9053\u4e86\u6d88\u606f\u8f6c\u6362\u5668\u7684\u52a0\u8f7d\u987a\uff0c\u6240\u6709\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u7ee7\u627fWebMvcConfigurationSupport\u7c7b\uff0c\u91cdextendMessageConverters\u65b9\u6cd5\u5b9e\u73b0\u6dfb\u52a0\u81ea\u5df1\u7684\u6d88\u606f\u8f6c\u6362\u5668\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u6784\u5efaRESTful\u6570\u636e\u670d\u52a1\u8fc7\u7a0b\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86controller\u3001repositories\uff0c\u5e76\u7528\u4e00\u4e9b\u6ce8\u89e3\u4fee\u9970 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[26],"tags":[],"_links":{"self":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/9041"}],"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=9041"}],"version-history":[{"count":4,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/9041\/revisions"}],"predecessor-version":[{"id":9051,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/9041\/revisions\/9051"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=9041"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=9041"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=9041"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}