Springboot:通过 Resttemplate 防止 % 的双重编码 [英] Springboot : Prevent double encoding of % by Resttemplate

查看:35
本文介绍了Springboot:通过 Resttemplate 防止 % 的双重编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们的代码使用 Asyncresttemplate 如下

Our code uses Asyncresttemplate as follows

String uri = http://api.host.com/version/test?address=%23&language=en-US&format=json

getAysncRestTemplate().getForEntity(uri, String.class);

但是 %23 在 Rest 模板中被双​​重编码为​​ %2523 并且 url 变成http://api.host.com/version/test?address=%2523&language=en-US&format=json,但是我需要传递编码字符串,如果我传递解码数据'#'

But %23 is double encoded in Rest template as %2523 and the url becomes http://api.host.com/version/test?address=%2523&language=en-US&format=json, But I need to pass encoded string, It doesn't encode if I pass decoded data '#'

如何在不对 URL 进行双重编码的情况下发送此请求?

How can I send this request without double encoding the URL?

已经尝试使用 UriComponentsBuilder避免使用 Spring 的 RestTemplate 对 URL 查询参数进行双重编码

Already tried using UriComponentsBuilder Avoid Double Encoding of URL query param with Spring's RestTemplate

推荐答案

传递给 RestTemplateuri 参数(String 类型)根据 JavaDoc,实际上是一个 URI 模板.没有双重编码的rest模板的使用方法如下:

The uri argument (of type String) passed to the RestTemplate is actually a URI template as per the JavaDoc. The way to use the rest template without the double encoding would be as follows:

getAysncRestTemplate().getForEntity(
        "http://api.host.com/version/test?address={address}&language=en-US&format=json", 
        String.class, 
        "#"); // (%23 decoded)

如果你知道你已经有一个正确编码的 URL,你可以使用具有 URI 作为第一个参数的方法:

If you know you already have a properly encoded URL you can use the method that has a URI as the first parameter instead:

restTemplate.getForEntity(
        new URI("http://api.host.com/version/test?address=%23&language=en-US&format=json"),
        String.class);

这篇关于Springboot:通过 Resttemplate 防止 % 的双重编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