我尝试使用 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 的内容
不幸的是,我的调用导致 org.springframework.web.client.ResourceAccessException: I/O error: service.host.com%2Fapi%2Fservice.php
所以 spring 似乎在创建 url 期间对我的 endpointUrl 字符串进行了编码。有没有一种简单的方法可以防止 spring 这样做?
Regards
没有简单的方法可以做到这一点。 URI 模板变量通常用于路径元素或查询字符串参数。您正在尝试超越主机。理想情况下,您会找到更好的解决方案来构建 URI。我建议榆次的解决方案 https://stackoverflow.com/a/27550501/438154.
如果您仍然想使用 Spring 实用程序和模板扩展,一种解决方法是使用UriTemplate
使用 URI 变量生成 URL,然后对其进行 URL 解码并将其传递给您的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);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)