首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >查询参数中的AsyncRestTemplate '#‘符号编码

查询参数中的AsyncRestTemplate '#‘符号编码
EN

Stack Overflow用户
提问于 2019-03-28 20:08:16
回答 1查看 74关注 0票数 0

我正在使用AsyncRestTemplate从Springboot1.5.2服务对Google Maps进行API调用。不幸的是,我的一些搜索字符串包含井号/标签符号#,并且在我的搜索参数中没有正确编码。我使用的是exchange方法。

以下是address 05406, VT, BURLINGTON, 309 College St #10的示例

代码语言:javascript
复制
@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编码为:

代码语言:javascript
复制
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替换#,这实际上是可行的,但感觉有点老套/不太理想。

谢谢你的帮助。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-03-28 21:22:19

请注意,googleUrl是一个模板,其中插入了编码的参数。因此,您不能将实际参数作为url的一部分提供。您需要将字符串更改为如下所示的模板

代码语言:javascript
复制
final String googleUrl = "https://maps.googleapis.com/maps/api/geocode/json?address={address}&key={key}";

这将返回正确的编码:

代码语言:javascript
复制
https://maps.googleapis.com/maps/api/geocode/json?address=05406,%20VT,%20BURLINGTON,%20309%20College%20St%20%2310&key=some_key
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55397283

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档