您好,登錄后才能下訂單哦!
這篇文章主要介紹了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); } }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。