首页 > 解决方案 > URI removes / from path

问题描述

Good morning everyone. I have the following code:

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);

The problem is that the uri variable is assigned to the RestTemplate constructor as https://example/example/rest/verify/gRmaik5E5rUAoJFy7iovg==, that is, it removes the initial / from the code variable.

I would need the url to look like this: https://example/example/rest/verify//gRmaik5E5rUAoJFy7iovg== with two // in the variable code.

Any requests?

标签: javaresturlresttemplate

解决方案


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

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

推荐阅读