Parameter parser

When the parameter parser maps to some type of parameter of the Controller, it does the corresponding processing.

public interface HandlerMethodArgumentResolver {
    ResolveArgument is used to determine whether the argument should be resolved, returns true as yes, and calls the following method: resolveArgument
    boolean supportsParameter(MethodParameter parameter);
    // The actual method used to handle parameter decomposition returns the Object as the parameter Object on the controller method.
    Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception;
}
Copy the code

For example,

public class HandlerMethodArgumentResolverExtended implements HandlerMethodArgumentResolver {

    @Override
    public boolean supportsParameter(MethodParameter parameter) { Class<? > targetClass = parameter.getParameterType();if (targetClass.isAssignableFrom(PageRequest.class)
            || targetClass.isAssignableFrom(UserProfile.class)
            || targetClass.isAssignableFrom(MobileProfile.class)) {
            return true;
        }
        return false;
    }

    @Override
    public Object resolveArgument( MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception { Class<? > targetClass = parameter.getParameterType();if (targetClass.isAssignableFrom(PageRequest.class)) {
            return PageHelper.buildPageRequest(webRequest);
        } else if (targetClass.isAssignableFrom(UserProfile.class)) {
            return UserHelper.buildUserProfile(webRequest);
        } else if (targetClass.isAssignableFrom(MobileProfile.class)) {
            return UserHelper.buildMobileProfile(webRequest);
        }
        returnWebArgumentResolver.UNRESOLVED; }}Copy the code

scenario

  1. Convert different paging parameters passed in from the front end to parameters that conform to the framework specification, for examplePageable;
  2. Assign some parameters obtained by the transformation to parameters in the Controller;