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;@Servicepublic 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@Transactionalpublic 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;@Configurationpublic class WebConfig extends WebMvcConfigurationSupport{ @Autowired UserArgumentResolver userArgumentResolver; @Override protected void addArgumentResolvers(List
以上就是Spring中如何自定义参数解析器,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注创新互联行业资讯频道。
网站题目:Spring中如何自定义参数解析器-创新互联
文章URL:http://azwzsj.com/article/cepsgg.html