当前位置:网站首页>HandlerMethodArgumentResolver(参数解析器)的作用+使用小案例
HandlerMethodArgumentResolver(参数解析器)的作用+使用小案例
2022-06-11 00:14:00 【达文西不辅助】
前言
今天在做项目时遇到了一个有关参数解析HandlerMethodArgumentResolver的使用疑惑。我想作为新手的我在此和大家一起交流并记录。接下来开始发车......
正文
首先先来认识一下这个接口,HandlerMethodArgumentResolver。
public interface HandlerMethodArgumentResolver {
boolean supportsParameter(MethodParameter var1);
Object resolveArgument(MethodParameter var1, ModelAndViewContainer var2, NativeWebRequest var3, WebDataBinderFactory var4) throws Exception;
}
大家可以看到,这个接口有两个方法,supportsParameter和resolveArgument。
方法supportsParameter很好理解,返回值是boolean类型,它的作用是判断Controller层中的参数,是否满足条件,满足条件则执行resolveArgument方法,不满足则跳过。
而resolveArgument方法呢,它只有在supportsParameter方法返回true的情况下才会被调用。用于处理一些业务,将返回值赋值给Controller层中的这个参数。
因此呢,我们可以将HandlerMethodArgumentResolver理解为是一个参数解析器,我们可以通过写一个类实现HandlerMethodArgumentResolver接口来实现对Controller层中方法参数的修改。
项目代码举例
未使用参数解析器时的Controller控制器:
@Controller
@RequestMapping("/goods")
public class GoodsController {
private static Logger logger = LoggerFactory.getLogger(GoodsController.class);
@Autowired
UserService userService;
@RequestMapping("/to_list")
public String to_login(Model model,HttpServletResponse response,
@CookieValue(value=UserService.TOKEN,required = false)String cookieToken,
@RequestParam(value = UserService.TOKEN,required = false) String paramToken) {
if(StringUtils.isEmpty(cookieToken)&&StringUtils.isEmpty(paramToken)){
return "login";
}
String token = StringUtils.isEmpty(cookieToken) ? paramToken : cookieToken;
User user=userService.getByToken(response,token);
logger.info(user.toString());
model.addAttribute("user",user);
return "goods_list";
}
}
请大家仔细看to_login方法的这两个参数 cookieToken和paramToken,再仔细看一下方法体。不难看出,这两个参数和方法体中的语句代码就是为了,通过cookie或request其中的token这一变量来查询用户user的信息。
但是大家发没发现,这一个方法,就为了简简单单的这一个小功能,就写了这么多代码。是不是有点过分臃肿呢?而且假如还有其他方法会用到用户user这一信息变量呢?
好的,这个时候我们就可以使用HandlerMethodArgumentResolver接口来进行一下小小的优化。
使用HandlerMethodArgumentResolver解析器后:
写一个类实现HandlerMethodArgumentResolver接口
@Service
public class UserArgumentResolver implements HandlerMethodArgumentResolver {
@Autowired
MiaoshaUserService userService;
public boolean supportsParameter(MethodParameter parameter) {
Class<?> clazz = parameter.getParameterType();
return clazz==MiaoshaUser.class;
}
public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer,
NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
HttpServletRequest request = webRequest.getNativeRequest(HttpServletRequest.class);
HttpServletResponse response = webRequest.getNativeResponse(HttpServletResponse.class);
String paramToken = request.getParameter(MiaoshaUserService.COOKI_NAME_TOKEN);
String cookieToken = getCookieValue(request, MiaoshaUserService.COOKI_NAME_TOKEN);
if(StringUtils.isEmpty(cookieToken) && StringUtils.isEmpty(paramToken)) {
return null;
}
String token = StringUtils.isEmpty(paramToken)?cookieToken:paramToken;
return userService.getByToken(response, token);
}
private String getCookieValue(HttpServletRequest request, String cookiName) {
Cookie[] cookies = request.getCookies();
for(Cookie cookie : cookies) {
if(cookie.getName().equals(cookiName)) {
return cookie.getValue();
}
}
return null;
}
}
把我们编写的这个类,注册到配置文件中去
@Configuration
public class WebConfig extends WebMvcConfigurerAdapter{
@Autowired
UserArgumentResolver userArgumentResolver;
@Override
public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
argumentResolvers.add(userArgumentResolver);
}
}
最后,我们的控制类就可以这样写了
@RequestMapping("/to_list")
public String list(Model model,MiaoshaUser user) {
model.addAttribute("user", user);
//查询商品列表
List<GoodsVo> goodsList = goodsService.listGoodsVo();
model.addAttribute("goodsList", goodsList);
return "goods_list";
}
————————————————
版权声明:本文为CSDN博主「翻翻oO」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_36408229/article/details/104436508
边栏推荐
- Optimization of startup under SYSTEMd, deleting useless SYSTEMd services
- Solid basic knowledge + correct method is the key to quickly read the source code
- 北京门头沟区高新技术企业培育支持标准,补贴10万
- Animation
- System interpretation: Authority Design Guide
- Non presented paper (no show) policy
- CentOS7 实战部署MySQL8(二进制方式)
- SqlServer中的锁
- 程序员应对压力的几个小窍门
- SQL audit | "cloud" users can use the SQL audit service with one click
猜你喜欢

The file "setup" does not exist. What should I do?

Pd虚拟机安装系统提示 “网络初始化失败 操作失败 ”的解决方案

About log traffic monitoring and early warning small project | database management tool: migrate

最好的创意鼓工具:Groove Agent 5

About log traffic monitoring and early warning small project | flag log monitoring script

ViewPager和底部无线循环的小圆点

CentOS7 实战部署MySQL8(二进制方式)

Team management | how to improve the thinking skills of technical leaders?

zabbix离线安装
![[introduction to ROS] - 03 single chip microcomputer, PC host and ROS communication mechanism](/img/ad/d798284ceb370f7c68cbab755a11de.png)
[introduction to ROS] - 03 single chip microcomputer, PC host and ROS communication mechanism
随机推荐
Optimization of startup under SYSTEMd, deleting useless SYSTEMd services
深圳中国专利奖政策支持介绍,补贴100万
SQL audit | "cloud" users can use the SQL audit service with one click
ion_dma_buf_begin_cpu_access
[persistent problems of NVIDIA driver] - - /dev/sdax:clean, xxx/xxx files, xxx/xxx blocks - the most complete solution
nodejs中使用mySql数据库
SqlServer中的鎖
[introduction to ROS] - 03 single chip microcomputer, PC host and ROS communication mechanism
Serrures dans SQLSERVER
[论文阅读] TGANet: Text-guided attention for improved polyp segmentation
Josephus problem_ Unidirectional circular linked list_ code implementation
北京昌平区高新技术企业培育支持标准,补贴10万
Small project on log traffic monitoring and early warning | environment foundation 2
CentOS7 实战部署MySQL8(二进制方式)
增额终身寿险产品都有哪些优势?门槛高吗?
ion_ mmap
Promise
北京朝阳区专精特新制造业企业支持标准介绍,补贴100万
[论文阅读] FixMatch: Simplifying Semi-Supervised Learning with Consistency and Confidence
一些有的没的闲话