ThinkChat🤖让你学习和工作更高效,注册即送10W Token,即刻开启你的AI之旅 广告
# 27.1.2. HttpMessageConverters Spring MVC使用`HttpMessageConverter`接口转换HTTP请求和响应,合适的默认配置可以开箱即用,例如对象自动转换为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)。