request請求util

我們在做一些對 request 處理的時候,比如 寫權(quán)限框架時,經(jīng)常會對 request 做一下攔截或者處理,比如 url的解析和處理,或者參數(shù)的解析和處理,下面提供一個我常用的 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的映射和方法映射和路徑參數(shù)
     * @param request
     * @param response
     * @return
     */
    public static String getServletPath(HttpServletRequest request, HttpServletResponse response){
        return request.getServletPath();
    }

    /**
     * 獲取真實Url,包含controller的映射和方法映射,去除path的參數(shù)
     * 常用在攔截器中
     * @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;
    }

    /**
     * 獲取請求的路徑的所有參數(shù)map
     * @param request
     * @param response
     * @return
     */
    public static Map<String, String[]> getParameterMap(HttpServletRequest request, HttpServletResponse response){
        return request.getParameterMap();
    }

    /**
     * 獲取請求的路徑的指定參數(shù)的value數(shù)組
     * @param paramKey 請求的路徑的參數(shù)key
     * @param request 請求
     * @param response 響應(yīng)
     * @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;
    }
    /**
     * 獲取請求的路徑的指定參數(shù)的value數(shù)組中第index的值
     * @param paramKey 請求的路徑的參數(shù)key
     * @param request 請求
     * @param response 響應(yīng) 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;
    }

    /**
     * 獲取請求的路徑的指定參數(shù)的value數(shù)組中第index的值
     * @param paramKey 請求的路徑的參數(shù)key
     * @param request 請求
     * @param response 響應(yīng)
     * @return
     */
    public static String getFirstParameterByParamKey(@NotNull String paramKey, HttpServletRequest request, HttpServletResponse response){
        return getIndexParameterByParamKey(paramKey, 0, request, response);
    }



}


作者:IT學習道場


歡迎關(guān)注微信公眾號 : IT學習道場