Support Jackson based XML serialization and Jackson2ObjectMapperBuilder
This commit introduces support for Jackson based XML serialization, using the
new MappingJackson2XmlHttpMessageConverter provided by Spring Framework
4.1. It is automatically activated when Jackson XML extension is detected on the
classpath.
Jackson2ObjectMapperBuilder is now used to create ObjectMapper and XmlMapper
instances with the following customized properties:
- MapperFeature.DEFAULT_VIEW_INCLUSION is disabled
- DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES is disabled
JodaModuleAutoConfiguration and Jsr310ModuleAutoConfiguration have been removed
since their behaviors are now handled directly by the ObjectMapper builder.
In addition to the existing @Bean of type ObjectMapper support, it is now
possible to customize Jackson based serialization properties by declaring
a @Bean of type Jackson2ObjectMapperBuilder.
Fixes gh-1237
Fixes gh-1580
Fixes gh-1644
pull/1600/merge
Sebastien Deleuze12 years agocommitted byAndy Wilkinson
@ -141,7 +142,8 @@ public class HttpMessageConverters implements Iterable<HttpMessageConverter<?>>
@@ -141,7 +142,8 @@ public class HttpMessageConverters implements Iterable<HttpMessageConverter<?>>
@ -59,11 +62,13 @@ public class HttpMessageConvertersAutoConfigurationTests {
@@ -59,11 +62,13 @@ public class HttpMessageConvertersAutoConfigurationTests {
@ -73,9 +78,25 @@ public class HttpMessageConvertersAutoConfigurationTests {
@@ -73,9 +78,25 @@ public class HttpMessageConvertersAutoConfigurationTests {
@ -128,13 +149,27 @@ public class HttpMessageConvertersAutoConfigurationTests {
@@ -128,13 +149,27 @@ public class HttpMessageConvertersAutoConfigurationTests {
@ -643,15 +643,40 @@ default as long as Jackson2 is on the classpath. For example:
@@ -643,15 +643,40 @@ default as long as Jackson2 is on the classpath. For example:
As long as `MyThing` can be serialized by Jackson2 (e.g. a normal POJO or Groovy object)
then `http://localhost:8080/thing` will serve a JSON representation of it by default.
Sometimes in a browser you might see XML responses (but by default only if `MyThing` was
a JAXB object) because browsers tend to send accept headers that prefer XML.
Sometimes in a browser you might see XML responses because browsers tend to send accept
headers that prefer XML.
[[howto-write-an-xml-rest-service]]
=== Write an XML REST service
Since JAXB is in the JDK the same example as we used for JSON would work, as long as the
`MyThing` was annotated as `@XmlRootElement`:
If you have the Jackson XML extension (`jackson-dataformat-xml`) on the classpath, it will
be used to render XML responses and the very same example as we used for JSON would work.
To use it, add the following dependency to your project:
@ -670,14 +695,21 @@ To get the server to render XML instead of JSON you might have to send an
@@ -670,14 +695,21 @@ To get the server to render XML instead of JSON you might have to send an
[[howto-customize-the-jackson-objectmapper]]
=== Customize the Jackson ObjectMapper
Spring MVC (client and server side) uses `HttpMessageConverters` to negotiate content
conversion in an HTTP exchange. If Jackson is on the classpath you already get a default
converter with a vanilla `ObjectMapper`. Spring Boot has some features to make it easier
to customize this behavior.
conversion in an HTTP exchange. If Jackson is on the classpath you already get the
default converter(s) provided by `Jackson2ObjectMapperBuilder`.
You can configure the vanilla `ObjectMapper` using the environment. Jackson provides an
extensive suite of simple on/off features that can be used to configure various aspects
of its processing. These features are described in five enums in Jackson which map onto
properties in the environment:
The `ObjectMapper` (or `XmlMapper` for Jackson XML converter) instance created by default
have the following customized properties:
* MapperFeature.DEFAULT_VIEW_INCLUSION is disabled
* DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES is disabled
Spring Boot has also some features to make it easier to customize this behavior.
You can configure the `ObjectMapper` and `XmlMapper` instances using the environment.
Jackson provides an extensive suite of simple on/off features that can be used to
configure various aspects of its processing. These features are described in five enums in
Jackson which map onto properties in the environment:
|===
|Jackson enum|Environment property
@ -698,14 +730,17 @@ properties in the environment:
@@ -698,14 +730,17 @@ properties in the environment:
@ -885,7 +885,8 @@ formatters, view controllers etc.) you can add your own `@Bean` of type
@@ -885,7 +885,8 @@ formatters, view controllers etc.) you can add your own `@Bean` of type
==== HttpMessageConverters
Spring MVC uses the `HttpMessageConverter` interface to convert HTTP requests and
responses. Sensible defaults are included out of the box, for example Objects can be
automatically converted to JSON (using the Jackson library) or XML (using JAXB).
automatically converted to JSON (using the Jackson library) or XML (using the Jackson
XML extension if available, else using JAXB).
If you need to add or customize converters you can use Spring Boot's