首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何避免RestTemplate将%20代替空白字符到请求查询参数中?

如何避免RestTemplate将%20代替空白字符到请求查询参数中?
EN

Stack Overflow用户
提问于 2021-09-15 08:55:28
回答 1查看 455关注 0票数 3

我正在使用RestTemplate进行一个Spring项目,以便对包含查询参数的URL执行POST请求

这是我的密码:

代码语言:javascript
复制
@Override
public boolean insertNotaryDistrictDetailsAsPost(NotaryDistrictDetails notaryDistrict) {
    
    HttpHeaders basicAuthHeader =  this.getWpBasicAuthenticationHeader();
    HttpEntity<String> request = new HttpEntity<String>(basicAuthHeader);
    
    UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(wpPortalWriteNotaryDistrictDetailsAsPostUrl)
            
            // BASIC INFO:
            .queryParam("title", notaryDistrict.getDistretto())
            .queryParam("wpcf-distretto-notary-district", notaryDistrict.getDistretto())
            .queryParam("wpcf-indirizzo-notary-district", notaryDistrict.getIndirizzo())
            .queryParam("wpcf-denominazione-notary-district", notaryDistrict.getDenominazione())
            .queryParam("wpcf-provincia-notary-district", notaryDistrict.getProvincia())
            .queryParam("wpcf-regione-notary-district", notaryDistrict.getRegione())
            .queryParam("wpcf-cap-notary-district", notaryDistrict.getCap())
            .queryParam("wpcf-telefono-notary-district", notaryDistrict.getTelefono())
            .queryParam("wpcf-fax-notary-district", notaryDistrict.getFax())
            .queryParam("wpcf-email-notary-district", notaryDistrict.getEmail())
            .queryParam("wpcf-pec-notary-district", notaryDistrict.getPec())
            .queryParam("wpcf-web-url-notary-district", notaryDistrict.getWebUrl());
    
            ResponseEntity<String> response = restTemplate.exchange(builder.toUriString(), 
                                                                    HttpMethod.POST, 
                                                                    request, 
                                                                    String.class);
            
            System.out.println("RESPONSE STATUS: " + response.getStatusCodeValue());
            System.out.println(response.getBody());

    return true;
}

问题是生成的URL是这个URL:

代码语言:javascript
复制
https://www.MYURL.it/wp-json/custom-api/notary-district?title=DISTRICT%20NAME%20TEST&wpcf-distretto-notary-district=DISTRICT%20NAME%20TEST&wpcf-indirizzo-notary-district=INDIRIZZO%20TEST&wpcf-denominazione-notary-district=DENOMINAZIONE%20DISTRETTO&wpcf-provincia-notary-district=PROVINCIA%20TEST&wpcf-regione-notary-district=REGIONE%20TEST&wpcf-cap-notary-district=00100&wpcf-telefono-notary-district=3293332232&wpcf-fax-notary-district=23232322&wpcf-email-notary-district=test@test.it&wpcf-pec-notary-district=pec@test.it&wpcf-web-url-notary-district=www.test.it

如您所见,一些查询参数包含用%20编码的空格。这是一个问题,因为当接收到这些字段并从调用的API中保存时,这些字段是用%20而不是空白保存的。我无法更改被调用的API来将其解码为空白,因为它不是由我开发的。

使用Postman,我没有问题:插入包含空白的查询参数的值,它被接收并用空白保存(不是用编码的%20)。

是否存在一种向RestTemplate表示使用空格而不是%20字符的方法?

EN

回答 1

Stack Overflow用户

发布于 2022-02-14 20:56:16

代码语言:javascript
复制
/**************************************** 
 * URL with white spaces
 ****************************************/
String url "https://www.MYURL.it/wp-json/custom-api/notary-district?title=DISTRICT NAME TEST"

// set encoded = false
UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(url);
URI uri = builder.build(false).toUri();

result = restTemplate.exchange( uri, HttpMethod.GET, entity, Class.class);



/**************************************** 
 * URL with %20
 ****************************************/
String url "https://www.MYURL.it/wp-json/custom-api/notary-district?title=DISTRICT%20NAME%20TEST"

// set encoded = true
UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(url);
URI uri = builder.build(true).toUri();

result = restTemplate.exchange( uri, HttpMethod.GET, entity, Class.class);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69189988

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档