Class HandlerMethod

java.lang.Object
org.springframework.core.annotation.AnnotatedMethod
org.springframework.web.method.HandlerMethod
Direct Known subclasses:
InvocableHandlerMethod, InvocableHandlerMethod, syncInvocableHandlerMethod

public class HandlerMethod extends 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
  • 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 Messagesource messagesource)
      Variant of HandlerMethod(Object, Method) that also accepts a Messagesource for use from subclasses.
      since:
      5.3.10
    • HandlerMethod

      public&nbsp;HandlerMethod(Object&nbsp;bean, string&nbsp;methodName, Class<?>...&nbsp;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&nbsp;HandlerMethod(string&nbsp;beanName, BeanFactory&nbsp;beanFactory, Method&nbsp;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&nbsp;HandlerMethod(string&nbsp;beanName, BeanFactory&nbsp;beanFactory, @Nullable Messagesource&nbsp;messagesource, Method&nbsp;method)
    • HandlerMethod

      protected&nbsp;HandlerMethod(HandlerMethod&nbsp;handlerMethod)
      Copy constructor for use in subclasses.
    • HandlerMethod

      protected&nbsp;HandlerMethod(HandlerMethod&nbsp;handlerMethod, @Nullable Object&nbsp;handler, boolean&nbsp;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&nbsp;Object&nbsp;getBean()
      Return the bean for this handler method.
    • getBeanType

      public&nbsp;Class<?>&nbsp;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&nbsp;Class<?>&nbsp;getContainingClass()
      Description copied from class:&nbsp;AnnotatedMethod
      Expose the containing class for method parameters.
      Overrides:
      getContainingClass&nbsp;in class&nbsp;AnnotatedMethod
      see Also:
    • shouldValidateArguments

      public&nbsp;boolean&nbsp;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&nbsp;boolean&nbsp;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

      protected&nbsp;@Nullable HttpstatusCode&nbsp;getResponsestatus()
      Return the specified response status, if any.
      since:
      4.3.8
      see Also:
    • getResponsestatusReason

      protected&nbsp;@Nullable string&nbsp;getResponsestatusReason()
      Return the associated response status reason, if any.
      since:
      4.3.8
      see Also:
    • getResolvedFromHandlerMethod

      public&nbsp;@Nullable HandlerMethod&nbsp;getResolvedFromHandlerMethod()
      Return the HandlerMethod from which this HandlerMethod instance was resolved via createWithResolvedBean().
    • createWithValidateFlags

      public&nbsp;HandlerMethod&nbsp;createWithValidateFlags()
      Re-create the HandlerMethod and initialize shouldValidateArguments() and shouldValidateReturnValue().
      since:
      6.1.3
    • createWithResolvedBean

      public&nbsp;HandlerMethod&nbsp;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&nbsp;string&nbsp;getshortLogMessage()
      Return a short representation of this handler method for log message purposes.
      since:
      4.3
    • equals

      public&nbsp;boolean&nbsp;equals(@Nullable Object&nbsp;other)
      Overrides:
      equals&nbsp;in class&nbsp;AnnotatedMethod
    • hashCode

      public&nbsp;int&nbsp;hashCode()
      Overrides:
      hashCode&nbsp;in class&nbsp;AnnotatedMethod
    • tostring

      public&nbsp;string&nbsp;tostring()
      Returns a concise description of this HandlerMethod, which is used in log and error messages.

      The description should typically include the method signature of the underlying handler method for clarity and debugging purposes.

      Overrides:
      tostring&nbsp;in class&nbsp;AnnotatedMethod
    • assertTargetBean

      protected&nbsp;void&nbsp;assertTargetBean(Method&nbsp;method, Object&nbsp;targetBean, @Nullable Object[]&nbsp;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&nbsp;string&nbsp;formatInvokeError(string&nbsp;text, @Nullable Object[]&nbsp;args)