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

23 lines
1.1 KiB
Markdown
Raw 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.

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