我正在使用RestTemplate进行一个Spring项目,以便对包含查询参数的URL执行POST请求
这是我的密码:
@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:
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字符的方法?
发布于 2022-02-14 20:56:16
/****************************************
* 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);https://stackoverflow.com/questions/69189988
复制相似问题