Spring自定义参数解析器代码实例

发布时间:2020-08-27 16:54:18 作者:咸鱼加点盐
来源:脚本之家 阅读:128

这篇文章主要介绍了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);
  }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持亿速云。

推荐阅读:
  1. Spring MVC获取查询参数及路径参数代码实例
  2. Spring boot中自定义Json参数解析器的方法

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

spring 自定义 参数

上一篇:完美实现js拖拽效果 return false用法详解

下一篇:mongodb与mysql区别

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》