Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java '+';(加号)未使用字符串url使用RestTemplate编码,但解释为'';(空间)_Java_Spring_Spring Boot_Timestamp_Rfc3986 - Fatal编程技术网

Java '+';(加号)未使用字符串url使用RestTemplate编码,但解释为'';(空间)

Java '+';(加号)未使用字符串url使用RestTemplate编码,但解释为'';(空间),java,spring,spring-boot,timestamp,rfc3986,Java,Spring,Spring Boot,Timestamp,Rfc3986,我们正在从Java8迁移到Java11,从而从SpringBoot1.5.6迁移到2.1.2。我们注意到,在使用RestTemplate时,“+”符号不再编码为“%2B”(SPR-14828更改)。这没关系,因为RFC3986没有将“+”列为保留字符,但在Spring引导端点中接收时,它仍然被解释为“”(空格) 我们有一个搜索查询,它可以将可选的时间戳作为查询参数。查询看起来像http://example.com/search?beforeTimestamp=2019-01-21T14:56:5

我们正在从Java8迁移到Java11,从而从SpringBoot1.5.6迁移到2.1.2。我们注意到,在使用RestTemplate时,“+”符号不再编码为“%2B”(SPR-14828更改)。这没关系,因为RFC3986没有将“+”列为保留字符,但在Spring引导端点中接收时,它仍然被解释为“”(空格)

我们有一个搜索查询,它可以将可选的时间戳作为查询参数。查询看起来像
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);
    }
}
package com.example.clientandserver;
导入org.springframework.boot.CommandLineRunner;
导入org.springframework.boot.SpringApplication;
导入org.springframework.boot.autoconfigure.springboot应用程序;
导入org.springframework.boot.web.client.RestTemplateBuilder;
导入org.springframework.web.bind.annotation.GetMapping;
导入org.springframework.web.bind.annotation.RequestParam;
导入org.springframework.web.bind.annotation.RestController;
导入org.springframework.web.client.rest模板;
导入org.springframework.web.util.DefaultUriBuilderFactory;
导入org.springframework.web.util.UriComponentsBuilder;
导入org.springframework.web.util.UriUtils;
导入java.nio.charset.StandardCharset;
导入java.util.HashMap;
导入java.util.Map;
@SpringBoot应用程序
@RestController
公共类ClientAndServerApp实现CommandLineRunner{
公共静态void main(字符串[]args){
run(ClientAndServerApp.class,args);
}
@凌驾
公共无效运行(字符串…参数){
字符串beforeTimestamp=“2019-01-21T14:56:50+00:00”;
//Previous-基本url和原始参数(自动编码)。
//这在早期版本的Spring Boot中起作用
{
RestTemplate RestTemplate=新的RestTemplateBuilder()
.rootUri(“http://localhost:8080build();
UriComponentsBuilder b=UriComponentsBuilder.fromPath(“/search”);
if(在时间戳之前!=null){
b、 queryParam(“BeforeTimstamp”,BeforeTimstamp);
}
getForEntity(b.toUriString(),Object.class);
//收到日期:2019-01-21T14:56:50 00:00
//这里缺少加号^
}
//选项1-没有基本url,自己对参数进行编码。
{
RestTemplate RestTemplate=新RestTemplate();
UriComponentsBuilder b=UriComponentsBuilder
.fromHttpUrl(“http://localhost:8080/search");
if(在时间戳之前!=null){
b、 槲寄生(
“在时间戳之前”,
UriUtils.encode(在时间戳之前,StandardCharsets.UTF_8)
);
}
restTemplate.getForEntity(
b、 build(true).toUri(),Object.class
).getBody();
//收到日期:2019-01-21T14:56:50+00:00
}
//选项2-对于模板化的基本url,查询参数不是可选的。
{
RestTemplate RestTemplate=新的RestTemplateBuilder()
.rootUri(“http://localhost:8080")
.uriTemplateHandler(新的DefaultUriBuilderFactory())
.build();
Map params=新的HashMap();
参数put(“beforeTimestamp”,beforeTimestamp);
restTemplate.getForEntity(
“/search?beforeTimestamp={beforeTimestamp}”,
Object.class,
参数);
//收到日期:2019-01-21T14:56:50+00:00
}
}
@GetMapping(“/search”)
公共无效搜索(@RequestParam String beforeTimestamp){
System.out.println(“接收:“+在时间戳之前”);
}
}

我们意识到,编码完成后,可以在拦截器中修改URL。因此,一个解决方案是使用拦截器,对查询参数中的加号进行编码

RestTemplate restTemplate = new RestTemplateBuilder()
        .rootUri("http://localhost:8080")
        .interceptors(new PlusEncoderInterceptor())
        .build();
一个简短的例子:

public class PlusEncoderInterceptor implements ClientHttpRequestInterceptor {

    @Override
    public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
        return execution.execute(new HttpRequestWrapper(request) {
            @Override
            public URI getURI() {
                URI u = super.getURI();
                String strictlyEscapedQuery = StringUtils.replace(u.getRawQuery(), "+", "%2B");
                return UriComponentsBuilder.fromUri(u)
                        .replaceQuery(strictlyEscapedQuery)
                        .build(true).toUri();
            }
        }, body);
    }
}

这里也讨论了这个问题

一个更简单的解决方案是将URI生成器上的编码模式设置为仅值\ u

    DefaultUriBuilderFactory builderFactory = new DefaultUriBuilderFactory();
    builderFactory.setEncodingMode(DefaultUriBuilderFactory.EncodingMode.VALUES_ONLY);
    RestTemplate restTemplate = new RestTemplateBuilder()
            .rootUri("http://localhost:8080")
            .uriTemplateHandler(builderFactory)
            .build();
这与使用查询参数时使用PluseCodingInterceptor获得的结果相同。

谢谢,它解决了我的问题。只是想添加一点,以防在调用
restemplate
之前格式化URL,您可以立即修复URL(而不是在
PlusEncoderInterceptor
中替换它):

UriComponentsBuilder uriBuilder=UriComponentsBuilder.fromUristering(“/search”);
uriBuilder.queryParam(“时间戳之前”、“2019-01-21T14:56:50+00:00”);
URI uriPlus=uriBuilder.encode().build(false.toUri();
//导入org.springframework.util.StringUtils;
strictlyEscapedQuery=StringUtils.replace(uriPlus.getRawQuery(),“+”,“%2B”);
URI=UriComponentsBuilder.fromUri(uriPlus)
.replaceQuery(StriclyEscapedQuery)
.build(true.toUri();
//打印“/搜索?时间戳之前=2019-01-21T14:56:50%2B00:00”
System.out.println(uri);
然后您可以在
RestTemplate
调用中使用:

RequestEntity requestEntit