【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
相关推荐
enjoy嚣士6 小时前
springboot 之 HTML与图片生成 (2)
spring boot·html转图片
白初&7 小时前
SpringBoot后端基础案例
java·spring boot·后端
再睡亿分钟!8 小时前
Spring MVC 的常用注解
java·开发语言·spring boot·spring
麦兜*9 小时前
MongoDB 常见错误解决方案:从连接失败到主从同步问题
java·数据库·spring boot·redis·mongodb·容器
爱吃烤鸡翅的酸菜鱼13 小时前
【Spring】原理解析:Spring Boot 自动配置
java·spring boot
十八旬13 小时前
苍穹外卖项目实战(day7-1)-缓存菜品和缓存套餐功能-记录实战教程、问题的解决方法以及完整代码
java·数据库·spring boot·redis·缓存·spring cache
郑洁文14 小时前
基于SpringBoot的天气预报系统的设计与实现
java·spring boot·后端·毕设
optimistic_chen14 小时前
【Java EE进阶 --- SpringBoot】Spring DI详解
spring boot·笔记·后端·spring·java-ee·mvc·di
中国胖子风清扬15 小时前
Rust 日志库完全指南:从入门到精通
spring boot·后端·rust·学习方法·logback
xiaogg367815 小时前
springboot rabbitmq 延时队列消息确认收货订单已完成
spring boot·rabbitmq·java-rabbitmq