AsyncRestTemplate '#' 查询参数中的符号编码
AsyncRestTemplate '#' sign encoding in query parameter
我正在使用 AsyncRestTemplate
从 Springboot 1.5.2 服务对 Google 地图进行 API 调用。不幸的是,我的一些搜索字符串包含 pound/hashtag 符号 #
并且没有在我的搜索参数中正确编码。我正在使用 exchange
方法。
下面是地址 05406, VT, BURLINGTON, 309 College St #10
的示例:
@Service
public class ExampleAsyncRestTemplate {
private AsyncRestTemplate asyncRestTemplate;
@Autowired
public ExampleAsyncRestTemplate() {
this.asyncRestTemplate = new AsyncRestTemplate();
}
public ListenableFuture<ResponseEntity<T>> getGeoCodedAddress() {
String googleUrl = "https://maps.googleapis.com/maps/api/geocode/json?address=05406, VT, BURLINGTON, 309 College St #10&key=some_key";
Map<String, String> uriVariables = new HashMap<>();
uriVariables.put("address", "05406, VT, BURLINGTON, 309 College St #10");
uriVariables.put("key", "some_key");
return asyncRestTemplate.exchange(googleUrl, HttpMethod.GET, new HttpEntity<>(), GoogleResponse.class, uriVariables);
}
}
结果 URL 被编码为:
https://maps.googleapis.com/maps/api/geocode/json?address=05406,%20VT,%20BURLINGTON,%20309%20College%20St%20#10&key=some_key
请注意,#
仍在地址参数中,而应根据 docs
.
将其编码为 %23
深入调试器,似乎 #
(10&key=some_key
) 之后的字符串被当作 URL 的 fragment
。因此,为什么 #
永远不会被编码。
是否有人能够使用 AsyncRestTemplate 在您的查询参数中提交 #
签名?
我唯一能想到的就是用 number
替换 #
,这确实有效,但感觉 hacky/suboptimal.
感谢您的帮助。
请注意,googleUrl
是一个模板,编码的参数被插入其中。所以您不能提供实际参数作为 url 的一部分。您需要将字符串更改为这样的模板
final String googleUrl = "https://maps.googleapis.com/maps/api/geocode/json?address={address}&key={key}";
这个returns正确的编码:
https://maps.googleapis.com/maps/api/geocode/json?address=05406,%20VT,%20BURLINGTON,%20309%20College%20St%20%2310&key=some_key
我正在使用 AsyncRestTemplate
从 Springboot 1.5.2 服务对 Google 地图进行 API 调用。不幸的是,我的一些搜索字符串包含 pound/hashtag 符号 #
并且没有在我的搜索参数中正确编码。我正在使用 exchange
方法。
下面是地址 05406, VT, BURLINGTON, 309 College St #10
的示例:
@Service
public class ExampleAsyncRestTemplate {
private AsyncRestTemplate asyncRestTemplate;
@Autowired
public ExampleAsyncRestTemplate() {
this.asyncRestTemplate = new AsyncRestTemplate();
}
public ListenableFuture<ResponseEntity<T>> getGeoCodedAddress() {
String googleUrl = "https://maps.googleapis.com/maps/api/geocode/json?address=05406, VT, BURLINGTON, 309 College St #10&key=some_key";
Map<String, String> uriVariables = new HashMap<>();
uriVariables.put("address", "05406, VT, BURLINGTON, 309 College St #10");
uriVariables.put("key", "some_key");
return asyncRestTemplate.exchange(googleUrl, HttpMethod.GET, new HttpEntity<>(), GoogleResponse.class, uriVariables);
}
}
结果 URL 被编码为:
https://maps.googleapis.com/maps/api/geocode/json?address=05406,%20VT,%20BURLINGTON,%20309%20College%20St%20#10&key=some_key
请注意,#
仍在地址参数中,而应根据 docs
.
%23
深入调试器,似乎 #
(10&key=some_key
) 之后的字符串被当作 URL 的 fragment
。因此,为什么 #
永远不会被编码。
是否有人能够使用 AsyncRestTemplate 在您的查询参数中提交 #
签名?
我唯一能想到的就是用 number
替换 #
,这确实有效,但感觉 hacky/suboptimal.
感谢您的帮助。
请注意,googleUrl
是一个模板,编码的参数被插入其中。所以您不能提供实际参数作为 url 的一部分。您需要将字符串更改为这样的模板
final String googleUrl = "https://maps.googleapis.com/maps/api/geocode/json?address={address}&key={key}";
这个returns正确的编码:
https://maps.googleapis.com/maps/api/geocode/json?address=05406,%20VT,%20BURLINGTON,%20309%20College%20St%20%2310&key=some_key