Top Description Inners Fields Constructors Methods
org.springframework.web.method

public Class HandlerMethod

extends AnnotatedMethod
Class Inheritance
Known Direct Subclasses
org.springframework.web.method.support.InvocableHandlerMethod
Imports
java.lang.annotation.Annotation, java.lang.reflect.AnnotatedParameterizedType, .AnnotatedType, .Method, java.util.ArrayList, .Collections, .List, .Map, .StringJoiner, java.util.function.Predicate, java.util.stream.Collectors, .IntStream, org.apache.commons.logging.Log, .LogFactory, org.springframework.beans.factory.BeanFactory, org.springframework.context.MessageSource, org.springframework.context.i18n.LocaleContextHolder, org.springframework.core.MethodParameter, org.springframework.core.annotation.AnnotatedElementUtils, .AnnotatedMethod, .AnnotationUtils, .MergedAnnotation, .MergedAnnotationPredicates, .MergedAnnotations, org.springframework.http.HttpStatusCode, org.springframework.lang.Nullable, org.springframework.util.Assert, .ClassUtils, .StringUtils, org.springframework.validation.annotation.Validated, org.springframework.web.bind.annotation.ResponseStatus

Encapsulates information about a handler method consisting of a method and a bean. Provides convenient access to method parameters, the method return value, method annotations, etc.

The class may be created with a bean instance or with a bean name (e.g. lazy-init bean, prototype bean). Use createWithResolvedBean() to obtain a HandlerMethod instance with a bean instance resolved through the associated BeanFactory.

Authors
Arjen Poutsma, Rossen Stoyanchev, Juergen Hoeller, Sam Brannen
Since
3.1

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
HandlerMethod.MethodValidationInitializer

Checks for the presence of @Constraint and @Valid annotations on the method and method parameters.

Field Summary

Modifier and TypeField and Description
private final Object
private final BeanFactory
private final Class<?>
private final String
protected static final Log
logger

Logger that is available to subclasses.

private final MessageSource
private HandlerMethod
private HttpStatusCode
private String
private final boolean
private final boolean

Constructor Summary

AccessConstructor and Description
public
HandlerMethod(Object bean, Method method)

Create an instance from a bean instance and a method.

protected
HandlerMethod(Object bean, Method method, MessageSource messageSource)

Variant of HandlerMethod(Object, Method) that also accepts a MessageSource for use from subclasses.

public
HandlerMethod(Object bean, String methodName, Class<?>... parameterTypes)

Create an instance from a bean instance, method name, and parameter types.

public
HandlerMethod(String beanName, BeanFactory beanFactory, Method method)

Create an instance from a bean name, a method, and a BeanFactory.

public
HandlerMethod(String beanName, BeanFactory beanFactory, MessageSource messageSource, Method method)

Variant of HandlerMethod(String, BeanFactory, Method) that also accepts a MessageSource.

protected
HandlerMethod(HandlerMethod handlerMethod)

Copy constructor for use in subclasses.

private
HandlerMethod(HandlerMethod handlerMethod, Object handler, boolean initValidateFlags)

Re-create HandlerMethod with additional input.

Method Summary

Modifier and TypeMethod and Description
protected void
assertTargetBean(Method method, Object targetBean, Object[] args)

Assert that the target bean class is an instance of the class where the given method is declared.

public HandlerMethod
createWithResolvedBean()

If the provided instance contains a bean name rather than an object instance, the bean name is resolved before a HandlerMethod is created and returned.

public HandlerMethod
createWithValidateFlags()

Re-create the HandlerMethod and initialize shouldValidateArguments() and shouldValidateReturnValue().

public boolean
equals(Object
the reference object with which to compare.
other
)

Overrides org.springframework.core.annotation.AnnotatedMethod.equals.

Indicates whether some other object is "equal to" this one.

private void
protected String
public Object
getBean()

Return the bean for this handler method.

public Class<?>
getBeanType()

This method returns the type of the handler for this handler method.

protected Class<?>
getContainingClass()

Overrides org.springframework.core.annotation.AnnotatedMethod.getContainingClass.

Expose the containing class for method parameters.

public HandlerMethod
getResolvedFromHandlerMethod()

Return the HandlerMethod from which this HandlerMethod instance was resolved via createWithResolvedBean().

protected HttpStatusCode
getResponseStatus()

Return the specified response status, if any.

protected String
getResponseStatusReason()

Return the associated response status reason, if any.

public String
getShortLogMessage()

Return a short representation of this handler method for log message purposes.

public int
hashCode()

Overrides org.springframework.core.annotation.AnnotatedMethod.hashCode.

Returns a hash code value for this object.

private static String
initDescription(Class<?> beanType, Method method)

