当前位置: 首页 > news >正文

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<>();// headersMap<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);// servletPathString servletPath = request.getServletPath();map.put("servletPath", servletPath);// contextPathString contextPath = request.getContextPath();map.put("contextPath", contextPath);// methodString method = request.getMethod();map.put("method", method);// pathInfoString pathInfo = request.getPathInfo();map.put("pathInfo", pathInfo);// requestedSessionIdString requestedSessionId = request.getRequestedSessionId();map.put("requestedSessionId", requestedSessionId);// queryStringString queryString = request.getQueryString();map.put("queryString", queryString);// requestURIString requestURI = request.getRequestURI();map.put("requestURI", requestURI);// requestURLStringBuffer requestURL = request.getRequestURL();map.put("requestURL", requestURL);// protocolString protocol = request.getProtocol();map.put("protocol", protocol);// parameterMapMap<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);// remoteAddrString remoteAddr = request.getRemoteAddr();map.put("remoteAddr", remoteAddr);// remoteHostString remoteHost = request.getRemoteHost();map.put("remoteHost", remoteHost);// remotePortint remotePort = request.getRemotePort();map.put("remotePort", remotePort);// localAddrString localAddr = request.getLocalAddr();map.put("localAddr", localAddr);// localNameString localName = request.getLocalName();map.put("localName", localName);// localPortint 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"
}

相关文章:

  • 【Android】使用intent.putExtra()方法在启动Activity时传递数据
  • 231204 刷题日报
  • 学习使用三个命令实现在腾讯云服务器TencentOS Server 3.1或者CentOS 8上安装ffmpeg
  • WPF绘制进度条(弧形,圆形,异形)
  • 通达OA inc/package/down.php接口存在未授权访问漏洞
  • ?.的用法
  • 如何在Linux环境搭建本地SVN服务器并结合cpolar实现公网访问
  • TCPDUMP抓包明确显示IP地址和端口号
  • Blender学习:荧光树桩
  • Affinity VS PS 2024最新功能详细对比?Affinity Photo与Photoshop比哪家强?
  • Unity中Shader指令优化(编译后指令解析)
  • 算法与数据结构(二十五)TopK问题:基于快排的Python模板
  • 使用正则表达式时-可能会导致性能下降的情况
  • 文字处理工具Word mac软件特点
  • 【LeeCode】438.找到字符串中所有字母异位词
  • JS 中的深拷贝与浅拷贝
  • Angular2开发踩坑系列-生产环境编译
  • ComponentOne 2017 V2版本正式发布
  • JS进阶 - JS 、JS-Web-API与DOM、BOM
  • magento 货币换算
  • Redis在Web项目中的应用与实践
  • tweak 支持第三方库
  • 百度地图API标注+时间轴组件
  • 半理解系列--Promise的进化史
  • 闭包,sync使用细节
  • 大主子表关联的性能优化方法
  • 高性能JavaScript阅读简记(三)
  • 官方解决所有 npm 全局安装权限问题
  • 欢迎参加第二届中国游戏开发者大会
  • 理解 C# 泛型接口中的协变与逆变(抗变)
  • 前端js -- this指向总结。
  • 前端工程化(Gulp、Webpack)-webpack
  • 深入体验bash on windows,在windows上搭建原生的linux开发环境,酷!
  • 算法---两个栈实现一个队列
  • 原创:新手布局福音!微信小程序使用flex的一些基础样式属性(一)
  • 阿里云IoT边缘计算助力企业零改造实现远程运维 ...
  • 京东物流联手山西图灵打造智能供应链,让阅读更有趣 ...
  • 资深实践篇 | 基于Kubernetes 1.61的Kubernetes Scheduler 调度详解 ...
  • #{}和${}的区别?
  • #includecmath
  • #大学#套接字
  • #图像处理
  • (1)SpringCloud 整合Python
  • (day6) 319. 灯泡开关
  • (黑马C++)L06 重载与继承
  • (转)关于多人操作数据的处理策略
  • (转)总结使用Unity 3D优化游戏运行性能的经验
  • *** 2003
  • ./configure,make,make install的作用(转)
  • .Net Web窗口页属性
  • .net 写了一个支持重试、熔断和超时策略的 HttpClient 实例池
  • .NET/C# 反射的的性能数据,以及高性能开发建议(反射获取 Attribute 和反射调用方法)
  • .NET命名规范和开发约定
  • .NET使用HttpClient以multipart/form-data形式post上传文件及其相关参数
  • .NET运行机制