Class AbstractNamedValueMethodArgumentResolver

java.lang.Object
org.springframework.web.method.annotation.AbstractNamedValueMethodArgumentResolver
All Implemented Interfaces:
HandlerMethodArgumentResolver
Direct Known Subclasses:
AbstractCookieValueMethodArgumentResolver, ExpressionValueMethodArgumentResolver, RequestHeaderMethodArgumentResolver, RequestParamMethodArgumentResolver

public abstract class AbstractNamedValueMethodArgumentResolver extends Object implements HandlerMethodArgumentResolver
Abstract base class for resolving method arguments from a named value. Request parameters, request headers, and path variables are examples of named values. Each may have a name, a required flag, and a default value.

Subclasses define how to do the following:

  • Obtain named value information for a method parameter
  • Resolve names into argument values
  • Handle missing argument values when argument values are required
  • Optionally handle a resolved value

A default value string can contain ${...} placeholders and Spring Expression Language #{...} expressions. For this to work a ConfigurableBeanFactory must be supplied to the class constructor.

A WebDataBinder is created to apply type conversion to the resolved argument value if it doesn't match the method parameter type.

Since:
3.1
Author:
Arjen Poutsma, Rossen Stoyanchev, Juergen Hoeller, Sebastien Deleuze
  • Constructor Details

    • AbstractNamedValueMethodArgumentResolver

      public AbstractNamedValueMethodArgumentResolver()
    • AbstractNamedValueMethodArgumentResolver

      public AbstractNamedValueMethodArgumentResolver(@Nullable org.springframework.beans.factory.config.ConfigurableBeanFactory beanFactory)
      Parameters:
      beanFactory - a bean factory to use for resolving ${...} placeholder and #{...} SpEL expressions in default values, or null if default values are not expected to contain expressions
  • Method Details

    • resolveArgument

      @Nullable public final Object resolveArgument(org.springframework.core.MethodParameter parameter, @Nullable ModelAndViewContainer mavContainer, NativeWebRequest webRequest, @Nullable WebDataBinderFactory binderFactory) throws Exception
      Description copied from interface: HandlerMethodArgumentResolver
      Resolves a method parameter into an argument value from a given request. A ModelAndViewContainer provides access to the model for the request. A WebDataBinderFactory provides a way to create a WebDataBinder instance when needed for data binding and type conversion purposes.
      Specified by:
      resolveArgument in interface HandlerMethodArgumentResolver
      Parameters:
      parameter - the method parameter to resolve. This parameter must have previously been passed to HandlerMethodArgumentResolver.supportsParameter(org.springframework.core.MethodParameter) which must have returned true.
      mavContainer - the ModelAndViewContainer for the current request
      webRequest - the current request
      binderFactory - a factory for creating WebDataBinder instances
      Returns:
      the resolved argument value, or null if not resolvable
      Throws:
      Exception - in case of errors with the preparation of argument values
    • createNamedValueInfo

      protected abstract AbstractNamedValueMethodArgumentResolver.NamedValueInfo createNamedValueInfo(org.springframework.core.MethodParameter parameter)
      Create the AbstractNamedValueMethodArgumentResolver.NamedValueInfo object for the given method parameter. Implementations typically retrieve the method annotation by means of MethodParameter.getParameterAnnotation(Class).
      Parameters:
      parameter - the method parameter
      Returns:
      the named value information
    • resolveName

      @Nullable protected abstract Object resolveName(String name, org.springframework.core.MethodParameter parameter, NativeWebRequest request) throws Exception
      Resolve the given parameter type and value name into an argument value.
      Parameters:
      name - the name of the value being resolved
      parameter - the method parameter to resolve to an argument value (pre-nested in case of a Optional declaration)
      request - the current request
      Returns:
      the resolved argument (may be null)
      Throws:
      Exception - in case of errors
    • handleMissingValue

      protected void handleMissingValue(String name, org.springframework.core.MethodParameter parameter, NativeWebRequest request) throws Exception
      Invoked when a named value is required, but resolveName(String, MethodParameter, NativeWebRequest) returned null and there is no default value. Subclasses typically throw an exception in this case.
      Parameters:
      name - the name for the value
      parameter - the method parameter
      request - the current request
      Throws:
      Exception
      Since:
      4.3
    • handleMissingValue

      protected void handleMissingValue(String name, org.springframework.core.MethodParameter parameter) throws ServletException
      Invoked when a named value is required, but resolveName(String, MethodParameter, NativeWebRequest) returned null and there is no default value. Subclasses typically throw an exception in this case.
      Parameters:
      name - the name for the value
      parameter - the method parameter
      Throws:
      ServletException
    • handleMissingValueAfterConversion

      protected void handleMissingValueAfterConversion(String name, org.springframework.core.MethodParameter parameter, NativeWebRequest request) throws Exception
      Invoked when a named value is present but becomes null after conversion.
      Parameters:
      name - the name for the value
      parameter - the method parameter
      request - the current request
      Throws:
      Exception
      Since:
      5.3.6
    • handleResolvedValue

      protected void handleResolvedValue(@Nullable Object arg, String name, org.springframework.core.MethodParameter parameter, @Nullable ModelAndViewContainer mavContainer, NativeWebRequest webRequest)
      Invoked after a value is resolved.
      Parameters:
      arg - the resolved argument value
      name - the argument name
      parameter - the argument parameter type
      mavContainer - the ModelAndViewContainer (may be null)
      webRequest - the current request