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);
  }
}

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

您可能感兴趣的文章:


  • 本文相关:
  • spring boot中自定义json参数解析器的方法
  • spring boot应用配置常用相关视图解析器详解
  • springboot使用自定义json解析器的使用方法
  • 浅谈springmvc之视图解析器(viewresolver)
  • springmvc自定义参数绑定实现详解
  • springboot js-sdk自定义微信分享的实现
  • spring boot创建自定义starter的完整步骤
  • 浅谈spring自定义注解从入门到精通
  • 理解zookeeper选举机制
  • spring mvc简介_动力节点java学院整理
  • 浅谈springboot整合rocketmq使用心得
  • java观察者模式例子
  • spring boot如何使用hikaricp连接池详解
  • java:"失效"的private修饰符
  • java经典用法总结
  • java对list集合进行排序collections.sort()
  • thread线程的基础知识及常见疑惑点总结
  • springboot中集成swagger2框架的方法
  • 免责声明 - 关于我们 - 联系我们 - 广告联系 - 友情链接 - 帮助中心 - 频道导航
    Copyright © 2017 www.zgxue.com All Rights Reserved