{"id":9918,"date":"2023-03-16T09:32:54","date_gmt":"2023-03-16T01:32:54","guid":{"rendered":"http:\/\/123.57.164.21\/?p=9918"},"modified":"2023-03-16T09:42:10","modified_gmt":"2023-03-16T01:42:10","slug":"springboot-httpmessageconverters","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=9918","title":{"rendered":"SpringBoot HttpMessageConverters"},"content":{"rendered":"\n<p><strong>\u524d\u8a00<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>SpringBoot\u4e25\u683c\u7684\u6765\u8bf4\uff0c\u5e94\u8be5\u662f\u4e00\u79cd\u8d1f\u8d23\u628a\u5176\u4ed6\u5df2\u6709\u6846\u67b6\u6574\u5408\u5728\u4e00\u8d77\u7684\u5de5\u5177\uff0cSpringBoot\u4e3b\u8981\u662f\u628a\u5404\u79cd\u6846\u67b6\u90fd\u6574\u5408\u96c6\u4e2d\u5728\u4e00\u8d77\uff0c\u7b80\u5316\u6211\u4eec\u7684Web\u5f00\u53d1\u3002\u6240\u4ee5\u5f88\u591a\u7684\u529f\u80fd\uff0c\u5176\u5b9e\u90fd\u4e0d\u662fSpringBoot\u5b8c\u6210\u7684\uff0c\u800c\u662f\u7531SpringBoot\u4e2d\u6574\u5408\u7684\u5176\u4ed6\u6846\u67b6\u6765\u5b8c\u6210\u7684\u3002\u6bd4\u5982Web\u5f00\u53d1\uff0c\u66f4\u591a\u7684\u662f\u7531SpringMVC\u6765\u5b8c\u6210\uff0c\u53ea\u662fSpringBoot\u5f88\u597d\u7684\u628aSpringMVC\u6574\u5408\u4e86\u8fdb\u6765\uff0c\u800c\u4e14\u5bf9SpringMVC\u7684\u5b9a\u5236\u5316\u5f00\u53d1\u4e5f\u63d0\u4f9b\u4e86\u5f88\u591a\u4fbf\u5229\u3002<\/p>\n\n\n\n<p>\u672c\u6587\u4f1a\u7ed9\u5927\u5bb6\u8bb2\u89e3SpringBoot\u4e2d\u662f\u5982\u4f55\u5bf9SpringMVC\u8fdb\u884c\u6574\u5408\u652f\u6301\u7684\uff0c\u4ee5\u6b64\u4e3a\u6211\u4eec\u4ee5\u540e\u5b66\u4e60\u5176\u4ed6\u6846\u67b6\u7684\u6574\u5408\u63d0\u4f9b\u501f\u9274\u601d\u8def\u3002<br><\/p>\n\n\n\n<p><strong>\u4e00. SpringMVC\u7b80\u4ecb<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>Spring Boot\u4e2d\u5185\u5d4c\u4e86Tomcat\uff0cJetty\u6216Undertow\u7b49\u670d\u52a1\u5668\uff0c\u4f7f\u5f97\u6211\u4eec\u53ef\u4ee5\u975e\u5e38\u8f7b\u677e\u7684\u521b\u5efa\u51fa\u4e00\u4e2aHTTP\u670d\u52a1\u5668\uff0c\u800c\u5927\u591a\u6570\u7684web\u5e94\u7528\u90fd\u53ef\u4ee5\u4f7f\u7528spring-boot-starter-web\u6a21\u5757\u8fdb\u884c\u5feb\u901f\u642d\u5efa\u548c\u8fd0\u884c\uff0cWeb\u6a21\u5757\u7684\u5f00\u53d1\u4e3b\u8981\u662f\u57fa\u4e8eSpringMVC\u6846\u67b6\u6765\u8fdb\u884c\u5b9e\u73b0\u3002<\/p>\n\n\n\n<p><strong>1. SpringMVC\u6982\u5ff5<\/strong><\/p>\n\n\n\n<p>SpringMVC\u6846\u67b6\u662f\u4e00\u4e2a\u201c\u6a21\u578b\uff0c\u89c6\u56fe\uff0c\u63a7\u5236\u5668\u201d\u7684Web\u6846\u67b6\uff0c\u5141\u8bb8\u7528\u6237\u521b\u5efa\u7279\u5b9a\u7684@Controller\u6216@RestController\u7c7b\uff0c\u6765\u5904\u7406\u4f20\u5165\u7684HTTP\u8bf7\u6c42\uff0c\u901a\u8fc7@RequestMapping\u6ce8\u89e3\u53ef\u4ee5\u5c06\u63a7\u5236\u5668\u4e2d\u7684\u65b9\u6cd5\u6620\u5c04\u5230\u76f8\u5e94\u7684HTTP\u8bf7\u6c42\u3002<\/p>\n\n\n\n<p><strong>2. SpringMVC\u7684\u81ea\u52a8\u914d\u7f6e\u7279\u6027<\/strong><\/p>\n\n\n\n<p>Spring Boot\u4e3aSpring MVC\u63d0\u4f9b\u7684auto-configuration\u7279\u6027\u9002\u7528\u4e8e\u5927\u591a\u6570\u5e94\u7528\uff0c\u5e76\u5728Spring\u9ed8\u8ba4\u529f\u80fd\u4e0a\u6dfb\u52a0\u4e86\u4ee5\u4e0b\u7279\u6027\uff1a<\/p>\n\n\n\n<ul><li>\u5f15\u5165ContentNegotiatingViewResolver\u548cBeanNameViewResolver beans\u3002<\/li><li>\u5bf9\u9759\u6001\u8d44\u6e90\u7684\u652f\u6301\uff0c\u5305\u62ec\u5bf9WebJars\u7684\u652f\u6301\u3002<\/li><li>\u81ea\u52a8\u6ce8\u518cConverter\uff0cGenericConverter\uff0cFormatter beans\u3002<\/li><li><strong>\u5bf9HttpMessageConverters\u7684\u652f\u6301\uff0c\u8d1f\u8d23Http\u4fe1\u606f\u8f6c\u6362\u3002<\/strong><\/li><li>\u81ea\u52a8\u6ce8\u518cMessageCodeResolver\u3002<\/li><li>\u5bf9\u9759\u6001index.html\u7684\u652f\u6301\u3002<\/li><li>\u5bf9\u81ea\u5b9a\u4e49Favicon\u7684\u652f\u6301\u3002<\/li><li>\u81ea\u52a8\u4f7f\u7528ConfigurableWebBindingInitializer bean\u3002<\/li><\/ul>\n\n\n\n<p>\u6211\u4eec\u53ef\u4ee5\u6dfb\u52a0\u81ea\u5df1\u7684WebMvcConfigurerAdapter\u7c7b\u578b\u7684@Configuration\u7c7b\uff0c\u800c\u4e0d\u9700\u8981@EnableWebMvc\u6ce8\u89e3\uff1b\u4e5f\u53ef\u4ee5\u6dfb\u52a0\u81ea\u5b9a\u4e49\u7684<strong>HttpMessageConverters\u5b9e\u73b0JSON\u3001XML\u4fe1\u606f\u7684\u81ea\u5b9a\u4e49\u8f6c\u6362\u3002<\/strong><\/p>\n\n\n\n<p><strong>\u4e8c. HttpMessageConverter\u7b80\u4ecb<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p><strong>1. \u6982\u8ff0<\/strong><\/p>\n\n\n\n<p>\u73b0\u5728\u6211\u4eec\u8fdb\u884cWeb\u5f00\u53d1\uff0c\u4e00\u822c\u90fd\u8981\u8bbe\u8ba1\u6210RESTful\u98ce\u683c\u7684API\uff0c\u901a\u8fc7JSON\u683c\u5f0f\u7684\u6570\u636e\u8fdb\u884c\u4ea4\u4e92\u3002\u4f46\u662f\u524d\u7aef\u4f20\u5165\u7684 JSON \u6570\u636e\u5230\u5e95\u662f\u5982\u4f55\u88ab\u89e3\u6790\u6210 Java \u5bf9\u8c61\u4f5c\u4e3a API\u5165\u53c2\u7684\uff0c\u540e\u7aef\u8fd4\u56de\u7684\u7ed3\u679c\u53c8\u662f\u5982\u4f55\u628a Java \u5bf9\u8c61\u89e3\u6790\u6210 JSON \u683c\u5f0f\u7684\u6570\u636e\u8fd4\u56de\u7ed9\u524d\u7aef\u7684\uff0c\u5728\u6574\u4e2a\u6570\u636e\u6d41\u8f6c\u7684\u8fc7\u7a0b\u4e2d\uff0c\u8fd9\u4e9b\u5de5\u4f5c\u90fd\u662f\u7531\u8c01\u6765\u5b8c\u6210\u7684\u5462\uff1f<\/p>\n\n\n\n<p>\u5176\u5b9e\u8fd9\u90fd\u662f\u7531HttpMessageConverter\u8d77\u5230\u7684\u4f5c\u7528\uff01<\/p>\n\n\n\n<p>\u5728SpringMVC\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528@RequestBody\u548c@ResponseBody\u4e24\u4e2a\u6ce8\u89e3\uff0c\u5206\u522b\u5b8c\u6210\u8bf7\u6c42\u62a5\u6587\u5230\u5bf9\u8c61\u548c\u5bf9\u8c61\u5230\u54cd\u5e94\u62a5\u6587\u7684\u8f6c\u6362\uff0c\u5e95\u5c42\u8fd9\u79cd\u7075\u6d3b\u7684\u6d88\u606f\u8f6c\u6362\u673a\u5236\u5c31\u662f\u5229\u7528HttpMessageConverter\u6765\u5b9e\u73b0\u7684\uff0cSpring\u5185\u7f6e\u4e86\u5f88\u591aHttpMessageConverter\uff0c\u6bd4\u5982MappingJackson2HttpMessageConverter\uff0cStringHttpMessageConverter\u7b49\u3002<\/p>\n\n\n\n<p>@RequestBody\u548c@ResponseBody\u662fSpring\u4e2d\u7684\u4e24\u4e2a\u6ce8\u89e3\uff0c\u7b80\u5355\u6765\u8bf4\uff1a<\/p>\n\n\n\n<ul><li>@RequestBody\uff0c\u7528\u4e8e\u5c06\u524d\u7aef\u53d1\u9001\u6765\u7684JSON\/XML\u683c\u5f0f\u7684\u6570\u636e\u8f6c\u5316\u4e3aJAVABEAN\u5bf9\u8c61\uff1b<\/li><li>@ResponseBody\uff0c\u7528\u4e8e\u5c06\u540e\u7aef\u7684JAVABEAN\u5bf9\u8c61\u5bf9\u8c61\u8f6c\u5316\u4e3aJSON\u683c\u5f0f\u7684\u6570\u636e\u8fd4\u56de\u7ed9\u524d\u7aef\u3002<\/li><\/ul>\n\n\n\n<p><strong>@RequestBody<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\uff08A\uff09. \u7528\u4e8e\u8bfb\u53d6\u8bf7\u6c42\u7684body\u6570\u636e\uff0c\u4f7f\u7528\u7cfb\u7edf\u9ed8\u8ba4\u914d\u7f6e\u7684HttpMessageConverter \u8fdb\u884c\u89e3\u6790\uff0c\u7136\u540e\u628a\u76f8\u5e94\u7684\u6570\u636e\u6620\u5c04\u5230\u65b9\u6cd5\u7684\u53c2\u6570\u4e0a\u3002\u4e00\u822c\u60c5\u51b5\u4e0b\u6765\u8bf4\u5e38\u7528\u6765\u5904\u7406application\/json\u7c7b\u578b\u7684\u5185\u5bb9\uff0c\u6216\u8005\u7c97\u9c81\u7684\u8bf4\uff1a\u5c06\u8bf7\u6c42\u4f53\u4e2d\u7684JSON\u6570\u636e\u7ed1\u5b9a\u5230\u65b9\u6cd5\u5bf9\u5e94\u7684BEAN\u4e0a\u3002\n\uff08B\uff09. @RequestBody\u4e0e@RequestParam()\u53ef\u4ee5\u540c\u65f6\u4f7f\u7528\uff0c@RequestBody\u6700\u591a\u53ea\u80fd\u6709\u4e00\u4e2a\uff0c\u800c@RequestParam()\u53ef\u4ee5\u6709\u591a\u4e2a\u3002\n\uff08C\uff09.\u6ce8\u89e3\u5728\u63a7\u5236\u5668\u65b9\u6cd5\u7684\u5f62\u53c2\u524d\uff0c\u7528\u4e8e\u63a5\u6536JSON\/XML\u683c\u5f0f\u5c01\u88c5\u6210\u7684JAVABEAN\u5bf9\u8c61\u6216\u8005\u96c6\u5408\uff0c\u5982\u679c\u662fXML\u683c\u5f0f\u7684\u6570\u636e\u9700\u8981\u6dfb\u52a0\u989d\u5916\u7684\u4f9d\u8d56\u3002\u5982\u679c\u65b9\u6cd5\u7684\u5f62\u53c2\u6ca1\u6709\u8be5\u6ce8\u89e3\u7684\u8bdd\uff0cJAVABEAN\u5bf9\u8c61\u6216\u8005\u96c6\u5408\u5219\u65e0\u6cd5\u63a5\u6536JSON\/XML\u683c\u5f0f\u7684\u6570\u636e\u3002\n\uff08D\uff09.\u5f53\u524d\u7aef\u9875\u9762\u4f20\u53c2\u65f6\uff0c\u9700\u8981\u624b\u52a8\u8bbe\u7f6e contentType: \u201capplication\/json\u201d\uff0c\u9ed8\u8ba4\u662f \u201capplication\/x-www-form-urlencoded\u201d\u3002\n<\/pre>\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=\"\">@PostMapping\npublic Result add(@Validated @RequestBody User user) {\n\n}<\/pre>\n\n\n\n<p><strong>@ResponseBody<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\uff08A\uff09. \u6807\u6ce8\u5728\u63a7\u5236\u5668\u7684\u65b9\u6cd5\u4e0a\uff0c\u7528\u4e8e\u5c06\u65b9\u6cd5\u7684\u8fd4\u56de\u503c\u4ee5JSON\/XML\u7684\u683c\u5f0f\u8fd4\u56de\u7ed9\u5ba2\u6237\u7aef\u3002\n\uff08B\uff09. \u5982\u679c\u6ca1\u6709\u8be5\u6ce8\u89e3\uff0c\u5c06\u8fd4\u56de\u4e00\u4e2aModelAndView\u7ed9\u5ba2\u6237\u7aef\uff0c\u5373\u8fd4\u56de\u89c6\u56fe\u3002\n\uff08C\uff09.Spring\u4e2d\u7684\u65b0\u589e\u6ce8\u89e3\uff1a@RestController\uff0c@RestController = @Controller + @ResponseBody\uff0c\u5982\u679c\u4e00\u4e2aController\u7c7b\u6dfb\u52a0\u4e86@RestController\u6ce8\u89e3\uff0c\u90a3\u4e48\u8be5Controller\u7c7b\u4e2d\u7684\u6240\u6709\u65b9\u6cd5\u90fd\u76f8\u5f53\u4e8e\u6dfb\u52a0\u4e86@ResponseBody \u6ce8\u89e3\u3002\n\uff08D\uff09.\u5f53\u63a7\u5236\u5668\u4f7f\u7528@RestController\u6807\u6ce8\u65f6\uff0c\u63a7\u5236\u5668\u4e2d\u7684\u6240\u6709\u65b9\u6cd5\u65e0\u9700\u518d\u6dfb\u52a0@ResponseBody \u6ce8\u89e3\uff1b\u5f53\u63a7\u5236\u5668\u4f7f\u7528@Controller\u6807\u6ce8\u65f6\uff0c\u63a7\u5236\u5668\u4e2d\u7684\u6240\u6709\u65b9\u6cd5\u5219\u9700\u8981\u6dfb\u52a0@ResponseBody\u6ce8\u89e3\u3002\n<\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>2. HttpMessageConverter\u7b80\u4ecb<\/strong><\/p>\n\n\n\n<p>org.springframework.http.converter.HttpMessageConverter \u662fSpringMVC\u4e2d\u63d0\u4f9b\u7684\u4e00\u4e2a\u7b56\u7565\u63a5\u53e3\uff0c\u5b83\u662f\u4e00\u4e2a\u8f6c\u6362\u5668\u7c7b\uff0c\u8d1f\u8d23\u8f6c\u6362HTTP\u7684\u8bf7\u6c42\u548c\u54cd\u5e94\uff0c\u53ef\u4ee5\u628a\u5bf9\u8c61\u81ea\u52a8\u8f6c\u6362\u4e3aJSON(\u4f7f\u7528Jackson\u5e93\u6216Gson\u5e93)\u6216XML(\u4f7f\u7528Jackson XML\u6216\u8005JAXB2)\uff0c\u5bf9\u5b57\u7b26\u4e32\u9ed8\u8ba4\u4f7f\u7528UTF-8\u7f16\u7801\u5904\u7406\uff0c\u4e00\u822c\u60c5\u51b5\u4e0b\u6211\u4eec\u91c7\u7528\u9ed8\u8ba4\u914d\u7f6e\u5c31\u53ef\u4ee5\u3002<\/p>\n\n\n\n<p>\u8be5\u63a5\u53e3\u8bf4\u660e\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Strategy interface that specifies a converter that can convert from and to HTTP requests and responses\u3002<\/pre>\n\n\n\n<p>\u7b80\u5355\u8bf4\u5c31\u662f HTTP request(\u8bf7\u6c42)\u548cresponse(\u54cd\u5e94)\u7684\u8f6c\u6362\u5668\u3002\u8be5\u63a5\u53e3\u91cc\u67095\u4e2a\u65b9\u6cd5\uff0c\u63a5\u6536\u5230\u8bf7\u6c42\u65f6\u5224\u65ad\u662f\u5426\u80fd\u8bfb(canRead)\uff0c\u80fd\u8bfb\u5219\u8bfb(read)\uff1b\u8fd4\u56de\u7ed3\u679c\u65f6\u5224\u65ad\u662f\u5426\u80fd\u5199(canWrite)\uff0c\u80fd\u5199\u5219\u5199(write)\uff0c\u4ee5\u53ca\u83b7\u53d6\u652f\u6301\u7684 MediaType(application\/json\u4e4b\u7c7b)\u65b9\u6cd5\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=\"\">boolean canRead(Class&lt;?> clazz, MediaType mediaType);\n \nboolean canWrite(Class&lt;?> clazz, MediaType mediaType);\n \nList&lt;MediaType> getSupportedMediaTypes();\n \nT read(Class&lt;? extends T> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException;\n \nvoid write(T t, MediaType contentType, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException;<\/pre>\n\n\n\n<p><strong>3. HttpMessageConverter\u8f6c\u6362\u539f\u7406<\/strong><\/p>\n\n\n\n<p>\u5229\u7528SpringMVC\u6846\u67b6\uff0c\u53ef\u4ee5\u4f7f\u5f97\u6211\u4eec\u5728\u5f00\u53d1\u65f6\uff0c\u53ea\u8981\u5728\u4ee3\u7801\u4e2d\u4f7f\u7528@RequestBody\u548c@ResponseBody\u4e24\u4e2a\u6ce8\u89e3\uff0c\u5c31\u53ef\u4ee5\u5206\u522b\u5b8c\u6210\u4ece\u8bf7\u6c42\u62a5\u6587\u5230\u5bf9\u8c61\u548c\u4ece\u5bf9\u8c61\u5230\u54cd\u5e94\u62a5\u6587\u7684\u8f6c\u6362\u3002\u800c\u5728\u6e90\u7801\u5185\u90e8\uff0c\u5176\u5b9e\u8fd9\u79cd\u7075\u6d3b\u7684\u6d88\u606f\u8f6c\u6362\u673a\u5236\u5c31\u662f\u5229\u7528HttpMessageConverter\u6765\u5b9e\u73b0\u7684\u3002<\/p>\n\n\n\n<p><strong>\u5b9e\u73b0\u8fc7\u7a0b\u53c2\u8003\u4e0b\u56fe\uff1a<\/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\/2023\/03\/\u56fe\u7247-69-1024x318.png\" alt=\"\" class=\"wp-image-9919\" width=\"557\" height=\"172\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-69-1024x318.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-69-300x93.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-69-768x239.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-69-830x258.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-69-230x72.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-69-350x109.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-69-480x149.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-69.png 1428w\" sizes=\"(max-width: 557px) 100vw, 557px\" \/><\/figure><\/div>\n\n\n\n<p>SpringMVC\u5728\u542f\u52a8\u65f6\u4f1a\u81ea\u52a8\u914d\u7f6e\u4e00\u4e9b\u9ed8\u8ba4\u7684HttpMessageConverter\u8f6c\u6362\u5668\uff0c\u5728 WebMvcConfigurationSupport\u7c7b\u4e2d\u6dfb\u52a0\u4e86\u7f3a\u7701\u7684MessageConverter\uff0c\u6bd4\u5982MappingJackson2HttpMessageConverter\uff0cStringHttpMessageConverter\u7b49\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=\"\">   protected final void addDefaultHttpMessageConverters(List&lt;HttpMessageConverter&lt;?>> messageConverters) {\n        StringHttpMessageConverter stringConverter = new StringHttpMessageConverter();\n        stringConverter.setWriteAcceptCharset(false);\n \n        messageConverters.add(new ByteArrayHttpMessageConverter());\n        messageConverters.add(stringConverter);\n        messageConverters.add(new ResourceHttpMessageConverter());\n        messageConverters.add(new SourceHttpMessageConverter&lt;Source>());\n        messageConverters.add(new AllEncompassingFormHttpMessageConverter());\n \n        if (romePresent) {\n            messageConverters.add(new AtomFeedHttpMessageConverter());\n            messageConverters.add(new RssChannelHttpMessageConverter());\n        }\n \n        if (jackson2XmlPresent) {\n            ObjectMapper objectMapper = Jackson2ObjectMapperBuilder.xml().applicationContext(this.applicationContext).build();\n            messageConverters.add(new MappingJackson2XmlHttpMessageConverter(objectMapper));\n        }\n        else if (jaxb2Present) {\n            messageConverters.add(new Jaxb2RootElementHttpMessageConverter());\n        }\n \n        if (jackson2Present) {\n            ObjectMapper objectMapper = Jackson2ObjectMapperBuilder.json().applicationContext(this.applicationContext).build();\n            messageConverters.add(new MappingJackson2HttpMessageConverter(objectMapper));\n        }\n        else if (gsonPresent) {\n            messageConverters.add(new GsonHttpMessageConverter());\n        }\n    }<\/pre>\n\n\n\n<p>\u4ece\u6e90\u7801\u4e2d\u53ef\u4ee5\u770b\u5230\uff0cSpringMVC\u4e2d\u4f7f\u7528Jackson\u5e93\u6216Gson\u5e93\u6765\u8f6c\u6362json\u5b57\u7b26\u4e32\uff0c\u4f7f\u7528\u4f7f\u7528Jackson XML\u6216\u8005JAXB2\u6765\u8f6c\u6362XML\u3002<\/p>\n\n\n\n<p>\u800c\u5982\u679c\u6211\u4eec\u6ca1\u6709\u914d\u7f6e\u81ea\u5df1\u7684 MessageConverter\uff0cSpringMVC\u542f\u52a8\u65f6\u5c31\u4f1a\u8c03\u7528 addDefaultHttpMessageConverters()\u65b9\u6cd5\u3002<br><\/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\/2023\/03\/\u56fe\u7247-70-1024x371.png\" alt=\"\" class=\"wp-image-9920\" width=\"536\" height=\"194\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-70-1024x371.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-70-300x109.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-70-768x279.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-70-830x301.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-70-230x83.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-70-350x127.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-70-480x174.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-70.png 1428w\" sizes=\"(max-width: 536px) 100vw, 536px\" \/><\/figure><\/div>\n\n\n\n<p><strong>4.&nbsp;SpringBoot\u4e2d\u81ea\u5b9a\u4e49HttpMessageConverters\u7684\u5b9e\u73b0\u601d\u8def<\/strong><\/p>\n\n\n\n<p>\u5982\u679c\u6211\u4eec\u60f3\u5728SpringBoot\u4e2d\u81ea\u5b9a\u4e49\u81ea\u5df1\u7684HttpMessageConverters\u8be5\u600e\u4e48\u529e\u5462\uff1f\u8fd9\u65f6\u5019\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528HttpMessageConverters\u6dfb\u52a0HttpMessageConverter\u539f\u751f\u8f6c\u6362\u7c7b\u6216\u81ea\u5b9a\u4e49\u8f6c\u6362\u7c7b\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/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=\"\">  import org.springframework.boot.autoconfigure.web.HttpMessageConverters;\nimport org.springframework.context.annotation.*;\nimport org.springframework.http.converter.*;\n \n@Configuration\npublic class MyConfiguration {\n \n    @Bean\n    public HttpMessageConverters customConverters() {\n        HttpMessageConverter&lt;?> additional = ...\n        HttpMessageConverter&lt;?> another = ...\n        return new HttpMessageConverters(additional, another);\n    }\n}<\/pre>\n\n\n\n<p>\u4ece\u4e0a\u9762\u7684\u4ee3\u7801\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u770b\u5230\uff0c\u53ef\u4ee5\u628aHttpMessageConverter\u7c7b\u6dfb\u52a0\u5230converters\u5217\u8868\u4e2d\uff0c\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\u6765\u8986\u76d6\u9ed8\u8ba4\u7684\u8f6c\u6362\u5668\u5217\u8868(converters)\u3002<\/p>\n\n\n\n<p><strong>5. \u81ea\u5b9a\u4e49\u6d88\u606f\u8f6c\u6362\u5668\u7684\u65b9\u5f0f<\/strong><\/p>\n\n\n\n<p>\u6211\u4eec\u53ef\u4ee5\u81ea\u5b9a\u4e49\u81ea\u5df1\u7684\u6d88\u606f\u8f6c\u6362\u5668\u6765\u6ee1\u8db3\u7279\u5b9a\u7684\u9700\u6c42\uff0c\u4e00\u822c\u81ea\u5b9a\u4e49\u6d88\u606f\u8f6c\u6362\u5668\u6709\u4e24\u79cd\u65b9\u5f0f:<\/p>\n\n\n\n<ul><li>1. \u4f7f\u7528Spring\u6216\u8005\u7b2c\u4e09\u65b9\u63d0\u4f9b\u7684\u73b0\u6210\u7684HttpMessageConverter\uff1b<\/li><li>2. \u81ea\u5df1\u91cd\u5199\u4e00\u4e2aHttpMessageConverter\u3002<\/li><\/ul>\n\n\n\n<p><strong>\u4e09.\u4f7f\u7528FastJsonHttpMessageConverter\u66ff\u6362\u9ed8\u8ba4\u7684\u8f6c\u6362\u5668<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u63a5\u4e0b\u6765\u5c31\u901a\u8fc7\u4e00\u4e2a\u6848\u4f8b\uff0c\u5229\u7528FastJsonHttpMessageConverter\u6765\u66ff\u6362\u6389\u9ed8\u8ba4\u7684\u6d88\u606f\u8f6c\u6362\u5668\u3002<\/p>\n\n\n\n<p><strong>1. \u521b\u5efaSpringBoot\u9879\u76ee<\/strong><\/p>\n\n\n\n<p>\u6211\u4eec\u5728\u4e4b\u524d\u7684\u57fa\u7840\u4e0a\uff0c\u65b0\u5efa\u4e00\u4e2a\u6a21\u5757demo10\uff0c\u5e76\u5c06\u5176\u6539\u9020\u4e3aSpringBoot\u9879\u76ee\u3002<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-71.png\" alt=\"\" class=\"wp-image-9921\" width=\"377\" height=\"380\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-71.png 950w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-71-297x300.png 297w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-71-150x150.png 150w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-71-768x774.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-71-830x837.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-71-230x232.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-71-350x353.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-71-480x484.png 480w\" sizes=\"(max-width: 377px) 100vw, 377px\" \/><\/figure><\/div>\n\n\n\n<p><strong>2. \u6dfb\u52a0FastJson\u4f9d\u8d56<\/strong><\/p>\n\n\n\n<p>\u5728SpringBoot\u9879\u76ee\u4e2d\uff0c\u5f53\u6211\u4eec\u5728\u63a7\u5236\u5668\u7c7b\u6216\u8005\u5176\u5185\u90e8\u7684\u65b9\u6cd5\u4e0a\u6dfb\u52a0@RestController\u6ce8\u89e3\u548c@ResponseBody\u6ce8\u89e3\u540e\uff0c\u9ed8\u8ba4\u4f1a\u4f7f\u7528jackson\u63d2\u4ef6\u6765\u8fd4\u56dejson\u683c\u5f0f\u7684\u6570\u636e\uff0c\u4f46\u662f\u6211\u4eec\u4e5f\u53ef\u4ee5\u5229\u7528fastjson\u4e3a\u6211\u4eec\u63d0\u4f9b\u7684FastJsonHttpMessageConverter\u6765\u8fd4\u56dejson\u683c\u5f0f\u7684\u6570\u636e\u3002\u6240\u4ee5\u8fd9\u91cc\u9996\u5148\u5f15\u5165fastjson\u7684\u4f9d\u8d56\u5305\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=\"\">&lt;dependency>\n   &lt;groupId>com.alibaba&lt;\/groupId>\n   &lt;artifactId>fastjson&lt;\/artifactId>\n   &lt;version>1.2.61&lt;\/version>\n&lt;\/dependency><\/pre>\n\n\n\n<p><br><strong>3. \u914d\u7f6eFastJsonHttpMessageConverter<\/strong><\/p>\n\n\n\n<p>\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u5b9e\u73b0WebMvcConfigurer\u63a5\u53e3\uff0c\u6765\u914d\u7f6eFastJsonHttpMessageConverter\uff0c\u5728Spring Boot2.0\u7248\u672c\u4ee5\u540e\u63a8\u8350\u4f7f\u7528\u8fd9\u79cd\u65b9\u5f0f\u6765\u8fdb\u884cWeb\u914d\u7f6e\uff0c\u8fd9\u6837\u4e0d\u4f1a\u8986\u76d6\u6389Spring Boot\u7684\u4e00\u4e9b\u9ed8\u8ba4\u914d\u7f6e\u3002<\/p>\n\n\n\n<p>\u63a5\u4e0b\u6765\u6211\u4eec\u5728\u9879\u76ee\u5305\u4e2d\u521b\u5efaconfig\u5305\uff0c\u5e76\u521b\u5efa\u4e00\u4e2aCustomWebMvcConfigurer\u7c7b\uff0c\u4ee3\u7801\u7ed3\u6784\u5982\u4e0b\u56fe\u6240\u793a\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\/2023\/03\/\u56fe\u7247-72-1024x673.png\" alt=\"\" class=\"wp-image-9922\" width=\"464\" height=\"305\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-72-1024x673.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-72-300x197.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-72-768x505.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-72-830x545.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-72-230x151.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-72-350x230.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-72-480x315.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-72.png 1388w\" sizes=\"(max-width: 464px) 100vw, 464px\" \/><\/figure><\/div>\n\n\n\n<p>CustomWebMvcConfigurer\u7c7b\u4e2d\u7684\u5185\u5bb9\uff1a<\/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=\"\">\/**\n *\u5b9e\u73b0WebMvcConfigurer\u63a5\u53e3\u6765\u914d\u7f6eFastJsonHttpMessageConverter,\n *Spring Boot2.0\u7248\u672c\u4ee5\u540e\u63a8\u8350\u4f7f\u7528\u8fd9\u79cd\u65b9\u5f0f\u6765\u8fdb\u884cweb\u914d\u7f6e\uff0c\u8fd9\u6837\u4e0d\u4f1a\u8986\u76d6\u6389Spring Boot\u7684\u4e00\u4e9b\u9ed8\u8ba4\u914d\u7f6e.\n *\/\n@Configuration\npublic class CustomWebMvcConfigurer implements WebMvcConfigurer {\n \n    @Override\n    public void extendMessageConverters(List&lt;HttpMessageConverter&lt;?>> converters) {\n        FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();\n        FastJsonConfig config = new FastJsonConfig();\n        config.setSerializerFeatures(SerializerFeature.DisableCircularReferenceDetect);\n        converter.setFastJsonConfig(config);\n        converter.setDefaultCharset(Charset.forName(\"UTF-8\"));\n        converters.add(converter);\n    }\n \n}<\/pre>\n\n\n\n<p><strong>3.1 FastJson\u4e2d\u7684SerializerFeature\u7b80\u4ecb<\/strong><\/p>\n\n\n\n<p>\u5728FastJson\u914d\u7f6e\u5b9e\u4f53\u65f6\uff0c\u4f1a\u8c03\u7528setSerializerFeatures\u65b9\u6cd5\u914d\u7f6e\u591a\u4e2a\u8fc7\u6ee4\u65b9\u5f0f\uff0c\u5e38\u7528\u7684\u914d\u7f6e\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\/2023\/03\/\u56fe\u7247-73-900x1024.png\" alt=\"\" class=\"wp-image-9923\" width=\"409\" height=\"465\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-73-900x1024.png 900w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-73-264x300.png 264w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-73-768x874.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-73-830x945.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-73-230x262.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-73-350x398.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-73-480x546.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-73.png 1130w\" sizes=\"(max-width: 409px) 100vw, 409px\" \/><\/figure><\/div>\n\n\n\n<p><strong>3.2 \u5e38\u7528\u7684SerializerFeature\u91ca\u4e49<\/strong><\/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=\"\">QuoteFieldNames,\/\/\u8f93\u51fakey\u65f6\u662f\u5426\u4f7f\u7528\u53cc\u5f15\u53f7,\u9ed8\u8ba4\u4e3atrue;\n \nUseSingleQuotes,\/\/\u4f7f\u7528\u5355\u5f15\u53f7\u800c\u4e0d\u662f\u53cc\u5f15\u53f7,\u9ed8\u8ba4\u4e3afalse;\n \nWriteMapNullValue,\/\/\u662f\u5426\u8f93\u51fa\u503c\u4e3anull\u7684\u5b57\u6bb5,\u9ed8\u8ba4\u4e3afalse; \n \nWriteEnumUsingToString,\/\/Enum\u8f93\u51faname()\u6216\u8005original,\u9ed8\u8ba4\u4e3afalse;\n \nUseISO8601DateFormat,\/\/Date\u4f7f\u7528ISO8601\u683c\u5f0f\u8f93\u51fa\uff0c\u9ed8\u8ba4\u4e3afalse;\n \nWriteNullListAsEmpty,\/\/List\u5b57\u6bb5\u5982\u679c\u4e3anull,\u8f93\u51fa\u4e3a[],\u800c\u975enull;\n \nWriteNullStringAsEmpty,\/\/\u5b57\u7b26\u7c7b\u578b\u5b57\u6bb5\u5982\u679c\u4e3anull,\u8f93\u51fa\u4e3a\"\",\u800c\u975enull; \n     \nWriteNullNumberAsZero,\/\/\u6570\u503c\u5b57\u6bb5\u5982\u679c\u4e3anull,\u8f93\u51fa\u4e3a0,\u800c\u975enull;\n \nWriteNullBooleanAsFalse,\/\/Boolean\u5b57\u6bb5\u5982\u679c\u4e3anull,\u8f93\u51fa\u4e3afalse,\u800c\u975enull;\n \nSkipTransientField,\/\/\u5982\u679c\u662ftrue\uff0c\u7c7b\u4e2d\u7684Get\u65b9\u6cd5\u5bf9\u5e94\u7684Field\u662ftransient\uff0c\u5e8f\u5217\u5316\u65f6\u5c06\u4f1a\u88ab\u5ffd\u7565\u3002\u9ed8\u8ba4\u4e3atrue;\n \nSortField,\/\/\u6309\u5b57\u6bb5\u540d\u79f0\u6392\u5e8f\u540e\u8f93\u51fa,\u9ed8\u8ba4\u4e3afalse;\n \n@Deprecated\nWriteTabAsSpecial,\/\/\u628a\\t\u505a\u8f6c\u4e49\u8f93\u51fa,\u9ed8\u8ba4\u4e3afalse;\n \nPrettyFormat,\/\/\u7ed3\u679c\u662f\u5426\u683c\u5f0f\u5316,\u9ed8\u8ba4\u4e3afalse;\n \nWriteClassName,\/\/\u5e8f\u5217\u5316\u65f6\u5199\u5165\u7c7b\u578b\u4fe1\u606f,\u9ed8\u8ba4\u4e3afalse,\u53cd\u5e8f\u5217\u5316\u662f\u9700\u7528\u5230;\n \nDisableCircularReferenceDetect,\/\/\u6d88\u9664\u5bf9\u540c\u4e00\u5bf9\u8c61\u5faa\u73af\u5f15\u7528\u7684\u95ee\u9898,\u9ed8\u8ba4\u4e3afalse;\n \nWriteSlashAsSpecial,\/\/\u5bf9\u659c\u6760'\/'\u8fdb\u884c\u8f6c\u4e49;\n \nBrowserCompatible,\/\/\u5c06\u4e2d\u6587\u90fd\u4f1a\u5e8f\u5217\u5316\u4e3a\\uXXXX\u683c\u5f0f\uff0c\u5b57\u8282\u6570\u4f1a\u591a\u4e00\u4e9b\uff0c\u4f46\u662f\u80fd\u517c\u5bb9IE 6\uff0c\u9ed8\u8ba4\u4e3afalse;\n \nWriteDateUseDateFormat,\/\/\u5168\u5c40\u4fee\u6539\u65e5\u671f\u683c\u5f0f,\u9ed8\u8ba4\u4e3afalse\u3002JSON.DEFFAULT_DATE_FORMAT = \"yyyy-MM-dd\";JSON.toJSONString(obj,SerializerFeature.WriteDateUseDateFormat);\n \nDisableCheckSpecialChar,\/\/\u4e00\u4e2a\u5bf9\u8c61\u7684\u5b57\u7b26\u4e32\u5c5e\u6027\u4e2d\u5982\u679c\u6709\u7279\u6b8a\u5b57\u7b26\u5982\u53cc\u5f15\u53f7\uff0c\u5c06\u4f1a\u5728\u8f6c\u6210json\u65f6\u5e26\u6709\u53cd\u659c\u6760\u8f6c\u79fb\u7b26\u3002\u5982\u679c\u4e0d\u9700\u8981\u8f6c\u4e49\uff0c\u53ef\u4ee5\u4f7f\u7528\u8fd9\u4e2a\u5c5e\u6027,\u9ed8\u8ba4\u4e3afalse.<\/pre>\n\n\n\n<p><strong>4. \u521b\u5efa\u5bf9\u8c61\u5b9e\u4f53<\/strong><\/p>\n\n\n\n<p>\u7136\u540e\u6211\u4eec\u518d\u521b\u5efa\u4e00\u4e2a\u201ccom.yyg.boot.domain\u201d\u5305\uff0c\u5728\u8fd9\u91cc\u521b\u5efa\u4e00\u4e2aUser\u5b9e\u4f53\u7c7b\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.yyg.boot.domain;\n \nimport lombok.AllArgsConstructor;\nimport lombok.Data;\n \n@Data\n@AllArgsConstructor\npublic class User {\n \n    private String name;\n \n    private String address;\n \n}<\/pre>\n\n\n\n<p><strong>5. \u521b\u5efaController<\/strong><\/p>\n\n\n\n<p>\u518d\u521b\u5efa\u4e00\u4e2a\u201ccom.yyg.boot.web\u201d\u5305\uff0c\u5728\u91cc\u9762\u521b\u5efaController\u7c7b\uff0c\u5b9a\u4e49\u4e00\u4e2a\u201d\/users\u201c\u63a5\u53e3\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.yyg.boot.web;\n \nimport com.yyg.boot.domain.User;\nimport org.springframework.web.bind.annotation.RequestMapping;\nimport org.springframework.web.bind.annotation.RequestMethod;\nimport org.springframework.web.bind.annotation.RestController;\n \nimport java.util.ArrayList;\nimport java.util.List;\n \n@RestController\npublic class UserController {\n \n    @RequestMapping(value = \"\/users\", method = RequestMethod.GET)\n    public Object getList() {\n        List&lt;User> list = new ArrayList&lt;>();\n        \/\/\u6d4b\u8bd5\u5b57\u7b26\u4e32\u6709null\u7684\u60c5\u51b5\n        User u1 = new User(\"\u4e00\u4e00\u54e5\", null);\n        list.add(u1);\n        return list;\n    }\n \n}<\/pre>\n\n\n\n<p><strong>6. \u521b\u5efa\u7a0b\u5e8f\u5165\u53e3\u7c7b<\/strong><\/p>\n\n\n\n<p>\u6700\u540e\u6211\u4eec\u5728\u9879\u76ee\u6839\u76ee\u5f55\u4e0b\u521b\u5efa\u542f\u52a8\u7c7b\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.yyg.boot;\n \nimport org.springframework.boot.SpringApplication;\nimport org.springframework.boot.autoconfigure.SpringBootApplication;\n \n\/**\n * \u81ea\u5b9a\u4e49HttpMessageConverter\n *\/\n@SpringBootApplication\npublic class ConvertApplication {\n \n    public static void main(String[] args) {\n        SpringApplication.run(ConvertApplication.class, args);\n    }\n \n}<\/pre>\n\n\n\n<p><strong>7. \u9879\u76ee\u7ed3\u6784<\/strong><\/p>\n\n\n\n<p>\u6700\u540e\u6574\u4e2a\u9879\u76ee\u7684\u4ee3\u7801\u7ed3\u6784\u5982\u4e0b\u56fe\u6240\u793a\uff0c\u5404\u4f4d\u53ef\u4ee5\u53c2\u8003\u521b\u5efa\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\/2023\/03\/\u56fe\u7247-74-898x1024.png\" alt=\"\" class=\"wp-image-9924\" width=\"403\" height=\"459\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-74-898x1024.png 898w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-74-263x300.png 263w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-74-768x876.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-74-830x946.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-74-230x262.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-74-350x399.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-74-480x547.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-74.png 900w\" sizes=\"(max-width: 403px) 100vw, 403px\" \/><\/figure><\/div>\n\n\n\n<p><strong>8. \u542f\u52a8\u9879\u76ee\u8fdb\u884c\u6d4b\u8bd5<\/strong><\/p>\n\n\n\n<p>\u6700\u540e\u6211\u4eec\u542f\u52a8\u9879\u76ee\uff0c\u5728\u6d4f\u89c8\u5668\u4e2d\u8bbf\u95eehttp:\/\/localhost:8080\/users\u63a5\u53e3\u3002<br>\u56e0\u4e3a\u6211\u4eecCustomWebMvcConfigurer\u7c7b\u7684\u4ee3\u7801\u4e2d\uff0c\u6ca1\u6709\u914d\u7f6eWriteMapNullValue\uff0c\u6240\u4ee5\u5982\u679c\u8fd4\u56de\u7ed3\u679c\u4e2d\u6709null\u503c\u5219\u4e0d\u663e\u793a\uff0c\u6548\u679c\u5982\u4e0b:<\/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\/2023\/03\/\u56fe\u7247-75-1024x496.png\" alt=\"\" class=\"wp-image-9925\" width=\"414\" height=\"199\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-75-300x145.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-75-230x111.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-75-350x169.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-75-480x232.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-75.png 1434w\" sizes=\"(max-width: 414px) 100vw, 414px\" \/><\/figure><\/div>\n\n\n\n<p>\u63a5\u7740\u6211\u4eec\u5728CustomWebMvcConfigurer\u4e2d\u6dfb\u52a0WriteMapNullValue\u914d\u7f6e\uff0c\u5982\u4e0b\u56fe:<\/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\/2023\/03\/\u56fe\u7247-76-1024x145.png\" alt=\"\" class=\"wp-image-9926\" width=\"562\" height=\"79\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-76-1024x145.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-76-300x42.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-76-768x108.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-76-1536x217.png 1536w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-76-830x117.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-76-230x32.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-76-350x49.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-76-480x68.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-76.png 1544w\" sizes=\"(max-width: 562px) 100vw, 562px\" \/><\/figure><\/div>\n\n\n\n<p>\u7136\u540e\u91cd\u65b0\u542f\u52a8\u9879\u76ee\u5e76\u8bbf\u95ee\uff0c\u4ece\u7ed3\u679c\u53ef\u4ee5\u770b\u51fa\uff0c\u6211\u4eec\u914d\u7f6e\u7684\u6d88\u606f\u8f6c\u6362\u5668\u8d77\u4e86\u4f5c\u7528\uff0cnull\u503c\u5c55\u793a\u4e86\u51fa\u6765\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\/2023\/03\/\u56fe\u7247-77-1024x585.png\" alt=\"\" class=\"wp-image-9927\" width=\"428\" height=\"244\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-77-1024x585.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-77-300x171.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-77-768x439.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-77-830x474.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-77-230x131.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-77-350x200.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-77-480x274.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-77.png 1358w\" sizes=\"(max-width: 428px) 100vw, 428px\" \/><\/figure><\/div>\n\n\n\n<p>\u7136\u540e\u6211\u4eec\u5728CustomWebMvcConfigurer\u7c7b\u4e2d\uff0c\u518d\u6539\u4e3aWriteNullStringAsEmpty\u914d\u7f6e\uff0c\u5982\u4e0b\u56fe:<\/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\/2023\/03\/\u56fe\u7247-78-1024x165.png\" alt=\"\" class=\"wp-image-9928\" width=\"571\" height=\"92\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-78-1024x165.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-78-300x48.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-78-768x124.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-78-1536x248.png 1536w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-78-830x134.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-78-230x37.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-78-350x57.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-78-480x78.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-78.png 1548w\" sizes=\"(max-width: 571px) 100vw, 571px\" \/><\/figure><\/div>\n\n\n\n<p>\u7136\u540e\u91cd\u65b0\u542f\u52a8\u9879\u76ee\u5e76\u8bbf\u95ee\uff0c\u4ece\u7ed3\u679c\u4e2d\u53ef\u4ee5\u770b\u51fa\uff0c\u6211\u4eec\u914d\u7f6e\u7684\u6d88\u606f\u8f6c\u6362\u5668\u53c8\u8d77\u4e86\u4f5c\u7528\uff0cnull\u88ab\u66ff\u6362\u6210\u4e86&#8221;&#8221;\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\/2023\/03\/\u56fe\u7247-79-1024x584.png\" alt=\"\" class=\"wp-image-9929\" width=\"426\" height=\"243\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-79-1024x584.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-79-300x171.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-79-768x438.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-79-830x474.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-79-230x131.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-79-350x200.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-79-480x274.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-79.png 1272w\" sizes=\"(max-width: 426px) 100vw, 426px\" \/><\/figure><\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u524d\u8a00 SpringBoot\u4e25\u683c\u7684\u6765\u8bf4\uff0c\u5e94\u8be5\u662f\u4e00\u79cd\u8d1f\u8d23\u628a\u5176\u4ed6\u5df2\u6709\u6846\u67b6\u6574\u5408\u5728\u4e00\u8d77\u7684\u5de5\u5177\uff0cSpringBoot\u4e3b\u8981\u662f [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[26,27],"tags":[],"_links":{"self":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/9918"}],"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=9918"}],"version-history":[{"count":3,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/9918\/revisions"}],"predecessor-version":[{"id":9933,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/9918\/revisions\/9933"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=9918"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=9918"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=9918"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}