本篇文章給大家分享的是有關(guān)Spring中如何自定義參數(shù)解析器,小編覺得挺實用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。
成都創(chuàng)新互聯(lián)是一家集網(wǎng)站建設(shè),太平企業(yè)網(wǎng)站建設(shè),太平品牌網(wǎng)站建設(shè),網(wǎng)站定制,太平網(wǎng)站建設(shè)報價,網(wǎng)絡(luò)營銷,網(wǎng)絡(luò)優(yōu)化,太平網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強企業(yè)競爭力??沙浞譂M足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時我們時刻保持專業(yè)、時尚、前沿,時刻以成就客戶成長自我,堅持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實用型網(wǎng)站。結(jié)合redis編寫User自定義參數(shù)解析器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中注冊自定義參數(shù)解析器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<HandlerMethodArgumentResolver> argumentResolvers) { argumentResolvers.add(userArgumentResolver); } public void addResourceHandlers(ResourceHandlerRegistry registry){ registry.addResourceHandler("/**").addResourceLocations(ResourceUtils.CLASSPATH_URL_PREFIX+"/static/"); super.addResourceHandlers(registry); }}
以上就是Spring中如何自定義參數(shù)解析器,小編相信有部分知識點可能是我們?nèi)粘9ぷ鲿姷交蛴玫降?。希望你能通過這篇文章學(xué)到更多知識。更多詳情敬請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
本文題目:Spring中如何自定義參數(shù)解析器-創(chuàng)新互聯(lián)
鏈接URL:http://www.rwnh.cn/article16/cepsgg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供域名注冊、靜態(tài)網(wǎng)站、用戶體驗、品牌網(wǎng)站建設(shè)、做網(wǎng)站、動態(tài)網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)