Skip to content

Commit 1293768

Browse files
committed
Add a section about message converters customization in the refdoc
Issue: SPR-13411
1 parent af905aa commit 1293768

File tree

1 file changed

+98
-0
lines changed

1 file changed

+98
-0
lines changed

src/asciidoc/web-mvc.adoc

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4566,6 +4566,28 @@ classpath.
45664566
.. `RssChannelHttpMessageConverter` converts RSS feeds -- added if Rome is present on
45674567
the classpath.
45684568

4569+
See <<mvc-config-message-converters>> for more information about how to customize these
4570+
default converters.
4571+
4572+
[NOTE]
4573+
====
4574+
Jackson JSON and XML converters are created using `ObjectMapper` instances created by
4575+
{javadoc-baseurl}/org/springframework/http/converter/json/Jackson2ObjectMapperBuilder.html[`Jackson2ObjectMapperBuilder`]
4576+
in order to provide a better default configuration.
4577+
4578+
This builder customizes Jackson's default properties with the following ones:
4579+
4580+
. http://fasterxml.github.io/jackson-databind/javadoc/2.6/com/fasterxml/jackson/databind/DeserializationFeature.html#FAIL_ON_UNKNOWN_PROPERTIES[`DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES`] is disabled.
4581+
. http://fasterxml.github.io/jackson-databind/javadoc/2.6/com/fasterxml/jackson/databind/MapperFeature.html#DEFAULT_VIEW_INCLUSION[`MapperFeature.DEFAULT_VIEW_INCLUSION`] is disabled.
4582+
4583+
It also automatically registers the following well-known modules if they are detected on the classpath:
4584+
4585+
. https://github.com/FasterXML/jackson-datatype-jdk7[jackson-datatype-jdk7]: support for Java 7 types like `java.nio.file.Path`.
4586+
. https://github.com/FasterXML/jackson-datatype-joda[jackson-datatype-joda]: support for Joda-Time types.
4587+
. https://github.com/FasterXML/jackson-datatype-jsr310[jackson-datatype-jsr310]: support for Java 8 Date & Time API types.
4588+
. https://github.com/FasterXML/jackson-datatype-jdk8[jackson-datatype-jdk8]: support for other Java 8 types like `Optional`.
4589+
====
4590+
45694591

45704592

45714593
[[mvc-config-customize]]
@@ -5299,6 +5321,82 @@ And the same in XML, use the `<mvc:path-matching>` element:
52995321

53005322

53015323

5324+
[[mvc-config-message-converters]]
5325+
=== Message Converters
5326+
5327+
Customization of `HttpMessageConverter` can be achieved in Java config by overriding
5328+
{javadoc-baseurl}/org/springframework/web/servlet/config/annotation/WebMvcConfigurerAdapter.html#configureMessageConverters-java.util.List-[`configureMessageConverters()`]
5329+
if you want to replace the default converters created by Spring MVC, or by overriding
5330+
{javadoc-baseurl}/org/springframework/web/servlet/config/annotation/WebMvcConfigurerAdapter.html#extendMessageConverters-java.util.List-[`extendMessageConverters()`]
5331+
if you just want to customize them or add additional converters to the default ones.
5332+
5333+
Below is an example that adds Jackson JSON and XML converters with a customized
5334+
`ObjectMapper` instead of default ones:
5335+
5336+
[source,java,indent=0]
5337+
[subs="verbatim,quotes"]
5338+
----
5339+
@Configuration
5340+
@EnableWebMvc
5341+
public class WebConfiguration extends WebMvcConfigurerAdapter {
5342+
5343+
@Override
5344+
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
5345+
Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder()
5346+
.indentOutput(true)
5347+
.dateFormat(new SimpleDateFormat("yyyy-MM-dd"))
5348+
.modulesToInstall(new ParameterNamesModule());
5349+
converters.add(new MappingJackson2HttpMessageConverter(builder.build()));
5350+
converters.add(new MappingJackson2XmlHttpMessageConverter(builder.xml().build()));
5351+
}
5352+
5353+
}
5354+
----
5355+
5356+
In this example, `Jackson2ObjectMapperBuilder` is used to create a common configuration for
5357+
both `MappingJackson2HttpMessageConverter` and `MappingJackson2XmlHttpMessageConverter` with
5358+
indentation enabled, a customized date format and the registration of
5359+
https://github.com/FasterXML/jackson-module-parameter-names[jackson-module-parameter-names]
5360+
that adds support for accessing parameter names (feature added in Java 8).
5361+
5362+
[NOTE]
5363+
====
5364+
Enabling indentation with Jackson XML support requires
5365+
http://search.maven.org/#search%7Cgav%7C1%7Cg%3A%22org.codehaus.woodstox%22%20AND%20a%3A%22woodstox-core-asl%22[`woodstox-core-asl`]
5366+
dependency in addition to http://search.maven.org/#search%7Cga%7C1%7Ca%3A%22jackson-dataformat-xml%22[`jackson-dataformat-xml`] one.
5367+
====
5368+
5369+
Other interesting Jackson modules are available:
5370+
5371+
. https://github.com/zalando/jackson-datatype-money[jackson-datatype-money]: support for `javax.money` types (unofficial module)
5372+
. https://github.com/FasterXML/jackson-datatype-hibernate[jackson-datatype-hibernate]: support for Hibernate specific types and properties (including lazy-loading aspects)
5373+
5374+
It is also possible to do the same in XML:
5375+
5376+
[source,xml,indent=0]
5377+
[subs="verbatim,quotes"]
5378+
----
5379+
<mvc:annotation-driven>
5380+
<mvc:message-converters>
5381+
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
5382+
<property name="objectMapper" ref="objectMapper" />
5383+
</bean>
5384+
<bean class="org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter">
5385+
<property name="objectMapper" ref="xmlMapper" />
5386+
</bean>
5387+
</mvc:message-converters>
5388+
</mvc:annotation-driven>
5389+
5390+
<bean id="objectMapper" class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean"
5391+
p:indentOutput="true"
5392+
p:simpleDateFormat="yyyy-MM-dd"
5393+
p:modulesToInstall="com.fasterxml.jackson.module.paramnames.ParameterNamesModule" />
5394+
5395+
<bean id="xmlMapper" parent="objectMapper" p:createXmlMapper="true" />
5396+
----
5397+
5398+
5399+
53025400
[[mvc-config-advanced-java]]
53035401
=== Advanced Customizations with MVC Java Config
53045402
As you can see from the above examples, MVC Java config and the MVC namespace provide

0 commit comments

Comments
 (0)