Spring自定义参数解析器代码实例
这篇文章主要介绍了Spring自定义参数解析器代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
结合redis编写User自定义参数解析器UserArgumentResolver
importjavax.servlet.http.Cookie; importjavax.servlet.http.HttpServletRequest; importorg.springframework.beans.factory.annotation.Autowired; importorg.springframework.core.MethodParameter; importorg.springframework.stereotype.Service; importorg.springframework.web.bind.support.WebDataBinderFactory; importorg.springframework.web.context.request.NativeWebRequest; importorg.springframework.web.method.support.HandlerMethodArgumentResolver; importorg.springframework.web.method.support.ModelAndViewContainer; importcn.xingaohbd.seckil.model.User; importcn.xingaohbd.seckil.service.UserService; @Service publicclassUserArgumentResolverimplementsHandlerMethodArgumentResolver{ @Autowired publicUserServiceuserService; publicStringgetParameterCokies(HttpServletRequestrequest,StringtokenName){ Cookie[]cookies=request.getCookies(); for(Cookieck:cookies){ if(ck.getName().equals(tokenName)){ returnck.getValue(); } } returnnull; } @Override publicbooleansupportsParameter(MethodParametermethodParameter){ Class>p_class=methodParameter.getParameterType(); returnp_class==User.class; } @Override publicObjectresolveArgument(MethodParameterparameter,ModelAndViewContainermavContainer, NativeWebRequestnativeWebRequest,WebDataBinderFactorybinderFactory)throwsException{ HttpServletRequestrequest=nativeWebRequest.getNativeRequest(HttpServletRequest.class); StringrequestParameter_token=request.getParameter("token"); Stringcokies_token=getParameterCokies(request,"token"); if(requestParameter_token==null&&cokies_token==null){ returnnull; } returnuserService.getUserFromRedisByToken(requestParameter_token!=null?requestParameter_token:cokies_token); } }
UserService
@Service @Transactional publicclassUserServiceImplimplementsUserService{ @Autowired privateUserRedisuserRedis; @Override publicObjectgetUserFromRedisByToken(Stringtoken){ returnuserRedis.get(token); } }
在WebConfig中注册自定义参数解析器UserArgumentResolver
importjava.util.List; importorg.springframework.beans.factory.annotation.Autowired; importorg.springframework.context.annotation.Configuration; importorg.springframework.util.ResourceUtils; importorg.springframework.web.method.support.HandlerMethodArgumentResolver; importorg.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; importorg.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport; @Configuration publicclassWebConfigextendsWebMvcConfigurationSupport{ @Autowired UserArgumentResolveruserArgumentResolver; @Override protectedvoidaddArgumentResolvers(ListargumentResolvers){ argumentResolvers.add(userArgumentResolver); } publicvoidaddResourceHandlers(ResourceHandlerRegistryregistry){ registry.addResourceHandler("/**").addResourceLocations(ResourceUtils.CLASSPATH_URL_PREFIX+"/static/"); super.addResourceHandlers(registry); } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。