ResponseStatusException reason is optional (with lazily constructed message)
Issue: SPR-15524
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2016 the original author or authors.
|
||||
* Copyright 2002-2017 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -49,7 +49,8 @@ import org.springframework.web.servlet.handler.AbstractHandlerExceptionResolver;
|
||||
* @author Rossen Stoyanchev
|
||||
* @author Sam Brannen
|
||||
* @since 3.0
|
||||
* @see AnnotatedElementUtils#findMergedAnnotation
|
||||
* @see ResponseStatus
|
||||
* @see ResponseStatusException
|
||||
*/
|
||||
public class ResponseStatusExceptionResolver extends AbstractHandlerExceptionResolver implements MessageSourceAware {
|
||||
|
||||
@@ -99,9 +100,8 @@ public class ResponseStatusExceptionResolver extends AbstractHandlerExceptionRes
|
||||
* @param ex the exception
|
||||
* @return an empty ModelAndView, i.e. exception resolved
|
||||
*/
|
||||
protected ModelAndView resolveResponseStatus(ResponseStatus responseStatus,
|
||||
HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
|
||||
throws Exception {
|
||||
protected ModelAndView resolveResponseStatus(ResponseStatus responseStatus, HttpServletRequest request,
|
||||
HttpServletResponse response, Object handler, Exception ex) throws Exception {
|
||||
|
||||
int statusCode = responseStatus.code().value();
|
||||
String reason = responseStatus.reason();
|
||||
@@ -125,8 +125,7 @@ public class ResponseStatusExceptionResolver extends AbstractHandlerExceptionRes
|
||||
|
||||
int statusCode = ex.getStatus().value();
|
||||
String reason = ex.getReason();
|
||||
applyStatusAndReason(statusCode, reason, response);
|
||||
return new ModelAndView();
|
||||
return applyStatusAndReason(statusCode, reason, response);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -135,19 +134,22 @@ public class ResponseStatusExceptionResolver extends AbstractHandlerExceptionRes
|
||||
* {@link HttpServletResponse#sendError(int)} or
|
||||
* {@link HttpServletResponse#sendError(int, String)} if there is a reason
|
||||
* and then returns an empty ModelAndView.
|
||||
* @param statusCode the HTTP status code
|
||||
* @param reason the associated reason (may be {@code null} or empty)
|
||||
* @param response current HTTP response
|
||||
* @since 5.0
|
||||
*/
|
||||
protected ModelAndView applyStatusAndReason(int statusCode, String reason, HttpServletResponse response)
|
||||
throws IOException {
|
||||
|
||||
if (this.messageSource != null) {
|
||||
reason = this.messageSource.getMessage(reason, null, reason, LocaleContextHolder.getLocale());
|
||||
}
|
||||
if (!StringUtils.hasLength(reason)) {
|
||||
response.sendError(statusCode);
|
||||
}
|
||||
else {
|
||||
response.sendError(statusCode, reason);
|
||||
String resolvedReason = (this.messageSource != null ?
|
||||
this.messageSource.getMessage(reason, null, reason, LocaleContextHolder.getLocale()) :
|
||||
reason);
|
||||
response.sendError(statusCode, resolvedReason);
|
||||
}
|
||||
return new ModelAndView();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user