【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
相关推荐
来自星星的猫教授25 分钟前
spring,spring boot, spring cloud三者区别
spring boot·spring·spring cloud
乌夷2 小时前
使用spring boot vue 上传mp4转码为dash并播放
vue.js·spring boot·dash
A阳俊yi3 小时前
Spring Boot日志配置
java·spring boot·后端
苹果酱05673 小时前
2020-06-23 暑期学习日更计划(机器学习入门之路(资源汇总)+概率论)
java·vue.js·spring boot·mysql·课程设计
斜月4 小时前
一个服务预约系统该如何设计?
spring boot·后端
Java水解4 小时前
线程池详解:在SpringBoot中的最佳实践
spring boot·后端
阿里小阿希5 小时前
解决 Spring Boot + MyBatis 项目迁移到 PostgreSQL 后的数据类型不匹配问题
spring boot·postgresql·mybatis
码起来呗5 小时前
基于SpringBoot的高校学习讲座预约系统-项目分享
spring boot·后端·学习
广西千灵通网络科技有限公司5 小时前
基于 springboot+vue+elementui 的办公自动化系统设计(
vue.js·spring boot·elementui
bing_1586 小时前
在 Spring Boot 项目中如何使用索引来优化 SQL 查询?
spring boot·sql·索引优化