Spring 引导 2:加号 (+) 未在查询参数中编码

Spring boot 2: Plus (+) sign is not getting encoded in query param

目前我正在将我的一项微服务从 spring boot 1.x 迁移到 spring boot 2。考虑有两个服务 A 和 B。服务 A 调用服务 B 的其余端点。在查询 Param 服务 A 正在传递一个字母数字字符串,该字符串还包含 (+) 字符(并不总是因为它是随机生成的字符串)。服务 B 将此字符串与存储在数据库中的字符串和 returns 响应进行比较。

我观察到 1.x URL 版本编码正确。前任。如果我通过 (a+b),它会被编码为 a%2Bb,而在服务 B 中,它会被解码为 (a+b)。但是,对于版本 2.x,它仅被编码为 (a+b),因此在服务 B 中,它被解码为 (a b) [+ 被解码为白色 space]

我正在使用 UriComponentBuilder 构建 URI 和编码 URI 的 encode() 方法。在调试时我发现 URL 中允许使用 + 字符,这就是它没有被编码的​​原因。

我的问题是 - 有没有办法改变这种行为,以便我得到 + 作为 %2B 。或者,如果我做错了什么,请指点我正确的地方。 如果需要,我也可以分享代码。

spring docs and from this issue 到 "invoke encode before and not after expanding URI variables"。例如

.encode()
.buildAndExpand("New York", "foo+bar")

回复评论:

If + character is allowed in URL then why does it get decoded as white space rather than the + character itself

来自w3schools

URLs cannot contain spaces. URL encoding normally replaces a space with a plus (+) sign or with %20.