Update Spring MVC message converters documentation
Build and Deploy Snapshot / Build and Deploy Snapshot (push) Waiting to run Details
Build and Deploy Snapshot / Verify (push) Blocked by required conditions Details
Deploy Docs / Dispatch docs deployment (push) Waiting to run Details

Closes gh-35166
This commit is contained in:
Sébastien Deleuze 2025-07-07 18:15:44 +02:00
parent a61b297967
commit 2d7323bb7f
5 changed files with 42 additions and 98 deletions

View File

@ -62,8 +62,6 @@ dependencies {
implementation(project(":spring-webmvc")) implementation(project(":spring-webmvc"))
implementation(project(":spring-websocket")) implementation(project(":spring-websocket"))
implementation("com.fasterxml.jackson.core:jackson-databind")
implementation("com.fasterxml.jackson.module:jackson-module-parameter-names")
implementation("com.github.ben-manes.caffeine:caffeine") implementation("com.github.ben-manes.caffeine:caffeine")
implementation("com.mchange:c3p0:0.9.5.5") implementation("com.mchange:c3p0:0.9.5.5")
implementation("com.oracle.database.jdbc:ojdbc11") implementation("com.oracle.database.jdbc:ojdbc11")
@ -81,4 +79,6 @@ dependencies {
implementation("org.eclipse.jetty.websocket:jetty-websocket-jetty-api") implementation("org.eclipse.jetty.websocket:jetty-websocket-jetty-api")
implementation("org.jetbrains.kotlin:kotlin-stdlib") implementation("org.jetbrains.kotlin:kotlin-stdlib")
implementation("org.junit.jupiter:junit-jupiter-api") implementation("org.junit.jupiter:junit-jupiter-api")
implementation("tools.jackson.core:jackson-databind")
implementation("tools.jackson.dataformat:jackson-dataformat-xml")
} }

View File

