diff --git a/spring-web/src/main/java/org/springframework/web/bind/annotation/ResponseStatus.java b/spring-web/src/main/java/org/springframework/web/bind/annotation/ResponseStatus.java index 129605eec4..a6febcac12 100644 --- a/spring-web/src/main/java/org/springframework/web/bind/annotation/ResponseStatus.java +++ b/spring-web/src/main/java/org/springframework/web/bind/annotation/ResponseStatus.java @@ -80,7 +80,8 @@ public @interface ResponseStatus { /** * The reason to be used for the response. *
Defaults to an empty string which will be ignored. Set the reason to a - * non-empty value to have it used for the response. + * non-empty value to have it used to send a Servlet container error page. + * In this case, the return value of the handler method will be ignored. * @see jakarta.servlet.http.HttpServletResponse#sendError(int, String) */ String reason() default ""; diff --git a/spring-web/src/main/java/org/springframework/web/method/HandlerMethod.java b/spring-web/src/main/java/org/springframework/web/method/HandlerMethod.java index 8ab32964e1..5474a1ce10 100644 --- a/spring-web/src/main/java/org/springframework/web/method/HandlerMethod.java +++ b/spring-web/src/main/java/org/springframework/web/method/HandlerMethod.java @@ -216,6 +216,9 @@ public class HandlerMethod extends AnnotatedMethod { this.responseStatus = annotation.code(); this.responseStatusReason = resolvedReason; + if (StringUtils.hasText(this.responseStatusReason) && getMethod().getReturnType() != void.class) { + logger.warn("Return value of [" + getMethod() + "] will be ignored since @ResponseStatus 'reason' attribute is set."); + } } }