如何在 Spring RestTemplate 中禁用 URL 编码? [英] How to disable URL encoding in Spring RestTemplate?
问题描述
String url = serverUrl + metadata.getUri();
response = restTemplate.exchange(url, metadata.getAction(), requestEntity, metadata.getResponseType());
url 包含字符串
https://localhost/api/fm/info/dump/a\b\f\20170722_225714.jpg?lastModified=1507881481909
https://localhost/api/fm/info/dump/a\b\f\20170722_225714.jpg?lastModified=1507881481909
Spring RestTemplate 向服务器请求时将其编码为以下内容
Spring RestTemplate encodes it to the following when requesting to server
https://localhost/api/fm/info/dump/a%5Cb%5Cf%5C20170722_225714.jpg?lastModified=1507881481909
仅供参考,我需要禁用 URL 编码,以便 '\' 字符在服务器端可用,因为这是业务需求,因为我们的 Web 服务器 (Nginx) 已配置为根据请求的路径执行一些检查包含\"字符.
FYI, I need to disable URL encoding so that the '\' characters are available on server side as it is a business requirement since our web server (Nginx) has been configured to perform some checks based on the path of the request containing '\' character.
推荐答案
创建一个 Configuration
类并添加以下代码:
Create a Configuration
class and add the following code:
@Configuration
public class RestTemplateConfig {
@Bean
public RestTemplate restTemplate() {
DefaultUriBuilderFactory defaultUriBuilderFactory = new DefaultUriBuilderFactory();
defaultUriBuilderFactory.setEncodingMode(DefaultUriBuilderFactory.EncodingMode.NONE);
RestTemplate restTemplate = new RestTemplate();
restTemplate.setUriTemplateHandler(defaultUriBuilderFactory);
return restTemplate;
}
}
然后在任何类中通过构造函数注入自动装配您的RestTemplate
.
这篇关于如何在 Spring RestTemplate 中禁用 URL 编码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!