diff --git a/spring-messaging/src/main/java/org/springframework/messaging/handler/HandlerMethod.java b/spring-messaging/src/main/java/org/springframework/messaging/handler/HandlerMethod.java index b83576ecb76..0cd4d91e16c 100644 --- a/spring-messaging/src/main/java/org/springframework/messaging/handler/HandlerMethod.java +++ b/spring-messaging/src/main/java/org/springframework/messaging/handler/HandlerMethod.java @@ -64,7 +64,7 @@ public class HandlerMethod { private final MethodParameter[] parameters; - private final HandlerMethod resolvedFromHandlerMethod; + private HandlerMethod resolvedFromHandlerMethod; /** @@ -79,7 +79,6 @@ public class HandlerMethod { this.method = method; this.bridgedMethod = BridgeMethodResolver.findBridgedMethod(method); this.parameters = initMethodParameters(); - this.resolvedFromHandlerMethod = null; } /** @@ -95,7 +94,6 @@ public class HandlerMethod { this.method = bean.getClass().getMethod(methodName, parameterTypes); this.bridgedMethod = BridgeMethodResolver.findBridgedMethod(this.method); this.parameters = initMethodParameters(); - this.resolvedFromHandlerMethod = null; } /** @@ -113,7 +111,6 @@ public class HandlerMethod { this.method = method; this.bridgedMethod = BridgeMethodResolver.findBridgedMethod(method); this.parameters = initMethodParameters(); - this.resolvedFromHandlerMethod = null; } /** @@ -158,14 +155,14 @@ public class HandlerMethod { } /** - * Returns the bean for this handler method. + * Return the bean for this handler method. */ public Object getBean() { return this.bean; } /** - * Returns the method for this handler method. + * Return the method for this handler method. */ public Method getMethod() { return this.method; @@ -189,21 +186,12 @@ public class HandlerMethod { } /** - * Returns the method parameters for this handler method. + * Return the method parameters for this handler method. */ public MethodParameter[] getMethodParameters() { return this.parameters; } - /** - * Return the HandlerMethod from which this HandlerMethod instance was - * resolved via {@link #createWithResolvedBean()}. - * @since 4.3 - */ - public HandlerMethod getResolvedFromHandlerMethod() { - return this.resolvedFromHandlerMethod; - } - /** * Return the HandlerMethod return type. */ @@ -219,14 +207,14 @@ public class HandlerMethod { } /** - * Returns {@code true} if the method return type is void, {@code false} otherwise. + * Return {@code true} if the method return type is void, {@code false} otherwise. */ public boolean isVoid() { return Void.TYPE.equals(getReturnType().getParameterType()); } /** - * Returns a single annotation on the underlying method traversing its super methods + * Return a single annotation on the underlying method traversing its super methods * if no annotation can be found on the given method itself. *
Also supports merged composed annotations with attribute * overrides as of Spring Framework 4.3. @@ -248,6 +236,15 @@ public class HandlerMethod { return AnnotatedElementUtils.hasAnnotation(this.method, annotationType); } + /** + * Return the HandlerMethod from which this HandlerMethod instance was + * resolved via {@link #createWithResolvedBean()}. + * @since 4.3 + */ + public HandlerMethod getResolvedFromHandlerMethod() { + return this.resolvedFromHandlerMethod; + } + /** * If the provided instance contains a bean name rather than an object instance, * the bean name is resolved before a {@link HandlerMethod} is created and returned. 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 d6cffd1d11c..d1541b5d8f4 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 @@ -28,8 +28,10 @@ import org.springframework.core.GenericTypeResolver; import org.springframework.core.MethodParameter; import org.springframework.core.annotation.AnnotatedElementUtils; import org.springframework.core.annotation.SynthesizingMethodParameter; +import org.springframework.http.HttpStatus; import org.springframework.util.Assert; import org.springframework.util.ClassUtils; +import org.springframework.web.bind.annotation.ResponseStatus; /** * Encapsulates information about a handler method consisting of a @@ -65,7 +67,11 @@ public class HandlerMethod { private final MethodParameter[] parameters; - private final HandlerMethod resolvedFromHandlerMethod; + private HttpStatus responseStatus; + + private String responseStatusReason; + + private HandlerMethod resolvedFromHandlerMethod; /** @@ -80,7 +86,7 @@ public class HandlerMethod { this.method = method; this.bridgedMethod = BridgeMethodResolver.findBridgedMethod(method); this.parameters = initMethodParameters(); - this.resolvedFromHandlerMethod = null; + evaluateResponseStatus(); } /** @@ -96,7 +102,7 @@ public class HandlerMethod { this.method = bean.getClass().getMethod(methodName, parameterTypes); this.bridgedMethod = BridgeMethodResolver.findBridgedMethod(this.method); this.parameters = initMethodParameters(); - this.resolvedFromHandlerMethod = null; + evaluateResponseStatus(); } /** @@ -114,7 +120,7 @@ public class HandlerMethod { this.method = method; this.bridgedMethod = BridgeMethodResolver.findBridgedMethod(method); this.parameters = initMethodParameters(); - this.resolvedFromHandlerMethod = null; + evaluateResponseStatus(); } /** @@ -128,6 +134,8 @@ public class HandlerMethod { this.method = handlerMethod.method; this.bridgedMethod = handlerMethod.bridgedMethod; this.parameters = handlerMethod.parameters; + this.responseStatus = handlerMethod.responseStatus; + this.responseStatusReason = handlerMethod.responseStatusReason; this.resolvedFromHandlerMethod = handlerMethod.resolvedFromHandlerMethod; } @@ -143,6 +151,8 @@ public class HandlerMethod { this.method = handlerMethod.method; this.bridgedMethod = handlerMethod.bridgedMethod; this.parameters = handlerMethod.parameters; + this.responseStatus = handlerMethod.responseStatus; + this.responseStatusReason = handlerMethod.responseStatusReason; this.resolvedFromHandlerMethod = handlerMethod; } @@ -158,15 +168,27 @@ public class HandlerMethod { return result; } + private void evaluateResponseStatus() { + ResponseStatus annotation = getMethodAnnotation(ResponseStatus.class); + if (annotation == null) { + annotation = AnnotatedElementUtils.findMergedAnnotation(getBeanType(), ResponseStatus.class); + } + if (annotation != null) { + this.responseStatus = annotation.code(); + this.responseStatusReason = annotation.reason(); + } + } + + /** - * Returns the bean for this handler method. + * Return the bean for this handler method. */ public Object getBean() { return this.bean; } /** - * Returns the method for this handler method. + * Return the method for this handler method. */ public Method getMethod() { return this.method; @@ -190,18 +212,28 @@ public class HandlerMethod { } /** - * Returns the method parameters for this handler method. + * Return the method parameters for this handler method. */ public MethodParameter[] getMethodParameters() { return this.parameters; } /** - * Return the HandlerMethod from which this HandlerMethod instance was - * resolved via {@link #createWithResolvedBean()}. + * Return the specified response status, if any. + * @since 4.3.8 + * @see ResponseStatus#code() */ - public HandlerMethod getResolvedFromHandlerMethod() { - return this.resolvedFromHandlerMethod; + protected HttpStatus getResponseStatus() { + return this.responseStatus; + } + + /** + * Return the associated response status reason, if any. + * @since 4.3.8 + * @see ResponseStatus#reason() + */ + protected String getResponseStatusReason() { + return this.responseStatusReason; } /** @@ -219,14 +251,14 @@ public class HandlerMethod { } /** - * Returns {@code true} if the method return type is void, {@code false} otherwise. + * Return {@code true} if the method return type is void, {@code false} otherwise. */ public boolean isVoid() { return Void.TYPE.equals(getReturnType().getParameterType()); } /** - * Returns a single annotation on the underlying method traversing its super methods + * Return a single annotation on the underlying method traversing its super methods * if no annotation can be found on the given method itself. *
Also supports merged composed annotations with attribute
* overrides as of Spring Framework 4.2.2.
@@ -248,6 +280,14 @@ public class HandlerMethod {
return AnnotatedElementUtils.hasAnnotation(this.method, annotationType);
}
+ /**
+ * Return the HandlerMethod from which this HandlerMethod instance was
+ * resolved via {@link #createWithResolvedBean()}.
+ */
+ public HandlerMethod getResolvedFromHandlerMethod() {
+ return this.resolvedFromHandlerMethod;
+ }
+
/**
* If the provided instance contains a bean name rather than an object instance,
* the bean name is resolved before a {@link HandlerMethod} is created and returned.
diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/InvocableHandlerMethod.java b/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/InvocableHandlerMethod.java
index b7a6811f9d0..ff9f10c6c7b 100644
--- a/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/InvocableHandlerMethod.java
+++ b/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/InvocableHandlerMethod.java
@@ -31,12 +31,10 @@ import reactor.core.publisher.Mono;
import org.springframework.core.DefaultParameterNameDiscoverer;
import org.springframework.core.MethodParameter;
import org.springframework.core.ParameterNameDiscoverer;
-import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.http.HttpStatus;
import org.springframework.util.ClassUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.ReflectionUtils;
-import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.reactive.BindingContext;
import org.springframework.web.reactive.HandlerResult;
@@ -57,8 +55,6 @@ public class InvocableHandlerMethod extends HandlerMethod {
private static final Object NO_ARG_VALUE = new Object();
- private HttpStatus responseStatus;
-
private List