Java:SpringBoot中HttpServletRequest对象获取客户端的请求参数

文档

  • https://docs.oracle.com/javaee/6/api/javax/servlet/http/HttpServletRequest.html

代码示例

package com.example.demo.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

@RestController
public class IndexController {
    @GetMapping("/getDeviceInfo")
    public Map<String, Object> getDeviceInfo(HttpServletRequest request){
        Map<String, Object> map = new HashMap<>();

        // headers
        Map<String, Object> headers = new HashMap<>();
        Enumeration<String> headerNames = request.getHeaderNames();
        while (headerNames.hasMoreElements()){
            String headerName = headerNames.nextElement();
            String header = request.getHeader(headerName);
            headers.put(headerName, header);
        }

        map.put("headers", headers);

        // servletPath
        String servletPath = request.getServletPath();
        map.put("servletPath", servletPath);

        // contextPath
        String contextPath = request.getContextPath();
        map.put("contextPath", contextPath);

        // method
        String method = request.getMethod();
        map.put("method", method);

        // pathInfo
        String pathInfo = request.getPathInfo();
        map.put("pathInfo", pathInfo);

        // requestedSessionId
        String requestedSessionId = request.getRequestedSessionId();
        map.put("requestedSessionId", requestedSessionId);

        // queryString
        String queryString = request.getQueryString();
        map.put("queryString", queryString);

        // requestURI
        String requestURI = request.getRequestURI();
        map.put("requestURI", requestURI);

        // requestURL
        StringBuffer requestURL = request.getRequestURL();
        map.put("requestURL", requestURL);

        // protocol
        String protocol = request.getProtocol();
        map.put("protocol", protocol);

        // parameterMap
        Map<String, String> parameterMap= new HashMap<>();
        Enumeration<String> parameterNames = request.getParameterNames();
        while (parameterNames.hasMoreElements()){
            String parameterName = parameterNames.nextElement();
            String parameterValue = request.getParameter(parameterName);
            parameterMap.put(parameterName, parameterValue);
        }

        map.put("parameterMap", parameterMap);

        // remoteAddr
        String remoteAddr = request.getRemoteAddr();
        map.put("remoteAddr", remoteAddr);

        // remoteHost
        String remoteHost = request.getRemoteHost();
        map.put("remoteHost", remoteHost);

        // remotePort
        int remotePort = request.getRemotePort();
        map.put("remotePort", remotePort);

        // localAddr
        String localAddr = request.getLocalAddr();
        map.put("localAddr", localAddr);

        // localName
        String localName = request.getLocalName();
        map.put("localName", localName);

        // localPort
        int localPort = request.getLocalPort();
        map.put("localPort", localPort);

        return map;
    }
}

请求地址

http://localhost:8080/getDeviceInfo?name=Tom&age=20

返回数据

{
    "headers": {
        "sec-fetch-mode": "navigate",
        "sec-fetch-site": "none",
        "accept-language": "zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7,ja;q=0.6",
        "cookie": "JSESSIONID=58B59336B8CDA4E92494C79F5E769D1A",
        "sec-fetch-user": "?1",
        "accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7",
        "sec-ch-ua": "\"Chromium\";v=\"116\", \"Not)A;Brand\";v=\"24\", \"Google Chrome\";v=\"116\"",
        "sec-ch-ua-mobile": "?0",
        "sec-ch-ua-platform": "\"macOS\"",
        "host": "localhost:8080",
        "upgrade-insecure-requests": "1",
        "connection": "keep-alive",
        "cache-control": "max-age=0",
        "accept-encoding": "gzip, deflate, br",
        "user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36",
        "sec-fetch-dest": "document"
    },
    "method": "GET",
    "localPort": 8080,
    "remoteHost": "0:0:0:0:0:0:0:1",
    "contextPath": "",
    "remotePort": 55266,
    "requestURI": "/getDeviceInfo",
    "queryString": "name=Tom&age=20",
    "localName": "localhost",
    "protocol": "HTTP/1.1",
    "requestURL": "http://localhost:8080/getDeviceInfo",
    "servletPath": "/getDeviceInfo",
    "requestedSessionId": "58B59336B8CDA4E92494C79F5E769D1A",
    "pathInfo": null,
    "parameterMap": {
        "name": "Tom",
        "age": "20"
    },
    "localAddr": "0:0:0:0:0:0:0:1",
    "remoteAddr": "0:0:0:0:0:0:0:1"
}

你可能感兴趣的:(SpringBoot学习笔记,java,spring,boot,开发语言)