diff --git a/IV. Spring Boot features/26.1.2. HttpMessageConverters.md b/IV. Spring Boot features/26.1.2. HttpMessageConverters.md index e69de29..68b076d 100644 --- a/IV. Spring Boot features/26.1.2. HttpMessageConverters.md +++ b/IV. Spring Boot features/26.1.2. HttpMessageConverters.md @@ -0,0 +1,22 @@ +### 26.1.2. HttpMessageConverters + +Spring MVC使用HttpMessageConverter接口转换HTTP请求和响应。合理的缺省值被包含的恰到好处(out of the box),例如对象可以自动转换为JSON(使用Jackson库)或XML(如果Jackson XML扩展可用则使用它,否则使用JAXB)。字符串默认使用UTF-8编码。 + +如果需要添加或自定义转换器,你可以使用Spring Boot的HttpMessageConverters类: +```java +import org.springframework.boot.autoconfigure.web.HttpMessageConverters; +import org.springframework.context.annotation.*; +import org.springframework.http.converter.*; + +@Configuration +public class MyConfiguration { + + @Bean + public HttpMessageConverters customConverters() { + HttpMessageConverter additional = ... + HttpMessageConverter another = ... + return new HttpMessageConverters(additional, another); + } +} +``` +任何在上下文中出现的HttpMessageConverter bean将会添加到converters列表,你可以通过这种方式覆盖默认的转换器(converters)。