Obtain the front-end request method name, parameters, path and other information according to ServletRequestAttributes

Obtain the method name, parameters, path and other information according to the front-end request

// If you want to get the request information in a method, you need to get the request from the request context, or use HttpServletRequest request in the parameter list

① Get the request context in the method

// Receive the request and record the request content
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes ();

② Get request
     //接收到request
    HttpServletRequest request = attributes.getRequest();
③ Get parameters
    Map<String,Object> map = getParameterMap(request);
    System.out.println("我是从Map参数获取的:"+Arrays.asList(map));

Focus: getParameterMap method

 /**
     * 获取所有请求参数,封装为map对象
     *
     * @return
     */
    public Map<String, Object> getParameterMap(HttpServletRequest request) {
        if (request == null) {
            return null;
        }
        Enumeration<String> enumeration = request.getParameterNames();
        Map<String, Object> parameterMap = new HashMap<String, Object>();
        StringBuilder stringBuilder = new StringBuilder();
        while (enumeration.hasMoreElements()) {
            String key = enumeration.nextElement();
            String value = request.getParameter(key);
            String keyValue = key + " : " + value + " ; ";
            stringBuilder.append(keyValue);
            parameterMap.put(key, value);
        }
        return parameterMap;
    }

So you can get all the information

Published 67 original articles · Liked12 · Visitors 10,000+

Guess you like

Origin blog.csdn.net/m0_37635053/article/details/103969075