【JavaEE】SpringMVC获取HTTP中的元素

news/2025/2/27 5:12:46
http://www.w3.org/2000/svg" style="display: none;">

目录

  • 一、获取URL中的参数@PathVariable
  • 二、上传⽂件@RequestPart
  • 三、获取Cookie/Session
    • 3.1 HttpServletRequest和 HttpServletResponse
    • 3.2 获取Cookie
      • 3.2.1 使用HttpServletRequest
      • 3.2.2 使用注解@CookieValue
    • 3.3 设置session
    • 3.4 获取session
      • 3.4.1 使用HttpServletRequest
      • 3.4.2 直接使用HttpSession
      • 3.4.3 使用注解@SessionValue
  • 四、获取Header
    • 4.1 使用HttpServletRequest
    • 4.2 使用注解@RequestHeader

https://i-blog.csdnimg.cn/direct/239777ec59bb4c519dba1cf43590df34.gif#pic_center" alt=" " />

一、获取URL中的参数@PathVariable

path variable: 路径变量。
@PathVariable这个注解可以拿到URL中的参数,使用注意:

  1. 写在方法的参数名前,每个变量都要写。
  2. 当URL中的变量与方法中的变量同名时,可以不在@PathVariable写上URL的变量名。
  3. 当要对拿到的URL变量重命名,要在@PathVariable()括号中写上URL的名字。
  4. URL中的参数必传。

后端代码:
https://i-blog.csdnimg.cn/direct/8d12329b52314f3fb5c3541a3b7ee7da.png" alt=" " />
Postman传参:
https://i-blog.csdnimg.cn/direct/e50f8660a06c4f27a74f91eac76c2549.png" alt=" " />

二、上传⽂件@RequestPart

  1. 上传文件使用MuMultipartFile类下的transferTo方法。
  2. 使用@RequestPart注解可以进行重命名。
  3. 记得抛异常

后端代码:

@RequestMapping("/r2")
    public String getFile(@RequestPart("file11") MultipartFile file) throws IOException {
        String fileName = file.getOriginalFilename();
        file.transferTo(new File("E:/"+ file.getOriginalFilename()));
        return "上传成功"+fileName;
    }

Postman传参:

  • 在Body里面找form-data传File类型。

https://i-blog.csdnimg.cn/direct/0f925dc9af1047e9a4cc9b7ca6020758.png" alt=" " />

三、获取Cookie/Session

3.1 HttpServletRequest和 HttpServletResponse

  1. HttpServletRequest这个类可以拿到HTTP请求中的东西。
  2. HttpServletResponse这个类可以拿到HTTP响应中的东西,还可以进行修改。

https://i-blog.csdnimg.cn/direct/f3346bff63f941578f0d50b6d3d114e2.png" alt=" " />

3.2 获取Cookie

获取Cookie有以下两种方式。

3.2.1 使用HttpServletRequest

只需要调用该类下的getCookies方法即可。

@RequestMapping("r3")
    public String r3 (HttpServletRequest request) {
        Cookie[] cookies = request.getCookies();
        return "获取成功";
    }

Postman发请求:
https://i-blog.csdnimg.cn/direct/f7c272637ed04f31a78a0d663f96c00c.png" alt=" " />

3.2.2 使用注解@CookieValue

在注解中写下Cookie中变量的名,然后后面跟着要赋值的变量类型与名字。
这种方法每次只能获得一个Cookie变量。

@RequestMapping("/r4")
    public String r4(@CookieValue("name") String name) {
        return name;
    }

Postman发请求:
https://i-blog.csdnimg.cn/direct/675b8c248f3647368a4d185cf6657119.png" alt=" " />

3.3 设置session

使用HttpServletRequest下的getSession方法,拿到sessionId,拿到session。

 HttpSession getSession(boolean create);
  1. 默认为true,如果没有拿到session,返回一个空HttpSession对象。
  2. 如果为false,没有拿到session,返回一个null。

代码:

@RequestMapping("/r5")
    public String setSession(HttpServletRequest request) {
        HttpSession session = request.getSession();
        session.setAttribute("name","lisi");
        session.setAttribute("age","88");
        return "设置成功";
    }

3.4 获取session

获取session有以下三种方式。

3.4.1 使用HttpServletRequest

使用HttpServletRequest类下的getSession方法拿到session,
再通过HttpSession 类下的方法getAttribute获取session中的对象 Object getAttribute(String name);

代码:

@RequestMapping("/r6")
    public String r5(HttpServletRequest request) {
        HttpSession session = request.getSession(false);
        if (null == session) {
            return "用户未登录";
        } else {
            return "用户名:"+(String) session.getAttribute("name") +
                    "用户年龄"+(String) session.getAttribute("age");
        }
    }

Postman传参:
https://i-blog.csdnimg.cn/direct/724c8508dd964d9e8428cbafe67f251e.png" alt=" " />

