我们正在从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.如果我们要对参数self(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";
// …Run Code Online (Sandbox Code Playgroud)