RestTemplate.exchange() 不编码“+”?

RestTemplate.exchange() does not encode '+'?

RestTemplate.exchange() 将对 URL 中的所有无效字符进行编码,但不会对 + 中的所有无效字符进行编码,因为 + 是一个有效的 URL 字符。但是如何在任何 URL 的查询参数中传递 + 呢?

如果您传递给 RestTemplate 的 URI 已编码设置为 true,则它不会对您传递的 URI 执行编码,否则它会执行。

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.

传递
  1. builder.build(true).toUri(); 输出:http://example.com/endpoint?param1=abc%2B123%3D 因为编码将被执行一次。
  2. builder.build().toUri(); 输出:http://example.com/endpoint?param1=abc%252B123%253D 因为编码将被执行两次。