Skip to content

Commit 2d7323b

Browse files
committed
Update Spring MVC message converters documentation
Closes gh-35166
1 parent a61b297 commit 2d7323b

File tree

5 files changed

+42
-98
lines changed

5 files changed

+42
-98
lines changed

framework-docs/framework-docs.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,6 @@ dependencies {
6262
implementation(project(":spring-webmvc"))
6363
implementation(project(":spring-websocket"))
6464

65-
implementation("com.fasterxml.jackson.core:jackson-databind")
66-
implementation("com.fasterxml.jackson.module:jackson-module-parameter-names")
6765
implementation("com.github.ben-manes.caffeine:caffeine")
6866
implementation("com.mchange:c3p0:0.9.5.5")
6967
implementation("com.oracle.database.jdbc:ojdbc11")
@@ -81,4 +79,6 @@ dependencies {
8179
implementation("org.eclipse.jetty.websocket:jetty-websocket-jetty-api")
8280
implementation("org.jetbrains.kotlin:kotlin-stdlib")
8381
implementation("org.junit.jupiter:junit-jupiter-api")
82+
implementation("tools.jackson.core:jackson-databind")
83+
implementation("tools.jackson.dataformat:jackson-dataformat-xml")
8484
}

framework-docs/modules/ROOT/pages/web/webmvc/mvc-config/message-converters.adoc

Lines changed: 4 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -3,47 +3,10 @@
33

44
[.small]#xref:web/webflux/config.adoc#webflux-config-message-codecs[See equivalent in the Reactive stack]#
55

6-
You can set the `HttpMessageConverter` instances to use in Java configuration,
7-
replacing the ones used by default, by overriding
8-
{spring-framework-api}/web/servlet/config/annotation/WebMvcConfigurer.html#configureMessageConverters-java.util.List-[`configureMessageConverters()`].
9-
You can also customize the list of configured message converters at the end by overriding
10-
{spring-framework-api}/web/servlet/config/annotation/WebMvcConfigurer.html#extendMessageConverters-java.util.List-[`extendMessageConverters()`].
6+
You can configure the `HttpMessageConverter` instances to use by overriding
7+
{spring-framework-api}/web/servlet/config/annotation/WebMvcConfigurer.html#configureMessageConverters(org.springframework.http.converter.HttpMessageConverters.Builder)[`configureMessageConverters()`].
118

12-
TIP: In a Spring Boot application, the `WebMvcAutoConfiguration` adds any
13-
`HttpMessageConverter` beans it detects, in addition to default converters. Hence, in a
14-
Boot application, prefer to use the {spring-boot-docs-ref}/web/servlet.html#web.servlet.spring-mvc.message-converters[HttpMessageConverters]
15-
mechanism. Or alternatively, use `extendMessageConverters` to modify message converters
16-
at the end.
17-
18-
The following example adds XML and Jackson JSON converters with a customized `ObjectMapper`
19-
instead of the default ones:
9+
The following example configures custom Jackson JSON and XML converters with customized mappers instead of the default
10+
ones:
2011

2112
include-code::./WebConfiguration[tag=snippet,indent=0]
22-
23-
In the preceding example,
24-
{spring-framework-api}/http/converter/json/Jackson2ObjectMapperBuilder.html[`Jackson2ObjectMapperBuilder`]
25-
is used to create a common configuration for both `MappingJackson2HttpMessageConverter` and
26-
`MappingJackson2XmlHttpMessageConverter` with indentation enabled, a customized date format,
27-
and the registration of
28-
{jackson-github-org}/jackson-module-parameter-names[`jackson-module-parameter-names`],
29-
Which adds support for accessing parameter names (a feature added in Java 8).
30-
31-
This builder customizes Jackson's default properties as follows:
32-
33-
* {jackson-docs}/jackson-databind/javadoc/2.6/com/fasterxml/jackson/databind/DeserializationFeature.html#FAIL_ON_UNKNOWN_PROPERTIES[`DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES`] is disabled.
34-
* {jackson-docs}/jackson-databind/javadoc/2.6/com/fasterxml/jackson/databind/MapperFeature.html#DEFAULT_VIEW_INCLUSION[`MapperFeature.DEFAULT_VIEW_INCLUSION`] is disabled.
35-
36-
It also automatically registers the following well-known modules if they are detected on the classpath:
37-
38-
* {jackson-github-org}/jackson-datatype-jsr310[jackson-datatype-jsr310]: Support for Java 8 Date and Time API types.
39-
* {jackson-github-org}/jackson-datatype-jdk8[jackson-datatype-jdk8]: Support for other Java 8 types, such as `Optional`.
40-
* {jackson-github-org}/jackson-module-kotlin[jackson-module-kotlin]: Support for Kotlin classes and data classes.
41-
42-
NOTE: Enabling indentation with Jackson XML support requires
43-
https://search.maven.org/#search%7Cgav%7C1%7Cg%3A%22org.codehaus.woodstox%22%20AND%20a%3A%22woodstox-core-asl%22[`woodstox-core-asl`]
44-
dependency in addition to https://search.maven.org/#search%7Cga%7C1%7Ca%3A%22jackson-dataformat-xml%22[`jackson-dataformat-xml`] one.
45-
46-
Other interesting Jackson modules are available:
47-
48-
* https://github.com/zalando/jackson-datatype-money[jackson-datatype-money]: Support for `javax.money` types (unofficial module).
49-
* {jackson-github-org}/jackson-datatype-hibernate[jackson-datatype-hibernate]: Support for Hibernate-specific types and properties (including lazy-loading aspects).

framework-docs/src/main/java/org/springframework/docs/web/webmvc/mvcconfig/mvcconfigmessageconverters/WebConfiguration.java

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@
1717
package org.springframework.docs.web.webmvc.mvcconfig.mvcconfigmessageconverters;
1818

1919
import java.text.SimpleDateFormat;
20-
import java.util.List;
2120

22-
import com.fasterxml.jackson.module.paramnames.ParameterNamesModule;
21+
import tools.jackson.dataformat.xml.XmlMapper;
22+
import tools.jackson.databind.SerializationFeature;
23+
import tools.jackson.databind.json.JsonMapper;
2324

2425
import org.springframework.context.annotation.Configuration;
25-
import org.springframework.http.converter.HttpMessageConverter;
26-
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
27-
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
28-
import org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter;
26+
import org.springframework.http.converter.HttpMessageConverters;
27+
import org.springframework.http.converter.json.JacksonJsonHttpMessageConverter;
28+
import org.springframework.http.converter.xml.JacksonXmlHttpMessageConverter;
2929
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
3030

3131
@SuppressWarnings("removal")
@@ -34,13 +34,18 @@
3434
public class WebConfiguration implements WebMvcConfigurer {
3535

3636
@Override
37-
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
38-
Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder()
39-
.indentOutput(true)
40-
.dateFormat(new SimpleDateFormat("yyyy-MM-dd"))
41-
.modulesToInstall(new ParameterNamesModule());
42-
converters.add(new MappingJackson2HttpMessageConverter(builder.build()));
43-
converters.add(new MappingJackson2XmlHttpMessageConverter(builder.createXmlMapper(true).build()));
37+
public void configureMessageConverters(HttpMessageConverters.Builder builder) {
38+
JsonMapper jsonMapper = JsonMapper.builder()
39+
.findAndAddModules()
40+
.enable(SerializationFeature.INDENT_OUTPUT)
41+
.defaultDateFormat(new SimpleDateFormat("yyyy-MM-dd"))
42+
.build();
43+
XmlMapper xmlMapper = XmlMapper.builder()
44+
.findAndAddModules()
45+
.defaultUseWrapper(false)
46+
.build();
47+
builder.jsonMessageConverter(new JacksonJsonHttpMessageConverter(jsonMapper))
48+
.xmlMessageConverter(new JacksonXmlHttpMessageConverter(xmlMapper));
4449
}
4550
}
4651
// end::snippet[]

framework-docs/src/main/kotlin/org/springframework/docs/web/webmvc/mvcconfig/mvcconfigmessageconverters/WebConfiguration.kt

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,32 @@
22

33
package org.springframework.docs.web.webmvc.mvcconfig.mvcconfigmessageconverters
44

5-
import com.fasterxml.jackson.module.paramnames.ParameterNamesModule
65
import org.springframework.context.annotation.Configuration
7-
import org.springframework.http.converter.HttpMessageConverter
8-
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder
9-
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter
10-
import org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter
6+
import org.springframework.http.converter.HttpMessageConverters
7+
import org.springframework.http.converter.json.JacksonJsonHttpMessageConverter
8+
import org.springframework.http.converter.xml.JacksonXmlHttpMessageConverter
119
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer
10+
import tools.jackson.databind.SerializationFeature
11+
import tools.jackson.databind.json.JsonMapper
12+
import tools.jackson.dataformat.xml.XmlMapper
1213
import java.text.SimpleDateFormat
1314

1415
// tag::snippet[]
1516
@Configuration
1617
class WebConfiguration : WebMvcConfigurer {
1718

18-
override fun configureMessageConverters(converters: MutableList<HttpMessageConverter<*>>) {
19-
val builder = Jackson2ObjectMapperBuilder()
20-
.indentOutput(true)
21-
.dateFormat(SimpleDateFormat("yyyy-MM-dd"))
22-
.modulesToInstall(ParameterNamesModule())
23-
converters.add(MappingJackson2HttpMessageConverter(builder.build()))
24-
converters.add(MappingJackson2XmlHttpMessageConverter(builder.createXmlMapper(true).build()))
19+
override fun configureMessageConverters(builder: HttpMessageConverters.Builder) {
20+
val jsonMapper = JsonMapper.builder()
21+
.findAndAddModules()
22+
.enable(SerializationFeature.INDENT_OUTPUT)
23+
.defaultDateFormat(SimpleDateFormat("yyyy-MM-dd"))
24+
.build()
25+
val xmlMapper = XmlMapper.builder()
26+
.findAndAddModules()
27+
.defaultUseWrapper(false)
28+
.build()
29+
builder.jsonMessageConverter(JacksonJsonHttpMessageConverter(jsonMapper))
30+
.xmlMessageConverter(JacksonXmlHttpMessageConverter(xmlMapper))
2531
}
2632
}
2733
// end::snippet[]

framework-docs/src/main/resources/org/springframework/docs/web/webmvc/mvcconfig/mvcconfigmessageconverters/WebConfiguration.xml

Lines changed: 0 additions & 30 deletions
This file was deleted.

0 commit comments

Comments
 (0)