Package org.springframework.web.method

# Class HandlerMethod

java.lang.Object
org.springframework.core.annotation.AnnotatedMethod
org.springframework.web.method.HandlerMethod

Direct Known Subclasses:
  `InvocableHandlerMethod`

public class HandlerMethod extends org.springframework.core.annotation.AnnotatedMethod
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 (for example, lazy-init bean, prototype bean). Use `createWithResolvedBean()` to obtain a `HandlerMethod` instance with a bean instance resolved through the associated `BeanFactory`.

Since:
  3.1
Author:
  Arjen Poutsma, Rossen Stoyanchev, Juergen Hoeller, Sam Brannen

## Nested Class Summary

## Nested classes/interfaces inherited from class org.springframework.core.annotation.AnnotatedMethod

`org.springframework.core.annotation.AnnotatedMethod.AnnotatedMethodParameter`

## Field Summary

Fields
Modifier and Type
Field
Description
`protected static final org.apache.commons.logging.Log`
`logger`
Logger that is available to subclasses.

## Constructor Summary

Constructors
Modifier
Constructor
Description
``
`HandlerMethod(Object bean, Method method)`
Create an instance from a bean instance and a method.
`protected`
`HandlerMethod(Object bean, Method method, org.springframework.context.MessageSource messageSource)`
Variant of `HandlerMethod(Object, Method)` that also accepts a `MessageSource` for use from subclasses.
``
`HandlerMethod(Object bean, String methodName, Class<?>... parameterTypes)`
Create an instance from a bean instance, method name, and parameter types.
``
`HandlerMethod(String beanName, org.springframework.beans.factory.BeanFactory beanFactory, Method method)`
Create an instance from a bean name, a method, and a `BeanFactory`.
``
`HandlerMethod(String beanName, org.springframework.beans.factory.BeanFactory beanFactory, org.springframework.context.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.
`protected`
`HandlerMethod(HandlerMethod handlerMethod, Object handler, boolean initValidateFlags)`
Re-create new HandlerMethod instance that copies the given HandlerMethod but replaces the handler, and optionally checks for the presence of validation annotations.

## Method Summary

Modifier and Type
Method
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.
`HandlerMethod`
`createWithResolvedBean()`
If the `handler` is a bean name rather than the actual handler instance, resolve the bean name through Spring configuration (e.g.
`HandlerMethod`
`createWithValidateFlags()`
Re-create the HandlerMethod and initialize `shouldValidateArguments()` and `shouldValidateReturnValue()`.
`boolean`
`equals(Object other)`
`protected String`
`formatInvokeError(String text, Object[] args)`
`Object`
`getBean()`
Return the bean for this handler method.
`Class<?>`
`getBeanType()`
This method returns the type of the handler for this handler method.
`protected Class<?>`
`getContainingClass()`
`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.
`String`
`getShortLogMessage()`
Return a short representation of this handler method for log message purposes.
`int`
`hashCode()`
`boolean`
`shouldValidateArguments()`
Whether the method arguments are a candidate for method validation, which is the case when there are parameter `jakarta.validation.Constraint` annotations.
`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.
`String`
`toString()`

### Methods inherited from class org.springframework.core.annotation.AnnotatedMethod

`findProvidedArgument, formatArgumentError, getBridgedMethod, getMethod, getMethodAnnotation, getMethodParameters, getReturnType, getReturnValueType, hasMethodAnnotation, isVoid`

### Methods inherited from class java.lang.Object

`clone, finalize, getClass, notify, notifyAll, wait, wait, wait`

## Field Details

### logger

protected static final org.apache.commons.logging.Log logger
Logger that is available to subclasses.

## Constructor Details

### HandlerMethod

public HandlerMethod(Object bean, Method method)
Create an instance from a bean instance and a method.

### HandlerMethod

protected HandlerMethod(Object bean, Method method, @Nullable org.springframework.context.MessageSource messageSource)
Variant of `HandlerMethod(Object, Method)` that also accepts a `MessageSource` for use from subclasses.

Since:
  5.3.10

### HandlerMethod

public HandlerMethod(Object bean, String methodName, Class<?>... parameterTypes) throws NoSuchMethodException
Create an instance from a bean instance, method name, and parameter types.

Throws:
  `NoSuchMethodException` - when the method cannot be found

### HandlerMethod

public HandlerMethod(String beanName, org.springframework.beans.factory.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.

### HandlerMethod

public HandlerMethod(String beanName, org.springframework.beans.factory.BeanFactory beanFactory, @Nullable org.springframework.context.MessageSource messageSource, Method method)
Variant of `HandlerMethod(String, BeanFactory, Method)` that also accepts a `MessageSource`.

### HandlerMethod

protected HandlerMethod(HandlerMethod handlerMethod)
Copy constructor for use in subclasses.

### HandlerMethod

protected HandlerMethod(HandlerMethod handlerMethod, @Nullable Object handler, boolean initValidateFlags)
Re-create new HandlerMethod instance that copies the given HandlerMethod but replaces the handler, and optionally checks for the presence of validation annotations.

Subclasses can override this to ensure that a HandlerMethod is of the same type if re-created.

Since:
  6.2.3

## Method Details

### getBean

public Object getBean()
Return the bean for this handler method.

### getBeanType

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.

### getContainingClass

protected Class<?> getContainingClass()

Overrides:
  `getContainingClass` in class `org.springframework.core.annotation.AnnotatedMethod`

### shouldValidateArguments

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

### shouldValidateReturnValue

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

### getResponseStatus

@Nullable protected HttpStatusCode getResponseStatus()
Return the specified response status, if any.

Since:
  4.3.8
See Also:
  - `ResponseStatus.code()`

### getResponseStatusReason

@Nullable protected String getResponseStatusReason()
Return the associated response status reason, if any.

Since:
  4.3.8
See Also:
  - `ResponseStatus.reason()`

### getResolvedFromHandlerMethod

@Nullable public HandlerMethod getResolvedFromHandlerMethod()
Return the HandlerMethod from which this HandlerMethod instance was resolved via `createWithResolvedBean()`.

### createWithValidateFlags

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

Since:
  6.1.3

### createWithResolvedBean

public HandlerMethod createWithResolvedBean()
If the `handler` is a bean name rather than the actual handler instance, resolve the bean name through Spring configuration (e.g. for prototype beans), and return a new `HandlerMethod` instance with the resolved handler.

If the `handler` is not String, return the same instance.

### getShortLogMessage

public String getShortLogMessage()
Return a short representation of this handler method for log message purposes.

Since:
  4.3

### equals

public boolean equals(@Nullable Object other)

Overrides:
  `equals` in class `org.springframework.core.annotation.AnnotatedMethod`

### hashCode

public int hashCode()

Overrides:
  `hashCode` in class `org.springframework.core.annotation.AnnotatedMethod`

### toString

public String toString()

Overrides:
  `toString` in class `org.springframework.core.annotation.AnnotatedMethod`

### assertTargetBean

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.

### formatInvokeError

protected String formatInvokeError(String text, Object[] args)