spring_reference/IV. Spring Boot features/26.1.2. HttpMessageConverte...

23 lines
1.1 KiB
Markdown
Raw Normal View History

### 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