• springboot中参数处理


    springboot1中处理是这样的

    @Configuration
    public class WebConfig  extends WebMvcConfigurerAdapter{
        
        @Autowired
        UserArgumentResolver userArgumentResolver;
        
        @Override
        public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
            argumentResolvers.add(userArgumentResolver);
        }
        
        
    }

    再写一个参数处理类就可以了;

    package com.cxy.config;
    
    import javax.servlet.http.Cookie;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import com.cxy.domain.MiaoshaUser;
    import com.cxy.service.MiaoshaUserService;
    import org.apache.commons.lang3.StringUtils;
    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;
    
    
    @Service
    public class UserArgumentResolver implements HandlerMethodArgumentResolver {
    
        @Autowired
        MiaoshaUserService userService;
        
        public boolean supportsParameter(MethodParameter parameter) {
            Class<?> clazz = parameter.getParameterType();
            return clazz==MiaoshaUser.class;
        }
    
        public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer,
                NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
            HttpServletRequest request = webRequest.getNativeRequest(HttpServletRequest.class);
            HttpServletResponse response = webRequest.getNativeResponse(HttpServletResponse.class);
            
            String paramToken = request.getParameter(MiaoshaUserService.COOKI_NAME_TOKEN);
            String cookieToken = getCookieValue(request, MiaoshaUserService.COOKI_NAME_TOKEN);
            if(StringUtils.isEmpty(cookieToken) && StringUtils.isEmpty(paramToken)) {
                return null;
            }
            String token = StringUtils.isEmpty(paramToken)?cookieToken:paramToken;
            return userService.getByToken(response, token);
        }
    
        private String getCookieValue(HttpServletRequest request, String cookiName) {
            Cookie[]  cookies = request.getCookies();
            if(cookies == null || cookies.length <= 0){
                return null;
            }
            for(Cookie cookie : cookies) {
                if(cookie.getName().equals(cookiName)) {
                    return cookie.getValue();
                }
            }
            return null;
        }
    
    }

    如果你版本升级到2

    那么就需要进行:

    package com.cxy.config;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.web.method.support.HandlerMethodArgumentResolver;
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
    
    import java.util.List;
    @Configuration
    public class WebConfig1 implements WebMvcConfigurer{
        @Autowired
        UserArgumentResolver userArgumentResolver;
    
        @Override
        public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
            argumentResolvers.add(userArgumentResolver);
        }
    }
  • 相关阅读:
    基本输入输出函数
    变长参数表函数的编写
    一文精通Linux 命令行
    Linux 下的种种打包、压缩、解压命令
    GIT补丁怎么打?
    GIT 合并的冲突解决途径
    GIT 库整理方法
    GIT中常用命令详解1.reset
    霍夫曼编码及译码
    C语言链表实现队列
  • 原文地址:https://www.cnblogs.com/xiufengchen/p/11653748.html
Copyright © 2020-2023  润新知