HandlerMethod evaluates ResponseStatus annotation for early caching

Issue: SPR-15227
This commit is contained in:
Juergen Hoeller
2017-04-08 15:56:05 +02:00
parent ff03b4a4ad
commit 5986f881d0
4 changed files with 82 additions and 81 deletions

View File

@@ -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.
* <p>Also supports <em>merged</em> 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.