public boolean
shouldValidateArguments()

Whether the method arguments are a candidate for method validation, which is the case when there are parameter jakarta.validation.Constraint annotations.

public boolean
shouldValidateReturnValue()

Whether the method return value is a candidate for method validation, which is the case when there are method jakarta.validation.Constraint or jakarta.validation.Valid annotations.

public String
toString()

Overrides org.springframework.core.annotation.AnnotatedMethod.toString.

Returns a string representation of the object.

Inherited from org.springframework.core.annotation.AnnotatedMethod:
findProvidedArgumentformatArgumentErrorgetBridgedMethodgetMethodgetMethodAnnotationgetMethodParametersgetReturnTypegetReturnValueTypehasMethodAnnotationisVoid

Field Detail

beanback to summary
private final Object bean
beanFactoryback to summary
private final BeanFactory beanFactory
Annotations
@Nullable
beanTypeback to summary
private final Class<?> beanType
descriptionback to summary
private final String description
loggerback to summary
protected static final Log logger

Logger that is available to subclasses.

messageSourceback to summary
private final MessageSource messageSource
Annotations
@Nullable
resolvedFromHandlerMethodback to summary
private HandlerMethod resolvedFromHandlerMethod
Annotations
@Nullable
responseStatusback to summary
private HttpStatusCode responseStatus
Annotations
@Nullable
responseStatusReasonback to summary
private String responseStatusReason
Annotations
@Nullable
validateArgumentsback to summary
private final boolean validateArguments
validateReturnValueback to summary
private final boolean validateReturnValue

Constructor Detail

HandlerMethodback to summary
public HandlerMethod(Object bean, Method method)

Create an instance from a bean instance and a method.

HandlerMethodback to summary
protected HandlerMethod(Object bean, Method method, MessageSource messageSource)

Variant of HandlerMethod(Object, Method) that also accepts a MessageSource for use from subclasses.

Since
5.3.10
HandlerMethodback to summary
public HandlerMethod(Object bean, String methodName, Class<?>... parameterTypes) throws NoSuchMethodException

Create an instance from a bean instance, method name, and parameter types.

Exceptions
NoSuchMethodException:
when the method cannot be found
HandlerMethodback to summary
public HandlerMethod(String beanName, BeanFactory beanFactory, Method method)

Create an instance from a bean name, a method, and a BeanFactory. The method createWithResolvedBean() may be used later to re-create the HandlerMethod with an initialized bean.

HandlerMethodback to summary
public HandlerMethod(String beanName, BeanFactory beanFactory, MessageSource messageSource, Method method)

Variant of HandlerMethod(String, BeanFactory, Method) that also accepts a MessageSource.

HandlerMethodback to summary
protected HandlerMethod(HandlerMethod handlerMethod)

Copy constructor for use in subclasses.

HandlerMethodback to summary
private HandlerMethod(HandlerMethod handlerMethod, Object handler, boolean initValidateFlags)

Re-create HandlerMethod with additional input.

Method Detail

assertTargetBeanback to summary
protected void assertTargetBean(Method method, Object targetBean, Object[] args)

Assert that the target bean class is an instance of the class where the given method is declared. In some cases the actual controller instance at request- processing time may be a JDK dynamic proxy (lazy initialization, prototype beans, and others). @Controller's that require proxying should prefer class-based proxy mechanisms.

createWithResolvedBeanback to summary
public HandlerMethod createWithResolvedBean()

If the provided instance contains a bean name rather than an object instance, the bean name is resolved before a HandlerMethod is created and returned.

createWithValidateFlagsback to summary
public HandlerMethod createWithValidateFlags()

Re-create the HandlerMethod and initialize shouldValidateArguments() and shouldValidateReturnValue().

Since
6.1.3
equalsback to summary
public boolean equals(Object other)

Overrides org.springframework.core.annotation.AnnotatedMethod.equals.

Doc from java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.

The equals method implements an equivalence relation on non-null object references:

  • It is reflexive: for any non-null reference value x, x.equals(x) should return true.
  • It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
  • It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
  • It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.
  • For any non-null reference value x, x.equals(null) should return false.

An equivalence relation partitions the elements it operates on into equivalence classes; all the members of an equivalence class are equal to each other. Members of an equivalence class are substitutable for each other, at least for some purposes.

Parameters
other:Object

the reference object with which to compare.

Returns:boolean

true if this object is the same as the obj argument; false otherwise.

Annotations
@Override
evaluateResponseStatusback to summary
private void evaluateResponseStatus()
formatInvokeErrorback to summary
protected String formatInvokeError(String text, Object[] args)
getBeanback to summary
public Object getBean()

