'+'(加号)不是使用字符串 url 使用 RestTemplate 编码,而是解释为 ' ' (空格)
我们正在从 Java 8 迁移到 Java 11,因此,从 Spring Boot 1.5.6 迁移到 2.1.2。我们注意到,当使用 RestTemplate 时,'+' 符号不再编码为 '%2B'(由 SPR-14828 更改)。这没关系,因为 RFC3986 没有将 '+' 列为保留字符,但是当在 Spring Boot 终结点中接收到它时,它仍然被解释为 ''(空格)。
我们有一个搜索查询,它可以将可选的时间戳作为查询参数。查询类似于 。http://example.com/search?beforeTimestamp=2019-01-21T14:56:50%2B00:00
我们无法弄清楚如何在不进行双重编码的情况下发送编码的加号。查询参数将被解释为 。如果我们自己对参数进行编码(),那么它将被接收并解释为。2019-01-21T14:56:50+00:00
2019-01-21T14:56:50 00:00
2019-01-21T14:56:50%2B00:00
2019-01-21T14:56:50%252B00:00
另一个约束是,在设置 restTemplate 时,我们希望将基本 url 设置在其他地方,而不是在执行查询的位置。
或者,有没有办法强制“+”不被端点解释为''?
我写了一个简短的示例,演示了实现更严格编码的一些方法,其缺点解释为注释:
package com.example.clientandserver;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.DefaultUriBuilderFactory;
import org.springframework.web.util.UriComponentsBuilder;
import org.springframework.web.util.UriUtils;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
@SpringBootApplication
@RestController
public class ClientAndServerApp implements CommandLineRunner {
public static void main(String[] args) {
SpringApplication.run(ClientAndServerApp.class, args);
}
@Override
public void run(String... args) {
String beforeTimestamp = "2019-01-21T14:56:50+00:00";
// Previously - base url and raw params (encoded automatically).
// This worked in the earlier version of Spring Boot
{
RestTemplate restTemplate = new RestTemplateBuilder()
.rootUri("http://localhost:8080").build();
UriComponentsBuilder b = UriComponentsBuilder.fromPath("/search");
if (beforeTimestamp != null) {
b.queryParam("beforeTimestamp", beforeTimestamp);
}
restTemplate.getForEntity(b.toUriString(), Object.class);
// Received: 2019-01-21T14:56:50 00:00
// Plus sign missing here ^
}
// Option 1 - no base url and encoding the param ourselves.
{
RestTemplate restTemplate = new RestTemplate();
UriComponentsBuilder b = UriComponentsBuilder
.fromHttpUrl("http://localhost:8080/search");
if (beforeTimestamp != null) {
b.queryParam(
"beforeTimestamp",
UriUtils.encode(beforeTimestamp, StandardCharsets.UTF_8)
);
}
restTemplate.getForEntity(
b.build(true).toUri(), Object.class
).getBody();
// Received: 2019-01-21T14:56:50+00:00
}
// Option 2 - with templated base url, query parameter is not optional.
{
RestTemplate restTemplate = new RestTemplateBuilder()
.rootUri("http://localhost:8080")
.uriTemplateHandler(new DefaultUriBuilderFactory())
.build();
Map<String, String> params = new HashMap<>();
params.put("beforeTimestamp", beforeTimestamp);
restTemplate.getForEntity(
"/search?beforeTimestamp={beforeTimestamp}",
Object.class,
params);
// Received: 2019-01-21T14:56:50+00:00
}
}
@GetMapping("/search")
public void search(@RequestParam String beforeTimestamp) {
System.out.println("Received: " + beforeTimestamp);
}
}