Skip to content

Commit c9a2dbd

Browse files
committed
Add Jackson 2 HttpMessageConverter and View
Jackson 2 uses completely new package names and new maven artifact ids. This change adds Jackson 2 as an optional dependency and also provides MappingJackson2HttpMessageConverter and MappingJackson2JsonView for use with the new version. The MVC namespace and the MVC Java config detect and use MappingJackson2HttpMessageConverter if Jackson 2 is present. Otherwise if Jackson 1.x is present, then MappingJacksonHttpMessageConverter is used. Issue: SPR-9302 Backport-Issue: SPR-9507 Backport-Commit: e63ca04
1 parent 2fa0e63 commit c9a2dbd

File tree

20 files changed

+1051
-161
lines changed

20 files changed

+1051
-161
lines changed

build-spring-framework/resources/changelog.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Changes in version 3.1.2 (2012-06-??)
1616
* ServletContextResource's getFile implementation falls back to getRealPath for non-existent files
1717
* fixed StandardServletMultipartResolver compatibility with Resin (only deleting actual file parts)
1818
* fix issue with parsing invalid Content-Type or Accept headers
19-
19+
* add Jackson 2 HttpMessageConverter and View types
2020

2121
Changes in version 3.1.1 (2012-02-16)
2222
-------------------------------------

org.springframework.web.servlet/.classpath

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323
<classpathentry kind="var" path="IVY_CACHE/org.apache.velocity/com.springsource.org.apache.velocity/1.5.0/com.springsource.org.apache.velocity-1.5.0.jar" sourcepath="/IVY_CACHE/org.apache.velocity/com.springsource.org.apache.velocity/1.5.0/com.springsource.org.apache.velocity-sources-1.5.0.jar"/>
2424
<classpathentry kind="var" path="IVY_CACHE/org.codehaus.jackson/com.springsource.org.codehaus.jackson.mapper/1.4.2/com.springsource.org.codehaus.jackson.mapper-1.4.2.jar" sourcepath="/IVY_CACHE/org.codehaus.jackson/com.springsource.org.codehaus.jackson.mapper/1.4.2/com.springsource.org.codehaus.jackson.mapper-sources-1.4.2.jar"/>
2525
<classpathentry kind="var" path="IVY_CACHE/org.codehaus.jackson/com.springsource.org.codehaus.jackson/1.4.2/com.springsource.org.codehaus.jackson-1.4.2.jar" sourcepath="/IVY_CACHE/org.codehaus.jackson/com.springsource.org.codehaus.jackson/1.4.2/com.springsource.org.codehaus.jackson-sources-1.4.2.jar"/>
26+
<classpathentry kind="var" path="IVY_CACHE/com.fasterxml.jackson.core/com.springsource.com.fasterxml.jackson.core.jackson-databind/2.0.2/com.springsource.com.fasterxml.jackson.core.jackson-databind-2.0.2.jar" sourcepath="IVY_CACHE/com.fasterxml.jackson.core/com.springsource.com.fasterxml.jackson.core.jackson-databind/2.0.2/com.springsource.com.fasterxml.jackson.core.jackson-databind-2.0.2-sources.jar"/>
27+
<classpathentry kind="var" path="IVY_CACHE/com.fasterxml.jackson.core/com.springsource.com.fasterxml.jackson.core.jackson-core/2.0.2/com.springsource.com.fasterxml.jackson.core.jackson-core-2.0.2.jar" sourcepath="IVY_CACHE/com.fasterxml.jackson.core/com.springsource.com.fasterxml.jackson.core.jackson-core/2.0.2/com.springsource.com.fasterxml.jackson.core.jackson-core-2.0.2-sources.jar"/>
28+
<classpathentry kind="var" path="IVY_CACHE/com.fasterxml.jackson.core/com.springsource.com.fasterxml.jackson.core.jackson-annotations/2.0.2/com.springsource.com.fasterxml.jackson.core.jackson-annotations-2.0.2.jar" sourcepath="IVY_CACHE/com.fasterxml.jackson.core/com.springsource.com.fasterxml.jackson.core.jackson-annotations/2.0.2/com.springsource.com.fasterxml.jackson.core.jackson-annotations-2.0.2-sources.jar"/>
2629
<classpathentry kind="var" path="IVY_CACHE/org.custommonkey.xmlunit/com.springsource.org.custommonkey.xmlunit/1.2.0/com.springsource.org.custommonkey.xmlunit-1.2.0.jar" sourcepath="/IVY_CACHE/org.custommonkey.xmlunit/com.springsource.org.custommonkey.xmlunit/1.2.0/com.springsource.org.custommonkey.xmlunit-sources-1.2.0.jar"/>
2730
<classpathentry kind="var" path="IVY_CACHE/org.dom4j/com.springsource.org.dom4j/1.6.1/com.springsource.org.dom4j-1.6.1.jar" sourcepath="/IVY_CACHE/org.dom4j/com.springsource.org.dom4j/1.6.1/com.springsource.org.dom4j-sources-1.6.1.jar"/>
2831
<classpathentry kind="var" path="IVY_CACHE/org.easymock/com.springsource.org.easymock/2.5.1/com.springsource.org.easymock-2.5.1.jar" sourcepath="/IVY_CACHE/org.easymock/com.springsource.org.easymock/2.5.1/com.springsource.org.easymock-sources-2.5.1.jar"/>

org.springframework.web.servlet/ivy.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@
6969
conf="optional, velocity->compile"/>
7070
<dependency org="org.codehaus.jackson" name="com.springsource.org.codehaus.jackson.mapper" rev="1.4.2"
7171
conf="optional, jackson->compile"/>
72+
<dependency org="com.fasterxml.jackson.core" name="com.springsource.com.fasterxml.jackson.core.jackson-databind" rev="2.0.2"
73+
conf="optional, jackson->compile"/>
7274
<dependency org="org.springframework" name="org.springframework.beans" rev="latest.integration"
7375
conf="compile->compile"/>
7476
<dependency org="org.springframework" name="org.springframework.context" rev="latest.integration"

org.springframework.web.servlet/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,12 @@
145145
<version>1.4.2</version>
146146
<optional>true</optional>
147147
</dependency>
148+
<dependency>
149+
<groupId>com.fasterxml.jackson.core</groupId>
150+
<artifactId>jackson-databind</artifactId>
151+
<version>2.0.2</version>
152+
<optional>true</optional>
153+
</dependency>
148154
<dependency>
149155
<groupId>org.springframework</groupId>
150156
<artifactId>spring-asm</artifactId>

org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/config/AnnotationDrivenBeanDefinitionParser.java

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2010 the original author or authors.
2+
* Copyright 2002-2012 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -35,6 +35,7 @@
3535
import org.springframework.http.converter.StringHttpMessageConverter;
3636
import org.springframework.http.converter.feed.AtomFeedHttpMessageConverter;
3737
import org.springframework.http.converter.feed.RssChannelHttpMessageConverter;
38+
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
3839
import org.springframework.http.converter.json.MappingJacksonHttpMessageConverter;
3940
import org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter;
4041
import org.springframework.http.converter.xml.SourceHttpMessageConverter;
@@ -65,52 +66,52 @@
6566
import org.w3c.dom.Element;
6667

6768
/**
68-
* A {@link BeanDefinitionParser} that provides the configuration for the
69+
* A {@link BeanDefinitionParser} that provides the configuration for the
6970
* {@code <annotation-driven/>} MVC namespace element.
7071
*
7172
* <p>This class registers the following {@link HandlerMapping}s:</p>
7273
* <ul>
73-
* <li>{@link RequestMappingHandlerMapping}
74+
* <li>{@link RequestMappingHandlerMapping}
7475
* ordered at 0 for mapping requests to annotated controller methods.
75-
* <li>{@link BeanNameUrlHandlerMapping}
76+
* <li>{@link BeanNameUrlHandlerMapping}
7677
* ordered at 2 to map URL paths to controller bean names.
7778
* </ul>
7879
*
79-
* <p><strong>Note:</strong> Additional HandlerMappings may be registered
80-
* as a result of using the {@code <view-controller>} or the
80+
* <p><strong>Note:</strong> Additional HandlerMappings may be registered
81+
* as a result of using the {@code <view-controller>} or the
8182
* {@code <resources>} MVC namespace elements.
82-
*
83+
*
8384
* <p>This class registers the following {@link HandlerAdapter}s:
8485
* <ul>
85-
* <li>{@link RequestMappingHandlerAdapter}
86+
* <li>{@link RequestMappingHandlerAdapter}
8687
* for processing requests with annotated controller methods.
87-
* <li>{@link HttpRequestHandlerAdapter}
88+
* <li>{@link HttpRequestHandlerAdapter}
8889
* for processing requests with {@link HttpRequestHandler}s.
89-
* <li>{@link SimpleControllerHandlerAdapter}
90+
* <li>{@link SimpleControllerHandlerAdapter}
9091
* for processing requests with interface-based {@link Controller}s.
9192
* </ul>
92-
*
93+
*
9394
* <p>This class registers the following {@link HandlerExceptionResolver}s:
9495
* <ul>
95-
* <li>{@link ExceptionHandlerExceptionResolver} for handling exceptions
96+
* <li>{@link ExceptionHandlerExceptionResolver} for handling exceptions
9697
* through @{@link ExceptionHandler} methods.
97-
* <li>{@link ResponseStatusExceptionResolver} for exceptions annotated
98+
* <li>{@link ResponseStatusExceptionResolver} for exceptions annotated
9899
* with @{@link ResponseStatus}.
99-
* <li>{@link DefaultHandlerExceptionResolver} for resolving known Spring
100+
* <li>{@link DefaultHandlerExceptionResolver} for resolving known Spring
100101
* exception types
101102
* </ul>
102-
*
103-
* <p>Both the {@link RequestMappingHandlerAdapter} and the
104-
* {@link ExceptionHandlerExceptionResolver} are configured with default
103+
*
104+
* <p>Both the {@link RequestMappingHandlerAdapter} and the
105+
* {@link ExceptionHandlerExceptionResolver} are configured with default
105106
* instances of the following kind, unless custom instances are provided:
106107
* <ul>
107108
* <li>A {@link DefaultFormattingConversionService}
108-
* <li>A {@link LocalValidatorFactoryBean} if a JSR-303 implementation is
109+
* <li>A {@link LocalValidatorFactoryBean} if a JSR-303 implementation is
109110
* available on the classpath
110-
* <li>A range of {@link HttpMessageConverter}s depending on what 3rd party
111+
* <li>A range of {@link HttpMessageConverter}s depending on what 3rd party
111112
* libraries are available on the classpath.
112113
* </ul>
113-
*
114+
*
114115
* @author Keith Donald
115116
* @author Juergen Hoeller
116117
* @author Arjen Poutsma
@@ -125,6 +126,10 @@ class AnnotationDrivenBeanDefinitionParser implements BeanDefinitionParser {
125126
private static final boolean jaxb2Present =
126127
ClassUtils.isPresent("javax.xml.bind.Binder", AnnotationDrivenBeanDefinitionParser.class.getClassLoader());
127128

129+
private static final boolean jackson2Present =
130+
ClassUtils.isPresent("com.fasterxml.jackson.databind.ObjectMapper", AnnotationDrivenBeanDefinitionParser.class.getClassLoader()) &&
131+
ClassUtils.isPresent("com.fasterxml.jackson.core.JsonGenerator", AnnotationDrivenBeanDefinitionParser.class.getClassLoader());
132+
128133
private static final boolean jacksonPresent =
129134
ClassUtils.isPresent("org.codehaus.jackson.map.ObjectMapper", AnnotationDrivenBeanDefinitionParser.class.getClassLoader()) &&
130135
ClassUtils.isPresent("org.codehaus.jackson.JsonGenerator", AnnotationDrivenBeanDefinitionParser.class.getClassLoader());
@@ -158,7 +163,7 @@ public BeanDefinition parse(Element element, ParserContext parserContext) {
158163
ManagedList<?> messageConverters = getMessageConverters(element, source, parserContext);
159164
ManagedList<?> argumentResolvers = getArgumentResolvers(element, source, parserContext);
160165
ManagedList<?> returnValueHandlers = getReturnValueHandlers(element, source, parserContext);
161-
166+
162167
RootBeanDefinition methodAdapterDef = new RootBeanDefinition(RequestMappingHandlerAdapter.class);
163168
methodAdapterDef.setSource(source);
164169
methodAdapterDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
@@ -215,7 +220,7 @@ public BeanDefinition parse(Element element, ParserContext parserContext) {
215220
parserContext.registerComponent(new BeanComponentDefinition(defaultExceptionResolver, defaultExceptionResolverName));
216221
parserContext.registerComponent(new BeanComponentDefinition(mappedCsInterceptorDef, mappedInterceptorName));
217222

218-
// Ensure BeanNameUrlHandlerMapping (SPR-8289) and default HandlerAdapters are not "turned off"
223+
// Ensure BeanNameUrlHandlerMapping (SPR-8289) and default HandlerAdapters are not "turned off"
219224
MvcNamespaceUtils.registerDefaultComponents(parserContext, source);
220225

221226
parserContext.popAndRegisterContainingComponent();
@@ -309,7 +314,10 @@ private ManagedList<?> getMessageConverters(Element element, Object source, Pars
309314
messageConverters
310315
.add(createConverterBeanDefinition(Jaxb2RootElementHttpMessageConverter.class, source));
311316
}
312-
if (jacksonPresent) {
317+
if (jackson2Present) {
318+
messageConverters.add(createConverterBeanDefinition(MappingJackson2HttpMessageConverter.class, source));
319+
}
320+
else if (jacksonPresent) {
313321
messageConverters.add(createConverterBeanDefinition(MappingJacksonHttpMessageConverter.class, source));
314322
}
315323
if (romePresent) {

0 commit comments

Comments
 (0)