【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
相关推荐
Q_Q19632884753 小时前
python的电影院座位管理可视化数据分析系统
开发语言·spring boot·python·django·flask·node.js·php
诗人不说梦^4 小时前
[RCTF2015]EasySQL
web·ctf
悟纤6 小时前
Spring Boot 实用小技巧:多级缓存(Caffeine + Redis)- 第545篇
spring boot·后端·spring
MrSYJ7 小时前
UserDetailService是在什么环节生效的,为什么自定义之后就能被识别
java·spring boot·后端
Noii.9 小时前
Spring Boot初级概念及自动配置原理
java·spring boot·后端
勿在浮沙筑高台9 小时前
无法获取实体类com.example.springdemo2.entity.po.UserPO对应的表名!
java·spring boot·mybatis
掉头发的王富贵10 小时前
ShardingSphere-JDBC入门教程(上篇)
spring boot·后端·mysql
中草药z12 小时前
【自动化测试】Selenium详解-WebUI自动化测试
前端·功能测试·selenium·自动化·html·web·测试
杨DaB1 天前
【SpringBoot】Swagger 接口工具
java·spring boot·后端·restful·swagger
昵称为空C1 天前
SpringBoot接口限流的常用方案
服务器·spring boot