@ -3,47 +3,10 @@
[.small]#xref:web/webflux/config.adoc#webflux-config-message-codecs[See equivalent in the Reactive stack]# [.small]#xref:web/webflux/config.adoc#webflux-config-message-codecs[See equivalent in the Reactive stack]#
You can set the `HttpMessageConverter` instances to use in Java configuration, You can configure the `HttpMessageConverter` instances to use by overriding
replacing the ones used by default, by overriding {spring-framework-api}/web/servlet/config/annotation/WebMvcConfigurer.html#configureMessageConverters(org.springframework.http.converter.HttpMessageConverters.Builder)[`configureMessageConverters()`].
{spring-framework-api}/web/servlet/config/annotation/WebMvcConfigurer.html#configureMessageConverters-java.util.List-[`configureMessageConverters()`].
You can also customize the list of configured message converters at the end by overriding
{spring-framework-api}/web/servlet/config/annotation/WebMvcConfigurer.html#extendMessageConverters-java.util.List-[`extendMessageConverters()`].
TIP: In a Spring Boot application, the `WebMvcAutoConfiguration` adds any The following example configures custom Jackson JSON and XML converters with customized mappers instead of the default
`HttpMessageConverter` beans it detects, in addition to default converters. Hence, in a ones:
Boot application, prefer to use the {spring-boot-docs-ref}/web/servlet.html#web.servlet.spring-mvc.message-converters[HttpMessageConverters]
mechanism. Or alternatively, use `extendMessageConverters` to modify message converters
at the end.
The following example adds XML and Jackson JSON converters with a customized `ObjectMapper`
instead of the default ones:
include-code::./WebConfiguration[tag=snippet,indent=0] include-code::./WebConfiguration[tag=snippet,indent=0]
In the preceding example,
{spring-framework-api}/http/converter/json/Jackson2ObjectMapperBuilder.html[`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-github-org}/jackson-module-parameter-names[`jackson-module-parameter-names`],
Which adds support for accessing parameter names (a feature added in Java 8).
This builder customizes Jackson's default properties as follows:
* {jackson-docs}/jackson-databind/javadoc/2.6/com/fasterxml/jackson/databind/DeserializationFeature.html#FAIL_ON_UNKNOWN_PROPERTIES[`DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES`] is disabled.
* {jackson-docs}/jackson-databind/javadoc/2.6/com/fasterxml/jackson/databind/MapperFeature.html#DEFAULT_VIEW_INCLUSION[`MapperFeature.DEFAULT_VIEW_INCLUSION`] is disabled.
It also automatically registers the following well-known modules if they are detected on the classpath:
* {jackson-github-org}/jackson-datatype-jsr310[jackson-datatype-jsr310]: Support for Java 8 Date and Time API types.
* {jackson-github-org}/jackson-datatype-jdk8[jackson-datatype-jdk8]: Support for other Java 8 types, such as `Optional`.
* {jackson-github-org}/jackson-module-kotlin[jackson-module-kotlin]: Support for Kotlin classes and data classes.
NOTE: Enabling indentation with Jackson XML support requires
https://search.maven.org/#search%7Cgav%7C1%7Cg%3A%22org.codehaus.woodstox%22%20AND%20a%3A%22woodstox-core-asl%22[`woodstox-core-asl`]
dependency in addition to https://search.maven.org/#search%7Cga%7C1%7Ca%3A%22jackson-dataformat-xml%22[`jackson-dataformat-xml`] one.
Other interesting Jackson modules are available:
* https://github.com/zalando/jackson-datatype-money[jackson-datatype-money]: Support for `javax.money` types (unofficial module).
* {jackson-github-org}/jackson-datatype-hibernate[jackson-datatype-hibernate]: Support for Hibernate-specific types and properties (including lazy-loading aspects).

View File

@ -17,15 +17,15 @@
package org.springframework.docs.web.webmvc.mvcconfig.mvcconfigmessageconverters; package org.springframework.docs.web.webmvc.mvcconfig.mvcconfigmessageconverters;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.List;
import com.fasterxml.jackson.module.paramnames.ParameterNamesModule; import tools.jackson.dataformat.xml.XmlMapper;
import tools.jackson.databind.SerializationFeature;
import tools.jackson.databind.json.JsonMapper;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter; import org.springframework.http.converter.HttpMessageConverters;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder; import org.springframework.http.converter.json.JacksonJsonHttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; import org.springframework.http.converter.xml.JacksonXmlHttpMessageConverter;
import org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@SuppressWarnings("removal") @SuppressWarnings("removal")
@ -34,13 +34,18 @@ import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
public class WebConfiguration implements WebMvcConfigurer { public class WebConfiguration implements WebMvcConfigurer {
@Override @Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) { public void configureMessageConverters(HttpMessageConverters.Builder builder) {
Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder() JsonMapper jsonMapper = JsonMapper.builder()
.indentOutput(true) .findAndAddModules()
.dateFormat(new SimpleDateFormat("yyyy-MM-dd")) .enable(SerializationFeature.INDENT_OUTPUT)
.modulesToInstall(new ParameterNamesModule()); .defaultDateFormat(new SimpleDateFormat("yyyy-MM-dd"))
converters.add(new MappingJackson2HttpMessageConverter(builder.build())); .build();
converters.add(new MappingJackson2XmlHttpMessageConverter(builder.createXmlMapper(true).build())); XmlMapper xmlMapper = XmlMapper.builder()
.findAndAddModules()
.defaultUseWrapper(false)
.build();
builder.jsonMessageConverter(new JacksonJsonHttpMessageConverter(jsonMapper))
.xmlMessageConverter(new JacksonXmlHttpMessageConverter(xmlMapper));
} }
} }
// end::snippet[] // end::snippet[]

View File

@ -2,26 +2,32 @@
package org.springframework.docs.web.webmvc.mvcconfig.mvcconfigmessageconverters package org.springframework.docs.web.webmvc.mvcconfig.mvcconfigmessageconverters
import com.fasterxml.jackson.module.paramnames.ParameterNamesModule
import org.springframework.context.annotation.Configuration import org.springframework.context.annotation.Configuration
import org.springframework.http.converter.HttpMessageConverter import org.springframework.http.converter.HttpMessageConverters
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder import org.springframework.http.converter.json.JacksonJsonHttpMessageConverter
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter import org.springframework.http.converter.xml.JacksonXmlHttpMessageConverter
import org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer import org.springframework.web.servlet.config.annotation.WebMvcConfigurer
import tools.jackson.databind.SerializationFeature
import tools.jackson.databind.json.JsonMapper
import tools.jackson.dataformat.xml.XmlMapper
import java.text.SimpleDateFormat import java.text.SimpleDateFormat
// tag::snippet[] // tag::snippet[]
@Configuration @Configuration
class WebConfiguration : WebMvcConfigurer { class WebConfiguration : WebMvcConfigurer {
override fun configureMessageConverters(converters: MutableList<HttpMessageConverter<*>>) { override fun configureMessageConverters(builder: HttpMessageConverters.Builder) {
val builder = Jackson2ObjectMapperBuilder() val jsonMapper = JsonMapper.builder()
.indentOutput(true) .findAndAddModules()
.dateFormat(SimpleDateFormat("yyyy-MM-dd")) .enable(SerializationFeature.INDENT_OUTPUT)
.modulesToInstall(ParameterNamesModule()) .defaultDateFormat(SimpleDateFormat("yyyy-MM-dd"))
converters.add(MappingJackson2HttpMessageConverter(builder.build())) .build()
converters.add(MappingJackson2XmlHttpMessageConverter(builder.createXmlMapper(true).build())) val xmlMapper = XmlMapper.builder()
.findAndAddModules()
.defaultUseWrapper(false)
.build()
builder.jsonMessageConverter(JacksonJsonHttpMessageConverter(jsonMapper))
.xmlMessageConverter(JacksonXmlHttpMessageConverter(xmlMapper))
} }
} }
// end::snippet[] // end::snippet[]

View File

@ -1,30 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- tag::snippet[] -->
<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"/>
<!-- end::snippet[] -->
</beans>