【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
相关推荐
DCTANT25 分钟前
【原创】国产化适配-全量迁移MySQL数据到OpenGauss数据库
java·数据库·spring boot·mysql·opengauss
Touper.35 分钟前
SpringBoot -- 自动配置原理
java·spring boot·后端
喜欢敲代码的程序员2 小时前
SpringBoot+Mybatis+MySQL+Vue+ElementUI前后端分离版:项目搭建(一)
spring boot·mysql·elementui·vue·mybatis
华子w9089258593 小时前
基于 SpringBoot+Vue.js+ElementUI 的 “花开富贵“ 花园管理系统设计与实现7000字论文
vue.js·spring boot·elementui
小时候的阳光4 小时前
SpringBoot3 spring.factories 自动配置功能不生效?
spring boot·spring·失效·factories·imports
大只鹅5 小时前
Springboot3整合ehcache3缓存--XML配置和编程式配置
spring boot·缓存
执笔诉情殇〆6 小时前
springboot集成达梦数据库,取消MySQL数据库,解决问题和冲突
数据库·spring boot·mysql·达梦
hdsoft_huge6 小时前
Spring Boot 高并发框架实现方案:数字城市的奇妙之旅
java·spring boot·后端
gjh12088 小时前
Easy-excel监听器中对批量上传的工单做错误收集
java·spring boot
考虑考虑9 小时前
使用jpa中的group by返回一个数组对象
spring boot·后端·spring