【发布时间】:2017-11-10 07:24:46
【问题描述】:
我很难理解UriComponentsBuilder 的行为。我想用它来编码查询参数中的 URL,但它似乎只转义 % 字符,而不是其他必要的字符,例如 &。
查询参数中根本没有编码的 URL 示例:
UriComponentsBuilder.fromUri("http://example.com/endpoint")
.queryParam("query", "/path?foo=foo&bar=bar")
.build();
输出:http://example.com/endpoint?query=/path?foo=foo&bar=bar
这是不正确的,因为未编码的& 导致bar=bar 被解释为/endpoint 的查询参数,而不是/path。
但是,如果我使用包含% 字符的输入::
UriComponentsBuilder.fromUri("http://example.com/endpoint")
.queryParam("query", "/path?foo=%20bar")
.build();
输出:http://example.com/endpoint?query=/path?foo=%2520bar
% 字符被转义。
UriComponentsBuilder 会自动转义 % 字符而不是其他保留字符似乎不一致。
使用UriComponentsBuilder 将 URL 编码为查询参数的正确过程是什么?
【问题讨论】:
-
但是,据我了解,
&是 URL 中的有效字符。除某些字符外,其他所有内容都必须进行 url 编码。 -
您找到解决方案了吗?我也遇到了类似的问题
标签: java spring url-encoding query-parameters