HandlerMethodArgumentResolver

    1. 第一个方法用来判断是否需要进行参数解析
    2. 第二个方法返回值就是解析的参数值
    1. 方法分析
    2. 自定义解析器需要实现HandlerMethodArgumentResolver接口,HandlerMethodArgumentResolver接口包含两个接口:
    3. package org.springframework.web.method.support;
    4. import org.springframework.core.MethodParameter;
    5. import org.springframework.web.bind.WebDataBinder;
    6. import org.springframework.web.bind.support.WebDataBinderFactory;
    7. import org.springframework.web.context.request.NativeWebRequest;
    8. /**
    9. ● Strategy interface for resolving method parameters into argument values in
    10. ● the context of a given request.
    11. ● @author Arjen Poutsma
    12. ● @since 3.1
    13. ● @see HandlerMethodReturnValueHandler
    14. */
    15. public interface HandlerMethodArgumentResolver {
    16. boolean supportsParameter(MethodParameter parameter);
    17. Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer,
    18. NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception;
    19. }
    20. 2.1 接口说明:
    21. supportsParameter:用于判定是否需要处理该参数分解,返回true为需要,并会去调用下面的方法resolveArgument
    22. resolveArgument:真正用于处理参数分解的方法,返回的Object就是controller方法上的形参对象。
    1. 使用方法
    2. 3.1 自定义注解
    3. import java.lang.annotation.ElementType;
    4. import java.lang.annotation.Retention;
    5. import java.lang.annotation.RetentionPolicy;
    6. import java.lang.annotation.Target;
    7. /**
    8. ● 登录用户信息
    9. */
    10. @Target(ElementType.PARAMETER)
    11. @Retention(RetentionPolicy.RUNTIME)
    12. public @interface LoginUser {
    13. }
    1. 3.2 自定义参数解析器,实现HandlerMethodArgumentResolver接口
    2. package io.renren.modules.app.resolver;
    3. import io.renren.modules.app.annotation.LoginUser;
    4. import io.renren.modules.app.entity.UserEntity;
    5. import io.renren.modules.app.interceptor.AuthorizationInterceptor;
    6. import io.renren.modules.app.service.UserService;
    7. import org.springframework.beans.factory.annotation.Autowired;
    8. import org.springframework.core.MethodParameter;
    9. import org.springframework.stereotype.Component;
    10. import org.springframework.web.bind.support.WebDataBinderFactory;
    11. import org.springframework.web.context.request.NativeWebRequest;
    12. import org.springframework.web.context.request.RequestAttributes;
    13. import org.springframework.web.method.support.HandlerMethodArgumentResolver;
    14. import org.springframework.web.method.support.ModelAndViewContainer;
    15. /**
    16. ● 有@LoginUser注解的方法参数,注入当前登录用户
    17. */
    18. @Component
    19. public class LoginUserHandlerMethodArgumentResolver implements HandlerMethodArgumentResolver {
    20. @Autowired
    21. private UserService userService;
    22. @Override
    23. public boolean supportsParameter(MethodParameter parameter) {
    24. return parameter.getParameterType().isAssignableFrom(UserEntity.class) && parameter.hasParameterAnnotation(LoginUser.class);
    25. }
    26. @Override
    27. public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer container,
    28. NativeWebRequest request, WebDataBinderFactory factory) throws Exception {
    29. //获取用户ID
    30. Object object = request.getAttribute(AuthorizationInterceptor.USER_KEY, RequestAttributes.SCOPE_REQUEST);
    31. if(object == null){
    32. return null;
    33. }
    34. //获取用户信息
    35. UserEntity user = userService.selectById((Long)object);
    36. return user;
    37. }
    38. }
    1. 3.3 注册自定义参数解析器
    2. Springboot中使用方法:
    3. import java.util.List;
    4. /**
    5. ● MVC配置
    6. */
    7. @Configuration
    8. public class WebMvcConfig extends WebMvcConfigurerAdapter {
    9. @Autowired
    10. private AuthorizationInterceptor authorizationInterceptor;
    11. @Autowired
    12. private LoginUserHandlerMethodArgumentResolver loginUserHandlerMethodArgumentResolver;
    13. @Override
    14. public void addInterceptors(InterceptorRegistry registry) {
    15. registry.addInterceptor(authorizationInterceptor).addPathPatterns("/api/**");
    16. }
    17. @Override
    18. public void addArgumentResolvers(List argumentResolvers) {
    19. argumentResolvers.add(loginUserHandlerMethodArgumentResolver);
    20. }
    21. }
    22. 3.4 Controller使用自定义注解
    23. @GetMapping("info")
    24. public UserEntity userInfo(@LoginUser UserEntity user){
    25. return user;
    26. }