【Bug】Spring项目中Path参数以;分割导致接收参数不正确

最近在代码中遇到了一个bug,写的一个接口用swagger测试是没有问题的,但是在部署上线后,用户反馈只能接收到批量操作的第一条的数据。

接口功能很简单,用户要进行批量操作,前端把所选记录的id用;隔开,拼接为一个String,后端接收到后,使用split函数分割进行处理。

问题原因:;分隔符会转义为%3B,前端通过代理转发时可能将03B又转为了;,导致参数到后端时已经不是原来的值了

解决方案:前端在传递参数时以/path/AAA%3BCCC的格式,这样后端收到的结果就是AAA;CCC或者使用其他符号

代码复现:

  1. 新建SpringBoot项目,只要web和lombok依赖(非必须,用来打印日志,直接使用System.out.println()也行)
xml 复制代码
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.10</version>
        </dependency>
  1. 写一个Controller和一个接口,接口参数在Path上
java 复制代码
package com.zwj.springbootpath.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/path")
@Slf4j
public class PathController {

    @GetMapping("/test/{strs}")
    public void testPath(@PathVariable("strs") String strs) {
        log.info("接收到的strs值为:{}",strs);
        System.out.println();
    }

}
  1. 运行项目,测试结果,或者直接在浏览器测试
bash 复制代码
curl http://127.0.0.1:8080/path/test/111;123
  1. 测试另一种方式
bash 复制代码
curl http://127.0.0.1:8080/path/test/111%3B123
相关推荐
b***666141 分钟前
Spring Boot 整合 Apollo 配置中心实战
java·spring boot·后端
b***66611 小时前
前端的dist包放到后端springboot项目下一起打包
前端·spring boot·后端
i***11861 小时前
springboot使用redis
spring boot·redis·后端
毕设源码-郭学长1 小时前
【开题答辩全过程】以 高校兼职系统为例,包含答辩的问题和答案
java·spring boot
h***38181 小时前
SpringBoot - Cookie & Session 用户登录及登录状态保持功能实现
java·spring boot·后端
十五喵1 小时前
智慧物业|物业管理|基于SprinBoot+vue的智慧物业管理系统(源码+数据库+文档)
java·前端·数据库·vue.js·spring boot·毕设·智慧物业管理系统
k***81722 小时前
IDEA搭建SpringBoot,MyBatis,Mysql工程项目
spring boot·intellij-idea·mybatis
j***48542 小时前
idea创建SpringBoot自动创建Lombok无效果(解决)
spring boot·后端·intellij-idea
b***9102 小时前
idea创建springBoot的五种方式
java·spring boot·intellij-idea
tgethe2 小时前
MybatisPlus基础部分详解(中篇)
java·spring boot·mybatisplus