我尝试使用 springs resttemplate 进行简单的 rest 调用:
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 这样做?
问候
原文由 user1145874 发布,翻译遵循 CC BY-SA 4.0 许可协议
没有简单的方法可以做到这一点。 URI 模板变量通常用于路径元素或查询字符串参数。您正在尝试通过主机。理想情况下,您会找到构建 URI 的更好解决方案。我建议 榆次的解决方案。
如果您仍想使用 Spring 实用程序和模板扩展,一种解决方法是使用
UriTemplate
生成带有 URI 变量的 URL,然后对其进行 URL 解码并将其传递给您的RestTemplate
。