3.4.2 直接使用HttpSession

相当于HttpSession session = request.getSession();直接在方法参数完成。

@RequestMapping("/r8")
    public String r5(HttpSession session ) {
        if (null == session) {
            return "用户未登录";
        } else {
            return "用户名:"+(String) session.getAttribute("name") +
                    "用户年龄"+(String) session.getAttribute("age");
        }
    }

3.4.3 使用注解@SessionValue

注解中value代表:session中的对象, required 调配没拿到session时返回什么,true返回空HttpSession对象,false返回null。
代码:

@RequestMapping("r7")
    public String r6(@SessionAttribute(value = "name",required = false) String name) {
        System.out.println(name);
        return  name;
    }

Postman传参:
https://i-blog.csdnimg.cn/direct/51f3f96ab83246c0a5bdf7b7c8bc61ac.png" alt=" " />

四、获取Header

获取session有以下两种方式。

4.1 使用HttpServletRequest

使用HttpServletRequest类下的getHeader方法,方法中填入想要的header中的参数名称,拿到header下的参数。

代码:

@RequestMapping("/r9")
    public String getHeader(HttpServletRequest request) {
        String header = request.getHeader("user-agent");
        return "获取到 "+header;

    }

Postman传参:
https://i-blog.csdnimg.cn/direct/e675a3abddf84a348de26c50f4a52b93.png" alt=" " />

4.2 使用注解@RequestHeader

在注解中写下Header中参数的名,然后后面跟着要赋值给的变量类型与名字。

代码:

@RequestMapping("/r10")
    public String getHeader2(@RequestHeader("user-agent") String header) {
        return "获取到 "+header;
    }

Postman传参:

https://i-blog.csdnimg.cn/direct/64bb96dc2c3a4b74be60e1a1deae02f7.png" alt=" " />


http://www.niftyadmin.cn/n/5869465.html

相关文章

如何使用Spark Streaming将数据写入HBase

在Spark Streaming中将数据写入HBase涉及到几个步骤。以下是一个基本的指南,帮助你理解如何使用Spark Streaming将数据写入HBase。 1. 环境准备 HBase:确保HBase集群已经安装并运行。Spark:确保Spark已经安装,并且Spark版本与HB…

APNG格式图片文件大小优化方案 转WEBP

文章目录 原因过程相关下载相关文档后记 原因 页面上有个特效动画,PNG文件,APNG格式,13M大小,太占用内容了,要优化一下。 过程 直接上命令吧 ffmpeg -i input.apng -vf "formatrgba" -loop 0 output.web…

1.2部署可视化工具es head:9100

ElasticSearch Head是集群管理、数据可视化、增删查改、查询语句可视化工具 1.下载插件 插件下载地址: https://github.com/mobz/elasticsearch-head node下载地址: wget https://registry.npmmirror.com/-/binary/node/latest-v14.x/node-v14.19.3-linux-x64.tar.gz 2.安装插…

问道1.63单机版安装教程+虚拟机一键端+GM

今天为大家带来一款怀旧网单《问道1.63》的游戏架设,适用于单机娱乐, 仅供怀旧,本人已经安装游戏成功,特此带来详细安装教程。 适用环境 单机 视频演示 https://githubs.xyz/show/329.mp4 亲测截图 架设步骤 虚拟机准备 首先…

第十八:路由传参 query

第一种方法&#xff1a;传递参数&#xff1a; <RouterLink to "/news/detail?idnaaa&titlebbbb&contentccccccc">{{ news.title }}</RouterLink> 上面的是不对的&#xff0c;如果的 模板字符串里面 嵌入 js 那么应该如下所示&#xff1a; …

无人机实战系列(三)本地摄像头+远程GPU转换深度图

这篇文章将结合之前写的两篇文章 无人机实战系列&#xff08;一&#xff09;在局域网内传输数据 和 无人机实战系列&#xff08;二&#xff09;本地摄像头 Depth-Anything V2 实现了以下功能&#xff1a; 本地笔记本摄像头发布图像 远程GPU实时处理&#xff08;无回传&#…

labview实现有符号位16进制转二进制补码转真值

今天在用一个采集模块时&#xff0c;发现读出寄存器的数据是不同的&#xff0c;它有两种范围&#xff0c;一个时十六进制整型&#xff0c;一种是有符号位十六进制&#xff0c;对应的量程和范围也是不同的&#xff0c;针对之前读取温度没有出现负数的情况&#xff0c;应该是转成…

批量导出数据库表到Excel

这篇文章将介绍如何批量的将多个甚至成千上万的数据库表导出为Excel文件。 准备数据 如下图是数据库里的表&#xff0c;我们需要将它们全部导出为excel文件&#xff0c;这里以SQL Server数据库为例 新增导出 打开的卢导表工具&#xff0c;新建数据库连接&#xff0c;这里以S…