Skip to content

Commit 029df7f

Browse files
committed
Fix NPE due to null returnType when using spring-webmvc >= 4.2.0
The returnType parameter in HandlerMethodReturnValueHandler.handleReturnValue() is no longer ignored in Spring WebMVC 4.2.0. Closes #9. Thanks to @jervi for the warning and suggestion of fix.
1 parent 59885ef commit 029df7f

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
lines changed

src/main/java/cz/jirutka/spring/exhandler/RestHandlerExceptionResolver.java

+23-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014 Jakub Jirutka <[email protected]>.
2+
* Copyright 2014-2015 Jakub Jirutka <[email protected]>.
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.
@@ -19,10 +19,12 @@
1919
import org.slf4j.Logger;
2020
import org.slf4j.LoggerFactory;
2121
import org.springframework.beans.factory.InitializingBean;
22+
import org.springframework.core.MethodParameter;
2223
import org.springframework.http.MediaType;
2324
import org.springframework.http.ResponseEntity;
2425
import org.springframework.http.converter.HttpMessageConverter;
2526
import org.springframework.util.Assert;
27+
import org.springframework.util.ClassUtils;
2628
import org.springframework.web.HttpMediaTypeNotAcceptableException;
2729
import org.springframework.web.accept.ContentNegotiationManager;
2830
import org.springframework.web.accept.FixedContentNegotiationStrategy;
@@ -37,6 +39,7 @@
3739

3840
import javax.servlet.http.HttpServletRequest;
3941
import javax.servlet.http.HttpServletResponse;
42+
import java.lang.reflect.Method;
4043
import java.util.LinkedHashMap;
4144
import java.util.List;
4245
import java.util.Map;
@@ -58,6 +61,8 @@ public class RestHandlerExceptionResolver extends AbstractHandlerExceptionResolv
5861

5962
private static final Logger LOG = LoggerFactory.getLogger(RestHandlerExceptionResolver.class);
6063

64+
private final MethodParameter returnTypeMethodParam;
65+
6166
private List<HttpMessageConverter<?>> messageConverters = getDefaultHttpMessageConverters();
6267

6368
private Map<Class<? extends Exception>, RestExceptionHandler> handlers = new LinkedHashMap<>();
@@ -81,6 +86,18 @@ public static RestHandlerExceptionResolverBuilder builder() {
8186
}
8287

8388

89+
public RestHandlerExceptionResolver() {
90+
91+
Method method = ClassUtils.getMethod(
92+
RestExceptionHandler.class, "handleException", Exception.class, HttpServletRequest.class);
93+
94+
returnTypeMethodParam = new MethodParameter(method, -1);
95+
// This method caches the resolved value, so it's convenient to initialize it
96+
// only once here.
97+
returnTypeMethodParam.getGenericParameterType();
98+
}
99+
100+
84101
@Override
85102
public void afterPropertiesSet() {
86103
if (contentNegotiationManager == null) {
@@ -136,13 +153,16 @@ protected ResponseEntity<?> handleException(Exception exception, HttpServletRequ
136153

137154
protected void processResponse(ResponseEntity<?> entity, NativeWebRequest webRequest) throws Exception {
138155

156+
// XXX: Create MethodParameter from the actually used subclass of RestExceptionHandler?
157+
MethodParameter methodParameter = new MethodParameter(returnTypeMethodParam);
139158
ModelAndViewContainer mavContainer = new ModelAndViewContainer();
159+
140160
try {
141-
responseProcessor.handleReturnValue(entity, null, mavContainer, webRequest);
161+
responseProcessor.handleReturnValue(entity, methodParameter, mavContainer, webRequest);
142162

143163
} catch (HttpMediaTypeNotAcceptableException ex) {
144164
LOG.debug("Requested media type is not supported, falling back to default one");
145-
fallbackResponseProcessor.handleReturnValue(entity, null, mavContainer, webRequest);
165+
fallbackResponseProcessor.handleReturnValue(entity, methodParameter, mavContainer, webRequest);
146166
}
147167
}
148168

0 commit comments

Comments
 (0)