Spring自定义参数解析器代码实例
作者:咸鱼加点盐 发布时间:2023-07-02 15:08:06
标签:spring,自定义,参数,解析器
这篇文章主要介绍了Spring自定义参数解析器代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
结合redis编写User自定义参数解析器UserArgumentResolver
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.MethodParameter;
import org.springframework.stereotype.Service;
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 cn.xingaohbd.seckil.model.User;
import cn.xingaohbd.seckil.service.UserService;
@Service
public class UserArgumentResolver implements HandlerMethodArgumentResolver {
@Autowired
public UserService userService;
public String getParameterCokies(HttpServletRequest request,String tokenName){
Cookie[] cookies = request.getCookies();
for (Cookie ck : cookies) {
if(ck.getName().equals(tokenName)){
return ck.getValue();
}
}
return null;
}
@Override
public boolean supportsParameter(MethodParameter methodParameter) {
Class<?> p_class = methodParameter.getParameterType();
return p_class == User.class;
}
@Override
public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer,
NativeWebRequest nativeWebRequest, WebDataBinderFactory binderFactory) throws Exception {
HttpServletRequest request = nativeWebRequest.getNativeRequest(HttpServletRequest.class);
String requestParameter_token = request.getParameter("token");
String cokies_token = getParameterCokies(request, "token");
if(requestParameter_token == null && cokies_token == null){
return null;
}
return userService.getUserFromRedisByToken(requestParameter_token != null ? requestParameter_token : cokies_token);
}
}
UserService
@Service
@Transactional
public class UserServiceImpl implements UserService {
@Autowired
private UserRedis userRedis;
@Override
public Object getUserFromRedisByToken(String token) {
return userRedis.get(token);
}
}
在WebConfig中注册自定义参数解析器UserArgumentResolver
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.ResourceUtils;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
@Configuration
public class WebConfig extends WebMvcConfigurationSupport{
@Autowired
UserArgumentResolver userArgumentResolver;
@Override
protected void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
argumentResolvers.add(userArgumentResolver);
}
public void addResourceHandlers(ResourceHandlerRegistry registry){
registry.addResourceHandler("/**").addResourceLocations(ResourceUtils.CLASSPATH_URL_PREFIX+"/static/");
super.addResourceHandlers(registry);
}
}
来源:https://www.cnblogs.com/hjy415340835/p/11081702.html


猜你喜欢
- 前言参数绑定,简单来说就是客户端发送请求,而请求中包含一些数据,那么这些数据怎么到达 Controller ?这在实际项目开发中也是用到的最
- 现在有好多扫描识别银行卡号的SDK都是收费的,但是也有不收费的,但是有一定的问题,就是那种印刷的银行卡号扫描不出来,希望哪位大神指导原因给解
- 目录前言基本概念实践1. 首先准备一个控制台小程序2. 编写使用 AppDomain 的程序一些思考1. 为什么不使用 AppDomain
- 一、前言1、简单的登录验证可以通过Session或者Cookie实现。2、每次登录的时候都要进数据库校验下账户名和密码,只是加了cookie
- 前言:mybatisplus 可以说是对mybatis更好的拓展,一些简单的增删改查的操作已经被作者实现,我们只需引用即可。1.数据库建表这
- 一、Servlet概述1.sun公司提供的动态web资源开发技术。本质是上一段java小程序,要求这个小程序必须实现Servlet接口,以便
- 本章节内容很丰富,主要有基本的表单操作,数据的格式化,数据的校验,以及提示信息的国际化等实用技能。首先看效果图项目结构图接下来用代码重点学习
- 文章按照 Socket 的 创建、连接、传输数据、释放资源的过程来写。给出方法、参数的详细信息。一,网络基础说到 Socket,需要学习一下
- 这篇文章主要介绍了spring boot 全局异常处理方法汇总,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,
- 前言Java的StringUtil.isEmpty(str)和"".equals(str)都是用来判断字符串是否为空的方
- 一、JSON格式介绍JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式。相对于另一种数据交换格式
- 一.关于使用Mybatisplus自带的selectById和insert方法时的一些问题1.selectById的问题(1).表的主键列名
- 在Android控件View的文字周围添加图标,供大家参考,具体内容如下在控件TextView文字周围放置图片(基于TextView的But
- 本文介绍了Android短信的发送和广播接收者实现短信的监听,要注意Android清单中权限的设置以及广播的注册监听实现,废话不多说,代码如
- 1. 需求和思路最近要用java制作一个数据集,每一行是一个样本,格式是csv。用了一下java类的相关概念,把csv文件里的每一行,即每一
- 本文实例讲述了C#图像处理之边缘检测(Smoothed)的方法。分享给大家供大家参考。具体如下://定义smoothed算子边缘检测函数pr
- 前言在我们做后端服务Dao层开发,特别是大数据批量插入的时候,这时候普通的ORM框架(Mybatis、hibernate、JPA)就无法满足
- 以下内容通过1、实现目标注入程序,2、实现主程序,3、实现注入函数,4、thumb指令集实现等4个方面详细分析了android中inline
- 延迟加载1 使用延迟加载意义在进行数据查询时,为了提高数据库查询性能,尽量使用单表查询,因为单表查询比多表关联查询速度要快。如果查询单表就可
- String类原生的字符串处理方法short s=1;s=s+1;与short s=1;s+=1;的区别一、“+&