Skip to content

Commit 4764fa5

Browse files
committed
SPR-7259 - ResponseStatus.reason() ignored for @ExceptionHandler methods
1 parent f72c431 commit 4764fa5

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/mvc/annotation/AnnotationMethodHandlerExceptionResolver.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import org.springframework.http.HttpInputMessage;
4646
import org.springframework.http.HttpOutputMessage;
4747
import org.springframework.http.MediaType;
48+
import org.springframework.http.HttpStatus;
4849
import org.springframework.http.converter.ByteArrayHttpMessageConverter;
4950
import org.springframework.http.converter.FormHttpMessageConverter;
5051
import org.springframework.http.converter.HttpMessageConverter;
@@ -56,6 +57,7 @@
5657
import org.springframework.util.ClassUtils;
5758
import org.springframework.util.ObjectUtils;
5859
import org.springframework.util.ReflectionUtils;
60+
import org.springframework.util.StringUtils;
5961
import org.springframework.web.bind.annotation.ExceptionHandler;
6062
import org.springframework.web.bind.annotation.ResponseBody;
6163
import org.springframework.web.bind.annotation.ResponseStatus;
@@ -347,10 +349,16 @@ private Object doInvokeMethod(Method method, Object target, Object[] args) throw
347349
private ModelAndView getModelAndView(Method handlerMethod, Object returnValue, ServletWebRequest webRequest)
348350
throws Exception {
349351

350-
ResponseStatus responseStatus = AnnotationUtils.findAnnotation(handlerMethod, ResponseStatus.class);
351-
if (responseStatus != null) {
352-
HttpServletResponse response = webRequest.getResponse();
353-
response.setStatus(responseStatus.value().value());
352+
ResponseStatus responseStatusAnn = AnnotationUtils.findAnnotation(handlerMethod, ResponseStatus.class);
353+
if (responseStatusAnn != null) {
354+
HttpStatus responseStatus = responseStatusAnn.value();
355+
String reason = responseStatusAnn.reason();
356+
if (!StringUtils.hasText(reason)) {
357+
webRequest.getResponse().setStatus(responseStatus.value());
358+
}
359+
else {
360+
webRequest.getResponse().sendError(responseStatus.value(), reason);
361+
}
354362
}
355363

356364
if (returnValue != null && AnnotationUtils.findAnnotation(handlerMethod, ResponseBody.class) != null) {

org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/mvc/annotation/AnnotationMethodHandlerExceptionResolverTests.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ public void simpleWithSocketException() {
7878
assertNotNull("No ModelAndView returned", mav);
7979
assertEquals("Invalid view name returned", "Y:SocketException", mav.getViewName());
8080
assertEquals("Invalid status code returned", 406, response.getStatus());
81+
assertEquals("Invalid status reason returned", "This is simply unacceptable!", response.getErrorMessage());
8182
}
8283

8384
@Test
@@ -149,7 +150,7 @@ public String handleIOException(IOException ex, HttpServletRequest request) {
149150
}
150151

151152
@ExceptionHandler(SocketException.class)
152-
@ResponseStatus(HttpStatus.NOT_ACCEPTABLE)
153+
@ResponseStatus(value = HttpStatus.NOT_ACCEPTABLE, reason = "This is simply unacceptable!")
153154
public String handleSocketException(Exception ex, HttpServletResponse response) {
154155
return "Y:" + ClassUtils.getShortName(ex.getClass());
155156
}

0 commit comments

Comments
 (0)