You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Postman无法读取URL末尾数字,Spring接口endDate参数丢失最后一位

问题解决:Spring MVC接口endDate参数丢失最后一位

问题重现

接口代码

@RequestMapping(value = "base/limit/{sysId}/{startDate}/{endDate}?{limitIds}", 
                method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public Object getLimitIds(
        @PathVariable String baseId,
        @PathVariable String startDate, 
        @PathVariable String endDate,
        @RequestParam(required=false) String limitIds )

请求示例

GET:http://localhost:8080/base/limit/fab41439/2022-11-22-02:09:11/2022-12-22-02:09:35?limitIds=23,45,56

问题现象

打印endDate参数时,最后一位数字丢失,实际输出为endDate 2022-12-01-02:09:3,预期值应为2022-12-22-02:09:35,尝试重启服务、clean build、调整DateFormat均无效。


问题根源

  1. 路径模板配置错误:@RequestMapping的value中错误包含了?{limitIds}。在HTTP协议中,?是路径与查询参数的分隔符,不能作为路径模板的一部分。Spring MVC解析路径时会将?视为路径的一部分,导致endDate变量的解析逻辑异常,最终截断参数末尾字符。
  2. 路径变量名不匹配:方法参数中的@PathVariable String baseId与路径模板中的{sysId}名称不一致,会导致参数绑定错误(虽非endDate截断直接原因,但属于潜在问题)。

修复方案

修正@RequestMapping路径配置,移除路径中的?{limitIds},同时对齐路径变量名与模板中的名称:

@RequestMapping(value = "base/limit/{sysId}/{startDate}/{endDate}", 
                method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public Object getLimitIds(
        @PathVariable String sysId, // 修正参数名与路径模板{sysId}匹配
        @PathVariable String startDate, 
        @PathVariable String endDate,
        @RequestParam(required=false) String limitIds )

验证说明

修正后,原请求URL无需修改,Spring MVC会正确解析:

  • fab41439绑定到sysId参数
  • 2022-11-22-02:09:11绑定到startDate参数
  • 2022-12-22-02:09:35完整绑定到endDate参数
  • limitIds=23,45,56作为查询参数正常接收

内容的提问来源于stack exchange,提问作者Nitish K

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.07 10:50:23