How to disable URL encoding in Spring RestTemplate?

允我心安 提交于 2021-01-28 05:59:55

问题


String url = serverUrl + metadata.getUri();
response = restTemplate.exchange(url, metadata.getAction(), requestEntity, metadata.getResponseType());

url contains the string

https://localhost/api/fm/info/dump/a\b\f\20170722_225714.jpg?lastModified=1507881481909

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

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.


回答1:


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

Then Autowire your RestTemplate in any class Through Constructor injection.



来源:https://stackoverflow.com/questions/47422472/how-to-disable-url-encoding-in-spring-resttemplate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!