我正在使用REST模板有意发送请求uri中的%,类似于/items/a%b

String responseEntity = restTemplate.exchange("/items/a%b",
             requestObj.getHttpMethod(), requestEntity, String.class);
restTemplate正在转换此uri的结尾,并且正在变为/items/a%25b,这是合理的,因为默认情况下其余模板对uri进行了编码。

我尝试使用UriComponent禁用uri的编码
UriComponents uriComponents = UriComponentsBuilder.fromPath("/items/a%b").build();
URI uri= uriComponents.toUri();

String responseEntity = restTemplate.exchange(uri,
             requestObj.getHttpMethod(), requestEntity, String.class);

但这不起作用,因为uri再次是执行编码的URI类型。我确定我没有以正确的方式使用UriComponents。

如果有人能指出禁用编码的正确方法,我将不胜感激。

谢谢。

最佳答案

从UriComponentsBuilder文档中,存在方法build(boolean encoded)

UriComponents uriComponents = UriComponentsBuilder.fromPath("/items/a%b").build(true);

09-04 06:45