Return the bean for this handler method.

getBeanTypeback to summary
public Class<?> getBeanType()

This method returns the type of the handler for this handler method.

Note that if the bean type is a CGLIB-generated class, the original user-defined class is returned.

getContainingClassback to summary
protected Class<?> getContainingClass()

Overrides org.springframework.core.annotation.AnnotatedMethod.getContainingClass.

Doc from org.springframework.core.annotation.AnnotatedMethod.getContainingClass.

Expose the containing class for method parameters.

Annotations
@Override
getResolvedFromHandlerMethodback to summary
public HandlerMethod getResolvedFromHandlerMethod()

Return the HandlerMethod from which this HandlerMethod instance was resolved via createWithResolvedBean().

Annotations
@Nullable
getResponseStatusback to summary
protected HttpStatusCode getResponseStatus()

Return the specified response status, if any.

Annotations
@Nullable
Since
4.3.8
See Also
ResponseStatus#code()
getResponseStatusReasonback to summary
protected String getResponseStatusReason()

Return the associated response status reason, if any.

Annotations
@Nullable
Since
4.3.8
See Also
ResponseStatus#reason()
getShortLogMessageback to summary
public String getShortLogMessage()

Return a short representation of this handler method for log message purposes.

Since
4.3
hashCodeback to summary
public int hashCode()

Overrides org.springframework.core.annotation.AnnotatedMethod.hashCode.

Doc from java.lang.Object.hashCode.

Returns a hash code value for this object. This method is supported for the benefit of hash tables such as those provided by java.util.HashMap.

The general contract of hashCode is:

  • Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
  • If two objects are equal according to the equals method, then calling the hashCode method on each of the two objects must produce the same integer result.
  • It is not required that if two objects are unequal according to the equals method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.
Returns:int

a hash code value for this object

Annotations
@Override
initDescriptionback to summary
private static String initDescription(Class<?> beanType, Method method)
shouldValidateArgumentsback to summary
public boolean shouldValidateArguments()

Whether the method arguments are a candidate for method validation, which is the case when there are parameter jakarta.validation.Constraint annotations.

The presence of jakarta.validation.Valid by itself does not trigger method validation since such parameters are already validated at the level of argument resolvers.

Note

if the class is annotated with Validated, this method returns false, deferring to method validation via AOP proxy.

Since
6.1
shouldValidateReturnValueback to summary
public boolean shouldValidateReturnValue()

Whether the method return value is a candidate for method validation, which is the case when there are method jakarta.validation.Constraint or jakarta.validation.Valid annotations.

Note

if the class is annotated with Validated, this method returns false, deferring to method validation via AOP proxy.

Since
6.1
toStringback to summary
public String toString()

Overrides org.springframework.core.annotation.AnnotatedMethod.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object. Satisfying this method's contract implies a non-null result must be returned.

Returns:String

a string representation of the object

Annotations
@Override
org.springframework.web.method back to summary

private Class HandlerMethod.MethodValidationInitializer

extends Object
Class Inheritance
  • java.lang.Object
  • org.springframework.web.method.HandlerMethod.MethodValidationInitializer

Checks for the presence of @Constraint and @Valid annotations on the method and method parameters.

Field Summary

Modifier and TypeField and Description
private static final boolean
private static final Predicate<MergedAnnotation<? extends Annotation>>
private static final Predicate<MergedAnnotation<? extends Annotation>>

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static boolean
checkArguments(Class<?> beanType, MethodParameter[] parameters)

public static boolean
checkReturnValue(Class<?> beanType, Method method)

private static Annotation[]
getContainerElementAnnotations(MethodParameter param)

There may be constraints on elements of a container (list, map).

private static boolean
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

BEAN_VALIDATION_PRESENTback to summary
private static final boolean BEAN_VALIDATION_PRESENT
CONSTRAINT_PREDICATEback to summary
private static final Predicate<MergedAnnotation<? extends Annotation>> CONSTRAINT_PREDICATE
VALID_PREDICATEback to summary
private static final Predicate<MergedAnnotation<? extends Annotation>> VALID_PREDICATE

Constructor Detail

MethodValidationInitializerback to summary
private MethodValidationInitializer()

Method Detail

checkArgumentsback to summary
public static boolean checkArguments(Class<?> beanType, MethodParameter[] parameters)
checkReturnValueback to summary
public static boolean checkReturnValue(Class<?> beanType, Method method)
getContainerElementAnnotationsback to summary
private static Annotation[] getContainerElementAnnotations(MethodParameter param)

There may be constraints on elements of a container (list, map).

isIndexOrKeyBasedContainerback to summary
private static boolean isIndexOrKeyBasedContainer(Class<?> type)