spring resttemplate url编码 [英] spring resttemplate url encoding
问题描述
我尝试使用spring resttemplate进行简单的休息调用:
I try to do a simple rest call with springs resttemplate:
private void doLogout(String endpointUrl, String sessionId) {
template.getForObject("http://{enpointUrl}?method=logout&session={sessionId}", Object.class,
endpointUrl, sessionId);
}
其中endpointUrl变量包含类似service.host.com/api/service的内容.php
Where the endpointUrl variable contains something like service.host.com/api/service.php
不幸的是,我的调用导致org.springframework.web.client.ResourceAccessException:I / O错误:service.host.com%2Fapi%2Fservice.php
Unfortunately, my call results in a org.springframework.web.client.ResourceAccessException: I/O error: service.host.com%2Fapi%2Fservice.php
因此Spring在创建url之前似乎编码了我的endpointUrl字符串。是否有一种简单的方法可以防止春天这样做?
So spring seems to encode my endpointUrl string before during the creation of the url. Is there a simple way to prevent spring from doing this?
问候
推荐答案
没有简单的方法可以做到这一点。 URI变量通常用于一个路径元素或查询字符串参数。你试图传递多个元素。
There is no easy way to do this. URI variables are usually meant for one path element or a query string parameter. You're trying to pass multiple elements.
一种解决方法是使用 UriTemplate
来生成带有URI变量的URL,然后使用URL-解码并将其传递给 RestTemplate
。
One workaround is to use UriTemplate
to produce the URL with the URI variables as you have them, then URL-decode it and pass that to your RestTemplate
.
String url = "http://{enpointUrl}?method=logout&session={sessionId}";
URI expanded = new UriTemplate(url).expand(endpointUrl, sessionId); // this is what RestTemplate uses
url = URLDecoder.decode(expanded.toString(), "UTF-8"); // java.net class
template.getForObject(url, Object.class);
这篇关于spring resttemplate url编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!