1、HandlerMethodArgumentResolver的作用
在spring上下文解析参数,并将解析后的值作为方法入参,例如org.springframework.web.bind.annotation.RequestBody就是基于这个接口实现的
实现 HandlerMethodArgumentResolver接口,必须重写上面这两个方法
org.springframework.web.method.support.HandlerMethodArgumentResolver#supportsParameter:检查是否需要进行参数解析
org.springframework.web.method.support.HandlerMethodArgumentResolver#resolveArgument:
将方法参数解析为给定请求的参数值
2、简单的使用案例
package com.cn.dl.springbootdemo.test.user.annotation;
import java.lang.annotation.*;
/**
* @author: yanshao
* @date: 2022/4/29 10:14 上午
*/
@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface MethodParamTest {
String value() default "";
}
package com.cn.dl.springbootdemo.test.user.resolver;
import com.cn.dl.springbootdemo.test.user.annotation.MethodParamTest;
import org.springframework.core.MethodParameter;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;
import java.util.Objects;
/**
* @author: yanshao
* @date: 2022/4/29 10:14 上午
*/
public class MethodParamMethodArgumentResolver implements HandlerMethodArgumentResolver {
@Override
public boolean supportsParameter(MethodParameter parameter) {
return Objects.nonNull(parameter.getParameterAnnotation(MethodParamTest.class));
}
@Override
public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer modelAndViewContainer, NativeWebRequest nativeWebRequest, WebDataBinderFactory webDataBinderFactory) throws Exception {
//todo
return "hello 你好";
}
}
package com.cn.dl.springbootdemo.test.user.configuration;
import com.cn.dl.springbootdemo.test.user.resolver.MethodParamMethodArgumentResolver;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import java.util.List;
/**
* @author: yanshao
* @date: 2022/4/29 10:14 上午
*/
@Configuration
public class MethodParamTestConfiguration implements WebMvcConfigurer {
@Override
public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {
resolvers.add(new MethodParamMethodArgumentResolver());
}
}
package com.cn.dl.springbootdemo.controller;
import com.cn.dl.springbootdemo.test.user.annotation.MethodParamTest;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author yanshao
* @date 2022/4/29 2:45 下午
*/
@RequestMapping("demo")
@RestController
public class DemoController {
@RequestMapping("methodParamTest")
public String methodParamTest(@MethodParamTest String value){
System.out.println("methodParamTest>>" + value);
return value;
}
}
是不是可以利用org.springframework.web.method.support.HandlerMethodArgumentResolver做一个登录验证并且将登录用户信息作为入参的方法?那么就需要在com.cn.dl.springbootdemo.test.user.resolver.MethodParamMethodArgumentResolver#resolveArgument方法里面做文章了,在这个方法里面调用我们指定的方法解析参数
package com.cn.dl.springbootdemo.test.user.annotation;
import java.lang.annotation.*;
/**
* @author: yanshao
* @date: 2022/4/29 10:14 上午
*/
@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface MethodParamTest {
/**
* 指定检查用户是否登录并且组装登录用户信息的方法名
*
* @return
*/
String handlerMethod();
/**
* 指定检查用户是否登录并且组装登录用户信息的方法所在的类
*
* @return
*/
Class<?> handlerClass();
}
然后在com.cn.dl.springbootdemo.test.user.resolver.MethodParamMethodArgumentResolver#resolveArgument里面调用com.cn.dl.springbootdemo.test.user.annotation.MethodParamTest指定的方法进行登录验证和登录用户信息的组装,这样我们就可以将上面的基础代码以jar包的形式,在其它项目里面就可以使用,不需要重复造轮子,而且这种登录验证比拦截器或者过滤器更优雅一些 。