56
56
* {@link #setCustomArgumentResolvers} and {@link #setCustomReturnValueHandlers}.
57
57
* Or alternatively to re-configure all argument and return value types use
58
58
* {@link #setArgumentResolvers} and {@link #setReturnValueHandlers(List)}.
59
- *
59
+ *
60
60
* @author Rossen Stoyanchev
61
61
* @since 3.1
62
62
*/
@@ -73,17 +73,17 @@ public class ExceptionHandlerExceptionResolver extends AbstractHandlerMethodExce
73
73
new ConcurrentHashMap <Class <?>, ExceptionHandlerMethodResolver >();
74
74
75
75
private HandlerMethodArgumentResolverComposite argumentResolvers ;
76
-
76
+
77
77
private HandlerMethodReturnValueHandlerComposite returnValueHandlers ;
78
78
79
79
/**
80
80
* Default constructor.
81
81
*/
82
82
public ExceptionHandlerExceptionResolver () {
83
-
83
+
84
84
StringHttpMessageConverter stringHttpMessageConverter = new StringHttpMessageConverter ();
85
85
stringHttpMessageConverter .setWriteAcceptCharset (false ); // See SPR-7316
86
-
86
+
87
87
this .messageConverters = new ArrayList <HttpMessageConverter <?>>();
88
88
this .messageConverters .add (new ByteArrayHttpMessageConverter ());
89
89
this .messageConverters .add (stringHttpMessageConverter );
@@ -93,13 +93,13 @@ public ExceptionHandlerExceptionResolver() {
93
93
94
94
/**
95
95
* Provide resolvers for custom argument types. Custom resolvers are ordered
96
- * after built-in ones. To override the built-in support for argument
96
+ * after built-in ones. To override the built-in support for argument
97
97
* resolution use {@link #setArgumentResolvers} instead.
98
98
*/
99
99
public void setCustomArgumentResolvers (List <HandlerMethodArgumentResolver > argumentResolvers ) {
100
100
this .customArgumentResolvers = argumentResolvers ;
101
101
}
102
-
102
+
103
103
/**
104
104
* Return the custom argument resolvers, or {@code null}.
105
105
*/
@@ -120,9 +120,9 @@ public void setArgumentResolvers(List<HandlerMethodArgumentResolver> argumentRes
120
120
this .argumentResolvers .addResolvers (argumentResolvers );
121
121
}
122
122
}
123
-
123
+
124
124
/**
125
- * Return the configured argument resolvers, or possibly {@code null} if
125
+ * Return the configured argument resolvers, or possibly {@code null} if
126
126
* not initialized yet via {@link #afterPropertiesSet()}.
127
127
*/
128
128
public HandlerMethodArgumentResolverComposite getArgumentResolvers () {
@@ -146,7 +146,7 @@ public List<HandlerMethodReturnValueHandler> getCustomReturnValueHandlers() {
146
146
}
147
147
148
148
/**
149
- * Configure the complete list of supported return value types thus
149
+ * Configure the complete list of supported return value types thus
150
150
* overriding handlers that would otherwise be configured by default.
151
151
*/
152
152
public void setReturnValueHandlers (List <HandlerMethodReturnValueHandler > returnValueHandlers ) {
@@ -158,23 +158,23 @@ public void setReturnValueHandlers(List<HandlerMethodReturnValueHandler> returnV
158
158
this .returnValueHandlers .addHandlers (returnValueHandlers );
159
159
}
160
160
}
161
-
161
+
162
162
/**
163
- * Return the configured handlers, or possibly {@code null} if not
163
+ * Return the configured handlers, or possibly {@code null} if not
164
164
* initialized yet via {@link #afterPropertiesSet()}.
165
165
*/
166
166
public HandlerMethodReturnValueHandlerComposite getReturnValueHandlers () {
167
167
return this .returnValueHandlers ;
168
168
}
169
-
169
+
170
170
/**
171
171
* Set the message body converters to use.
172
172
* <p>These converters are used to convert from and to HTTP requests and responses.
173
173
*/
174
174
public void setMessageConverters (List <HttpMessageConverter <?>> messageConverters ) {
175
175
this .messageConverters = messageConverters ;
176
176
}
177
-
177
+
178
178
/**
179
179
* Return the configured message body converters.
180
180
*/
@@ -199,11 +199,11 @@ public void afterPropertiesSet() {
199
199
*/
200
200
protected List <HandlerMethodArgumentResolver > getDefaultArgumentResolvers () {
201
201
List <HandlerMethodArgumentResolver > resolvers = new ArrayList <HandlerMethodArgumentResolver >();
202
-
202
+
203
203
// Type-based argument resolution
204
204
resolvers .add (new ServletRequestMethodArgumentResolver ());
205
205
resolvers .add (new ServletResponseMethodArgumentResolver ());
206
-
206
+
207
207
// Custom arguments
208
208
if (getCustomArgumentResolvers () != null ) {
209
209
resolvers .addAll (getCustomArgumentResolvers ());
@@ -213,12 +213,12 @@ protected List<HandlerMethodArgumentResolver> getDefaultArgumentResolvers() {
213
213
}
214
214
215
215
/**
216
- * Return the list of return value handlers to use including built-in and
216
+ * Return the list of return value handlers to use including built-in and
217
217
* custom handlers provided via {@link #setReturnValueHandlers}.
218
218
*/
219
219
protected List <HandlerMethodReturnValueHandler > getDefaultReturnValueHandlers () {
220
220
List <HandlerMethodReturnValueHandler > handlers = new ArrayList <HandlerMethodReturnValueHandler >();
221
-
221
+
222
222
// Single-purpose return value types
223
223
handlers .add (new ModelAndViewMethodReturnValueHandler ());
224
224
handlers .add (new ModelMethodProcessor ());
@@ -240,23 +240,18 @@ protected List<HandlerMethodReturnValueHandler> getDefaultReturnValueHandlers()
240
240
241
241
// Catch-all
242
242
handlers .add (new ModelAttributeMethodProcessor (true ));
243
-
243
+
244
244
return handlers ;
245
245
}
246
246
247
247
/**
248
- * Find an @{@link ExceptionHandler} method and invoke it to handle the
248
+ * Find an @{@link ExceptionHandler} method and invoke it to handle the
249
249
* raised exception.
250
250
*/
251
251
@ Override
252
- protected ModelAndView doResolveHandlerMethodException (HttpServletRequest request ,
253
- HttpServletResponse response ,
254
- HandlerMethod handlerMethod ,
255
- Exception exception ) {
256
- if (handlerMethod == null ) {
257
- return null ;
258
- }
259
-
252
+ protected ModelAndView doResolveHandlerMethodException (HttpServletRequest request ,
253
+ HttpServletResponse response , HandlerMethod handlerMethod , Exception exception ) {
254
+
260
255
ServletInvocableHandlerMethod exceptionHandlerMethod = getExceptionHandlerMethod (handlerMethod , exception );
261
256
if (exceptionHandlerMethod == null ) {
262
257
return null ;
@@ -278,7 +273,7 @@ protected ModelAndView doResolveHandlerMethodException(HttpServletRequest reques
278
273
logger .error ("Failed to invoke @ExceptionHandler method: " + exceptionHandlerMethod , invocationEx );
279
274
return null ;
280
275
}
281
-
276
+
282
277
if (mavContainer .isRequestHandled ()) {
283
278
return new ModelAndView ();
284
279
}
@@ -288,19 +283,22 @@ protected ModelAndView doResolveHandlerMethodException(HttpServletRequest reques
288
283
if (!mavContainer .isViewReference ()) {
289
284
mav .setView ((View ) mavContainer .getView ());
290
285
}
291
- return mav ;
286
+ return mav ;
292
287
}
293
288
}
294
289
295
290
/**
296
291
* Find the @{@link ExceptionHandler} method for the given exception.
297
- * The default implementation searches @{@link ExceptionHandler} methods
292
+ * The default implementation searches @{@link ExceptionHandler} methods
298
293
* in the class hierarchy of the method that raised the exception.
299
- * @param handlerMethod the method where the exception was raised
294
+ * @param handlerMethod the method where the exception was raised, possibly {@code null}
300
295
* @param exception the raised exception
301
296
* @return a method to handle the exception, or {@code null}
302
297
*/
303
298
protected ServletInvocableHandlerMethod getExceptionHandlerMethod (HandlerMethod handlerMethod , Exception exception ) {
299
+ if (handlerMethod == null ) {
300
+ return null ;
301
+ }
304
302
Class <?> handlerType = handlerMethod .getBeanType ();
305
303
Method method = getExceptionHandlerMethodResolver (handlerType ).resolveMethod (exception );
306
304
return (method != null ? new ServletInvocableHandlerMethod (handlerMethod .getBean (), method ) : null );
0 commit comments