spring_reference/IX. ‘How-to’ guides/65.4. Customize the @Respon...

8 lines
1.3 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

### 65.4. 自定义@ResponseBody渲染
Spring使用HttpMessageConverters渲染`@ResponseBody`(或来自`@RestController`的响应。你可以通过在Spring Boot上下文中添加该类型的beans来贡献其他的转换器。如果你添加的bean类型默认已经包含了像用于JSON转换的MappingJackson2HttpMessageConverter那它将替换默认的。Spring Boot提供一个方便的HttpMessageConverters类型的bean它有一些有用的方法可以访问默认的和用户增强的message转换器有用比如你想要手动将它们注入到一个自定义的`RestTemplate`)。
在通常的MVC用例中任何你提供的WebMvcConfigurerAdapter beans通过覆盖configureMessageConverters方法也能贡献转换器但不同于通常的MVC你可以只提供你需要的转换器因为Spring Boot使用相同的机制来贡献它默认的转换器。最终如果你通过提供自己的` @EnableWebMvc`注解覆盖Spring Boot默认的MVC配置那你就可以完全控制并使用来自WebMvcConfigurationSupport的getMessageConverters手动做任何事。
具体参考[WebMvcAutoConfiguration](http://github.com/spring-projects/spring-boot/tree/master/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/WebMvcAutoConfiguration.java)源码。