快上网专注成都网站设计 成都网站制作 成都网站建设
成都网站建设公司服务热线:028-86922220

网站建设知识

十年网站开发经验 + 多家企业客户 + 靠谱的建站团队

量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决

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 argumentResolvers) {
    argumentResolvers.add(userArgumentResolver);
  }

  public void addResourceHandlers(ResourceHandlerRegistry registry){
    registry.addResourceHandler("/**").addResourceLocations(ResourceUtils.CLASSPATH_URL_PREFIX+"/static/");
    super.addResourceHandlers(registry);
  }
}

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


分享名称:Spring自定义参数解析器代码实例
网页网址:http://6mz.cn/article/posccj.html

其他资讯