文档

代码示例

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&gt; getDeviceInfo(HttpServletRequest request){
        Map<String, Object&gt; map = new HashMap<&gt;();

        // 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&amp;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&amp;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"
}

原文地址:https://blog.csdn.net/mouday/article/details/134788293

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任

如若转载,请注明出处:http://www.7code.cn/show_42912.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注