【问题标题】:URI removes / from pathURI 从路径中删除 /
【发布时间】:2021-09-30 08:32:55
【问题描述】:

大家早上好。我有以下代码:

String url = "https://example/example/rest/verify/";
<!-- It is absolutely necessary that code starts with / -->
String code = "/gRmaik5E5rUAoJFy7iovg==";

UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url).path(code);
UriComponents components = builder.build(true);
URI uri = components.toUri();
       
ResponseEntity<String> response  = new RestTemplate(requestFactory1).exchange(uri, HttpMethod.GET, null, String.class);

问题是uri变量被赋值给RestTemplate构造函数为https://example/example/rest/verify/gRmaik5E5rUAoJFy7iovg==,也就是说,它从代码变量中移除了初始的/。

我需要 url 看起来像这样:https://example/example/rest/verify//gRmaik5E5rUAoJFy7iovg==,变量代码中有两个 //。

有什么要求吗?

【问题讨论】:

  • 我想你不能修复不能正确处理 url 的服务器,对吧?
  • 没错,我没办法修改它。
  • 您是否尝试将第二个正斜杠发送为%2F 而不是/ 并查看服务器是否接受?
  • 这个问题的解决方案可能值得一看stackoverflow.com/questions/20885521/…
  • 太棒了!如果它工作正常,你说@MatteoNNZ 的方式。

标签: java rest url resttemplate


【解决方案1】:

您可以使用 org.apache.http.client.utils.URIBuilder 来构建 URI。
这会按照您想要的方式构建 URI:

URIBuilder uriBuilder = new URIBuilder(url);
URI uri = uriBuilder.setPath(uriBuilder.getPath() + code).build();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多