Message Converters

你可以设置 HttpMessageConverter 实例在 Java 配置中使用,从而覆盖默认使用的实例,方法是覆盖https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/servlet/config/annotation/WebMvcConfigurer.html#configureMessageConverters-java.util.List-[configureMessageConverters()]。你还可以通过覆盖https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/servlet/config/annotation/WebMvcConfigurer.html#extendMessageConverters-java.util.List-[extendMessageConverters()] 来自定义配置的消息转换程序的最终列表。

You can set the HttpMessageConverter instances to use in Java configuration, replacing the ones used by default, by overriding configureMessageConverters(). You can also customize the list of configured message converters at the end by overriding extendMessageConverters().

在 Spring Boot 应用程序中,除默认转换器外, WebMvcAutoConfiguration 还会添加它检测到的任何 HttpMessageConverter bean。因此,在 Boot 应用程序中,更倾向于使用 [|HttpMessageConverters|](https://docs.spring.io/spring-boot/docs/current/reference/html/web.html#web.servlet.spring-mvc.message-converters) 机制。或者,使用 extendMessageConverters 来最终修改消息转换器。

In a Spring Boot application, the WebMvcAutoConfiguration adds any HttpMessageConverter beans it detects, in addition to default converters. Hence, in a Boot application, prefer to use the HttpMessageConverters mechanism. Or alternatively, use extendMessageConverters to modify message converters at the end.

以下示例添加了一个 XML 和一个 Jackson JSON 转换器,其中包含一个自定义的 ObjectMapper,而不是默认的转换器:

The following example adds XML and Jackson JSON converters with a customized ObjectMapper instead of the default ones:

  • Java

  • Kotlin

  • Xml

@Configuration
public class WebConfiguration implements WebMvcConfigurer {

	@Override
	public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
		Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder()
				.indentOutput(true)
				.dateFormat(new SimpleDateFormat("yyyy-MM-dd"))
				.modulesToInstall(new ParameterNamesModule());
		converters.add(new MappingJackson2HttpMessageConverter(builder.build()));
		converters.add(new MappingJackson2XmlHttpMessageConverter(builder.createXmlMapper(true).build()));
	}
}
@Configuration
class WebConfiguration : WebMvcConfigurer {

	override fun configureMessageConverters(converters: MutableList<HttpMessageConverter<*>>) {
		val builder = Jackson2ObjectMapperBuilder()
			.indentOutput(true)
			.dateFormat(SimpleDateFormat("yyyy-MM-dd"))
			.modulesToInstall(ParameterNamesModule())
		converters.add(MappingJackson2HttpMessageConverter(builder.build()))
		converters.add(MappingJackson2XmlHttpMessageConverter(builder.createXmlMapper(true).build()))
	}
}
<mvc:annotation-driven>
	<mvc:message-converters>
		<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
			<property name="objectMapper" ref="objectMapper"/>
		</bean>
		<bean class="org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter">
			<property name="objectMapper" ref="xmlMapper"/>
		</bean>
	</mvc:message-converters>
</mvc:annotation-driven>

<bean id="objectMapper" class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean"
	  p:indentOutput="true"
	  p:simpleDateFormat="yyyy-MM-dd"
	  p:modulesToInstall="com.fasterxml.jackson.module.paramnames.ParameterNamesModule"/>

<bean id="xmlMapper" parent="objectMapper" p:createXmlMapper="true"/>

在上一个示例中,将 SpringMVC 提供的https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/http/converter/json/Jackson2ObjectMapperBuilder.html[Jackson2ObjectMapperBuilder] 用于给 MappingJackson2HttpMessageConverterMappingJackson2XmlHttpMessageConverter 创建一个通用配置,同时启用缩进、自定义的日期格式,并注册https://github.com/FasterXML/jackson-module-parameter-names[jackson-module-parameter-names],添加对访问参数名称的支持(此功能在 Java 8 中添加)。

In the preceding example, Jackson2ObjectMapperBuilder is used to create a common configuration for both MappingJackson2HttpMessageConverter and MappingJackson2XmlHttpMessageConverter with indentation enabled, a customized date format, and the registration of jackson-module-parameter-names, Which adds support for accessing parameter names (a feature added in Java 8).

此构建器按照以下方式自定义 Jackson 的默认属性:

This builder customizes Jackson’s default properties as follows:

如果在类路径中检测到以下著名的模块,它还将自动注册这些模块:

It also automatically registers the following well-known modules if they are detected on the classpath:

启用 Jackson XML 支持的缩进需要 woodstox-core-asl 依赖项以及 jackson-dataformat-xml 依赖项。

Enabling indentation with Jackson XML support requires woodstox-core-asl dependency in addition to jackson-dataformat-xml one.

还提供其他有用的 Jackson 模块:

Other interesting Jackson modules are available: