1
1
/*
2
- * Copyright 2002-2010 the original author or authors.
2
+ * Copyright 2002-2012 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
35
35
import org .springframework .http .converter .StringHttpMessageConverter ;
36
36
import org .springframework .http .converter .feed .AtomFeedHttpMessageConverter ;
37
37
import org .springframework .http .converter .feed .RssChannelHttpMessageConverter ;
38
+ import org .springframework .http .converter .json .MappingJackson2HttpMessageConverter ;
38
39
import org .springframework .http .converter .json .MappingJacksonHttpMessageConverter ;
39
40
import org .springframework .http .converter .xml .Jaxb2RootElementHttpMessageConverter ;
40
41
import org .springframework .http .converter .xml .SourceHttpMessageConverter ;
65
66
import org .w3c .dom .Element ;
66
67
67
68
/**
68
- * A {@link BeanDefinitionParser} that provides the configuration for the
69
+ * A {@link BeanDefinitionParser} that provides the configuration for the
69
70
* {@code <annotation-driven/>} MVC namespace element.
70
71
*
71
72
* <p>This class registers the following {@link HandlerMapping}s:</p>
72
73
* <ul>
73
- * <li>{@link RequestMappingHandlerMapping}
74
+ * <li>{@link RequestMappingHandlerMapping}
74
75
* ordered at 0 for mapping requests to annotated controller methods.
75
- * <li>{@link BeanNameUrlHandlerMapping}
76
+ * <li>{@link BeanNameUrlHandlerMapping}
76
77
* ordered at 2 to map URL paths to controller bean names.
77
78
* </ul>
78
79
*
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
81
82
* {@code <resources>} MVC namespace elements.
82
- *
83
+ *
83
84
* <p>This class registers the following {@link HandlerAdapter}s:
84
85
* <ul>
85
- * <li>{@link RequestMappingHandlerAdapter}
86
+ * <li>{@link RequestMappingHandlerAdapter}
86
87
* for processing requests with annotated controller methods.
87
- * <li>{@link HttpRequestHandlerAdapter}
88
+ * <li>{@link HttpRequestHandlerAdapter}
88
89
* for processing requests with {@link HttpRequestHandler}s.
89
- * <li>{@link SimpleControllerHandlerAdapter}
90
+ * <li>{@link SimpleControllerHandlerAdapter}
90
91
* for processing requests with interface-based {@link Controller}s.
91
92
* </ul>
92
- *
93
+ *
93
94
* <p>This class registers the following {@link HandlerExceptionResolver}s:
94
95
* <ul>
95
- * <li>{@link ExceptionHandlerExceptionResolver} for handling exceptions
96
+ * <li>{@link ExceptionHandlerExceptionResolver} for handling exceptions
96
97
* through @{@link ExceptionHandler} methods.
97
- * <li>{@link ResponseStatusExceptionResolver} for exceptions annotated
98
+ * <li>{@link ResponseStatusExceptionResolver} for exceptions annotated
98
99
* with @{@link ResponseStatus}.
99
- * <li>{@link DefaultHandlerExceptionResolver} for resolving known Spring
100
+ * <li>{@link DefaultHandlerExceptionResolver} for resolving known Spring
100
101
* exception types
101
102
* </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
105
106
* instances of the following kind, unless custom instances are provided:
106
107
* <ul>
107
108
* <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
109
110
* 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
111
112
* libraries are available on the classpath.
112
113
* </ul>
113
- *
114
+ *
114
115
* @author Keith Donald
115
116
* @author Juergen Hoeller
116
117
* @author Arjen Poutsma
@@ -125,6 +126,10 @@ class AnnotationDrivenBeanDefinitionParser implements BeanDefinitionParser {
125
126
private static final boolean jaxb2Present =
126
127
ClassUtils .isPresent ("javax.xml.bind.Binder" , AnnotationDrivenBeanDefinitionParser .class .getClassLoader ());
127
128
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
+
128
133
private static final boolean jacksonPresent =
129
134
ClassUtils .isPresent ("org.codehaus.jackson.map.ObjectMapper" , AnnotationDrivenBeanDefinitionParser .class .getClassLoader ()) &&
130
135
ClassUtils .isPresent ("org.codehaus.jackson.JsonGenerator" , AnnotationDrivenBeanDefinitionParser .class .getClassLoader ());
@@ -158,7 +163,7 @@ public BeanDefinition parse(Element element, ParserContext parserContext) {
158
163
ManagedList <?> messageConverters = getMessageConverters (element , source , parserContext );
159
164
ManagedList <?> argumentResolvers = getArgumentResolvers (element , source , parserContext );
160
165
ManagedList <?> returnValueHandlers = getReturnValueHandlers (element , source , parserContext );
161
-
166
+
162
167
RootBeanDefinition methodAdapterDef = new RootBeanDefinition (RequestMappingHandlerAdapter .class );
163
168
methodAdapterDef .setSource (source );
164
169
methodAdapterDef .setRole (BeanDefinition .ROLE_INFRASTRUCTURE );
@@ -215,7 +220,7 @@ public BeanDefinition parse(Element element, ParserContext parserContext) {
215
220
parserContext .registerComponent (new BeanComponentDefinition (defaultExceptionResolver , defaultExceptionResolverName ));
216
221
parserContext .registerComponent (new BeanComponentDefinition (mappedCsInterceptorDef , mappedInterceptorName ));
217
222
218
- // Ensure BeanNameUrlHandlerMapping (SPR-8289) and default HandlerAdapters are not "turned off"
223
+ // Ensure BeanNameUrlHandlerMapping (SPR-8289) and default HandlerAdapters are not "turned off"
219
224
MvcNamespaceUtils .registerDefaultComponents (parserContext , source );
220
225
221
226
parserContext .popAndRegisterContainingComponent ();
@@ -309,7 +314,10 @@ private ManagedList<?> getMessageConverters(Element element, Object source, Pars
309
314
messageConverters
310
315
.add (createConverterBeanDefinition (Jaxb2RootElementHttpMessageConverter .class , source ));
311
316
}
312
- if (jacksonPresent ) {
317
+ if (jackson2Present ) {
318
+ messageConverters .add (createConverterBeanDefinition (MappingJackson2HttpMessageConverter .class , source ));
319
+ }
320
+ else if (jacksonPresent ) {
313
321
messageConverters .add (createConverterBeanDefinition (MappingJacksonHttpMessageConverter .class , source ));
314
322
}
315
323
if (romePresent ) {
0 commit comments