request请求util

我们在做一些对 request 处理的时候,比如 写权限框架时,经常会对 request 做一下拦截或者处理,比如 url的解析和处理,或者参数的解析和处理,下面提供一个我常用的 util,希望能帮到大家。

import cn.hutool.core.util.StrUtil;
import com.google.common.base.Joiner;
import com.sun.istack.internal.NotNull;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.annotation.Annotation;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 描述: request请求util </br>
 * 时间: 2021-02-08 11:22  </br>
 * 作者:IT学习道场
 */
public class RequestUtil {
    /**
     * 获取请求的路径,包含controller的映射和方法映射和路径参数
     * @param request
     * @param response
     * @return
     */
    public static String getServletPath(HttpServletRequest request, HttpServletResponse response){
        return request.getServletPath();
    }

    /**
     * 获取真实Url,包含controller的映射和方法映射,去除path的参数
     * 常用在拦截器中
     * @param handler   controller对象
     * @param url       请求的url路径 getServletPath 可以获取
     * @return
     */
    public static String getRealUrl(Object handler, String url){
        Annotation[][] parameterAnnotations = ((HandlerMethod) handler).getMethod().getParameterAnnotations();
        int i = 0;
        for (Annotation[] annotations : parameterAnnotations) {
            for (Annotation annotation : annotations) {
                if(annotation instanceof PathVariable){
                    i++;
                    break;
                }
            }
        }
        if (i == 0){
            return url;
        }
        List<String> split = Arrays.asList(url.split("\\/"));
        List<String> subList = split.subList(0, split.size() - i);
        String join = Joiner.on("/").join(subList);
        return join;
    }

    /**
     * 获取请求的路径映射,包含controller的映射和方法映射,失灵时不灵
     * @param request
     * @param response
     * @return
     */
    public static String getServletRequestMapping(HttpServletRequest request, HttpServletResponse response){
        String servletPath = getServletPath(request, response);
        //Map<String, String[]> parameterMap = getParameterMap(request, response);
        Map<String, String[]> parameterMap = (Map) request.getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE);
        if (CollectionUtils.isEmpty(parameterMap)){
            return servletPath;
        }
        Integer paramSize = parameterMap.size();
        int count = StrUtil.count(servletPath, "/");
        int subIndex = StrUtil.ordinalIndexOf(servletPath,"/", count - paramSize + 1);
        String result = servletPath.substring(0, subIndex);
        return result;
    }

    /**
     * 获取请求的路径的所有参数map
     * @param request
     * @param response
     * @return
     */
    public static Map<String, String[]> getParameterMap(HttpServletRequest request, HttpServletResponse response){
        return request.getParameterMap();
    }

    /**
     * 获取请求的路径的指定参数的value数组
     * @param paramKey 请求的路径的参数key
     * @param request 请求
     * @param response 响应
     * @return
     */
    public static String[] getParameterArrayByParamKey(@NotNull String paramKey, HttpServletRequest request, HttpServletResponse response){
        Map<String, String[]> parameterMap = getParameterMap(request, response);
        for (String key : parameterMap.keySet()) {
            if (key.equals(paramKey)){
               return parameterMap.get(key);
            }
        }
        return null;
    }
    /**
     * 获取请求的路径的指定参数的value数组中第index的值
     * @param paramKey 请求的路径的参数key
     * @param request 请求
     * @param response 响应 getFirstParameterByParamKey
     * @return
     */
    public static String getIndexParameterByParamKey(@NotNull String paramKey, int index, HttpServletRequest request, HttpServletResponse response){
        String[] parameterArray = getParameterArrayByParamKey(paramKey, request, response);
        for (int i = 0, length = parameterArray.length; i < length; i++ ){
            if (i == index){
                return parameterArray[i];
            }
        }
        return null;
    }

    /**
     * 获取请求的路径的指定参数的value数组中第index的值
     * @param paramKey 请求的路径的参数key
     * @param request 请求
     * @param response 响应
     * @return
     */
    public static String getFirstParameterByParamKey(@NotNull String paramKey, HttpServletRequest request, HttpServletResponse response){
        return getIndexParameterByParamKey(paramKey, 0, request, response);
    }



}


作者:IT学习道场


欢迎关注微信公众号 : IT学习道场