RestTemplate.exchange()不编码“ +”吗? [英] RestTemplate.exchange() does not encode '+'?
问题描述
RestTemplate.exchange()
将URL中的所有无效字符编码,但将 +
编码为 +
是有效的URL字符。但是如何在任何URL的查询参数中传递 +
?
The RestTemplate.exchange()
will encode all the invalid characters in URL but not +
as +
is a valid URL character. But how to pass a +
in any URL's query parameter?
推荐答案
如果您传递给RestTemplate的URI的编码设置为true,那么它将不会对您传递的URI执行编码,否则它将执行编码。
If the URI you pass to the RestTemplate has encoded set as true then it will not perform the encoding on the URI you pass else it will do.
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URLEncoder;
import java.util.Collections;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.BufferingClientHttpRequestFactory;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;
class Scratch {
public static void main(String[] args) {
RestTemplate rest = new RestTemplate(
new BufferingClientHttpRequestFactory(new SimpleClientHttpRequestFactory()));
HttpHeaders headers = new HttpHeaders();
headers.add("Content-Type", "application/json");
headers.add("Accept", "application/json");
HttpEntity<String> requestEntity = new HttpEntity<>(headers);
UriComponentsBuilder builder = null;
try {
builder = UriComponentsBuilder.fromUriString("http://example.com/endpoint")
.queryParam("param1", URLEncoder.encode("abc+123=", "UTF-8"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
URI uri = builder.build(true).toUri();
ResponseEntity responseEntity = rest.exchange(uri, HttpMethod.GET, requestEntity, String.class);
}
}
因此,如果您需要通过查询参数中带有 +
的参数,那么RestTemplate不会将 +
编码,而将其他所有无效的URL字符编码为 +
是有效的URL字符。因此,您必须首先对参数进行编码( URLEncoder.encode( abc + 123 =, UTF-8)
),然后将编码后的参数传递给RestTemplate,以表明URI已经使用 builder.build(true).toUri();
进行了编码,其中 true
告诉RestTemplate: URI经过严格的编码,因此不再进行编码,因此 +
将作为%2B
传递。
So if you need to pass a query param with +
in it then the RestTemplate will not encode the +
but every other invalid URL character as +
is a valid URL character. Hence you have to first encode the param (URLEncoder.encode("abc+123=", "UTF-8")
) and then pass the encoded param to RestTemplate stating that the URI is already encoded using builder.build(true).toUri();
, where true
tells the RestTemplate that the URI is alrady encoded so not to encode again and hence the +
will be passed as %2B
.
- 使用
builder.build(true).toUri();
OUTPUT: http://example.com/endpoint?param1=abc%2B123%3D 作为编码将执行一次。 - 使用
builder.build()。toUri();
输出: http://example.com/endpoint?param1= abc%252B123%253D ,因为编码将进行两次。
- With
builder.build(true).toUri();
OUTPUT : http://example.com/endpoint?param1=abc%2B123%3D as the encoding will be perfromed once. - With
builder.build().toUri();
OUTPUT : http://example.com/endpoint?param1=abc%252B123%253D as the encoding will be perfromed twice.
这篇关于RestTemplate.exchange()不编码“ +”吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!