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"
}
原文地址:https://blog.csdn.net/mouday/article/details/134788293
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.7code.cn/show_42912.html
如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除!
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。