SPR-7259 - ResponseStatus.reason() ignored for @ExceptionHandler methods
This commit is contained in:
parent
f72c431e8a
commit
4764fa53f0
|
|
@ -45,6 +45,7 @@ import org.springframework.core.annotation.AnnotationUtils;
|
||||||
import org.springframework.http.HttpInputMessage;
|
import org.springframework.http.HttpInputMessage;
|
||||||
import org.springframework.http.HttpOutputMessage;
|
import org.springframework.http.HttpOutputMessage;
|
||||||
import org.springframework.http.MediaType;
|
import org.springframework.http.MediaType;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.http.converter.ByteArrayHttpMessageConverter;
|
import org.springframework.http.converter.ByteArrayHttpMessageConverter;
|
||||||
import org.springframework.http.converter.FormHttpMessageConverter;
|
import org.springframework.http.converter.FormHttpMessageConverter;
|
||||||
import org.springframework.http.converter.HttpMessageConverter;
|
import org.springframework.http.converter.HttpMessageConverter;
|
||||||
|
|
@ -56,6 +57,7 @@ import org.springframework.ui.Model;
|
||||||
import org.springframework.util.ClassUtils;
|
import org.springframework.util.ClassUtils;
|
||||||
import org.springframework.util.ObjectUtils;
|
import org.springframework.util.ObjectUtils;
|
||||||
import org.springframework.util.ReflectionUtils;
|
import org.springframework.util.ReflectionUtils;
|
||||||
|
import org.springframework.util.StringUtils;
|
||||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||||
import org.springframework.web.bind.annotation.ResponseBody;
|
import org.springframework.web.bind.annotation.ResponseBody;
|
||||||
import org.springframework.web.bind.annotation.ResponseStatus;
|
import org.springframework.web.bind.annotation.ResponseStatus;
|
||||||
|
|
@ -347,10 +349,16 @@ public class AnnotationMethodHandlerExceptionResolver extends AbstractHandlerExc
|
||||||
private ModelAndView getModelAndView(Method handlerMethod, Object returnValue, ServletWebRequest webRequest)
|
private ModelAndView getModelAndView(Method handlerMethod, Object returnValue, ServletWebRequest webRequest)
|
||||||
throws Exception {
|
throws Exception {
|
||||||
|
|
||||||
ResponseStatus responseStatus = AnnotationUtils.findAnnotation(handlerMethod, ResponseStatus.class);
|
ResponseStatus responseStatusAnn = AnnotationUtils.findAnnotation(handlerMethod, ResponseStatus.class);
|
||||||
if (responseStatus != null) {
|
if (responseStatusAnn != null) {
|
||||||
HttpServletResponse response = webRequest.getResponse();
|
HttpStatus responseStatus = responseStatusAnn.value();
|
||||||
response.setStatus(responseStatus.value().value());
|
String reason = responseStatusAnn.reason();
|
||||||
|
if (!StringUtils.hasText(reason)) {
|
||||||
|
webRequest.getResponse().setStatus(responseStatus.value());
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
webRequest.getResponse().sendError(responseStatus.value(), reason);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (returnValue != null && AnnotationUtils.findAnnotation(handlerMethod, ResponseBody.class) != null) {
|
if (returnValue != null && AnnotationUtils.findAnnotation(handlerMethod, ResponseBody.class) != null) {
|
||||||
|
|
|
||||||
|
|
@ -78,6 +78,7 @@ public class AnnotationMethodHandlerExceptionResolverTests {
|
||||||
assertNotNull("No ModelAndView returned", mav);
|
assertNotNull("No ModelAndView returned", mav);
|
||||||
assertEquals("Invalid view name returned", "Y:SocketException", mav.getViewName());
|
assertEquals("Invalid view name returned", "Y:SocketException", mav.getViewName());
|
||||||
assertEquals("Invalid status code returned", 406, response.getStatus());
|
assertEquals("Invalid status code returned", 406, response.getStatus());
|
||||||
|
assertEquals("Invalid status reason returned", "This is simply unacceptable!", response.getErrorMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
@ -149,7 +150,7 @@ public class AnnotationMethodHandlerExceptionResolverTests {
|
||||||
}
|
}
|
||||||
|
|
||||||
@ExceptionHandler(SocketException.class)
|
@ExceptionHandler(SocketException.class)
|
||||||
@ResponseStatus(HttpStatus.NOT_ACCEPTABLE)
|
@ResponseStatus(value = HttpStatus.NOT_ACCEPTABLE, reason = "This is simply unacceptable!")
|
||||||
public String handleSocketException(Exception ex, HttpServletResponse response) {
|
public String handleSocketException(Exception ex, HttpServletResponse response) {
|
||||||
return "Y:" + ClassUtils.getShortName(ex.getClass());
|
return "Y:" + ClassUtils.getShortName(ex.getClass());
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue