我正在使用AsyncRestTemplate从Springboot1.5.2服务对Google Maps进行API调用。不幸的是,我的一些搜索字符串包含井号/标签符号#,并且在我的搜索参数中没有正确编码。我使用的是exchange方法。
以下是address 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请注意,#仍在address参数中,但应按照docs将其编码为%23。
深入调试器,似乎# (10&key=some_key)之后的字符串被用作fragment。这就是为什么#从不编码的原因。
有没有人能够使用AsyncRestTemplate在您的查询参数中提交#签名?
我唯一能想到的就是用number替换#,这实际上是可行的,但感觉有点老套/不太理想。
谢谢你的帮助。
发布于 2019-03-28 21:22:19
请注意,googleUrl是一个模板,其中插入了编码的参数。因此,您不能将实际参数作为url的一部分提供。您需要将字符串更改为如下所示的模板
final String googleUrl = "https://maps.googleapis.com/maps/api/geocode/json?address={address}&key={key}";这将返回正确的编码:
https://maps.googleapis.com/maps/api/geocode/json?address=05406,%20VT,%20BURLINGTON,%20309%20College%20St%20%2310&key=some_keyhttps://stackoverflow.com/questions/55397283
复制相似问题