首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >'+‘(加号)不是使用字符串url的RestTemplate编码,而是解释为’‘(空格)

'+‘(加号)不是使用字符串url的RestTemplate编码,而是解释为’‘(空格)
EN

Stack Overflow用户
提问于 2019-01-22 01:14:52
回答 4查看 9.5K关注 0票数 36

我们正在从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,而不是在执行查询的位置。

或者,有没有办法强制'+‘不被端点解释为’‘?

我已经写了一个简短的例子,演示了一些实现更严格编码的方法,并以注释的形式解释了它们的缺点:

代码语言:javascript
运行
复制
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);
    }
}
EN

Stack Overflow用户

发布于 2020-12-24 17:13:37

谢谢https://stackoverflow.com/users/4466695/gregor-eesmaa,它解决了我的问题。我只是想补充一下,如果你可以在调用RestTemplate之前格式化网址,你可以立即修复网址(而不是在PlusEncoderInterceptor中替换它):

代码语言:javascript
运行
复制
UriComponentsBuilder uriBuilder = UriComponentsBuilder.fromUriString("/search");
uriBuilder.queryParam("beforeTimestamp", "2019-01-21T14:56:50+00:00");
URI uriPlus = uriBuilder.encode().build(false).toUri();

// import org.springframework.util.StringUtils;
String strictlyEscapedQuery = StringUtils.replace(uriPlus.getRawQuery(), "+", "%2B");
URI uri = UriComponentsBuilder.fromUri(uriPlus)
        .replaceQuery(strictlyEscapedQuery)
        .build(true).toUri();

// prints "/search?beforeTimestamp=2019-01-21T14:56:50%2B00:00"
System.out.println(uri);

然后,您可以在RestTemplate调用中使用:

代码语言:javascript
运行
复制
RequestEntity<?> requestEntity = RequestEntity.get(uri).build();
ResponseEntity<String> responseEntity = restTemplate.exchange(requestEntity, String.class);
票数 1
EN
查看全部 4 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54294843

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档