首页 > 解决方案 > 为什么 RestTemplate 不对“+”符号进行 urlencode 而是对其他所有内容进行 urlencode?

问题描述

我发现了一个奇怪的问题,即 urlencoding 的行为不一致。

更新

Spring MVC 版本 4.3 和 5.1 之间存在差异:

// FAIL in MVC 4.x 
@Test
public void test2() {
    rt.getForObject("http://localhost/expr={expr}", String.class, "x/y");
    Assert.assertEquals("http://localhost/expr=x%2Fy", savedUri.toString());
}

// FAIL in MVC 4 or 5
@Test
public void test3() {
    rt.getForObject("http://localhost/expr={expr}", String.class, "x+y");
    Assert.assertEquals("http://localhost/expr=x%2By", savedUri.toString());
}

// ok in MVC 4.x, FAIL in MVC 5
@Test
public void test4() {
    rt.getForObject("http://localhost/expr={expr}", String.class, "x+y");
    Assert.assertEquals("http://localhost/expr=x+y", savedUri.toString());
}

这可能是 Spring MVC 的更大重构的一部分,因为它在这里也表现在一个完全不同的地方

问题详情

下面的独立测试最好地说明了我的问题。不要被吓倒ClientHttpRequestFactory- 重要的部分是最后两种测试方法。

package com.stackoverflow.questions;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.StringReader;
import java.io.StringWriter;
import java.net.URI;

import org.apache.commons.io.input.ReaderInputStream;
import org.apache.commons.io.output.WriterOutputStream;
import org.junit.Assert;
import org.junit.Test;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.client.ClientHttpRequest;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.web.client.RestTemplate;

public class RestTemplateTest {

    StringWriter stringWriter = new StringWriter();

    WriterOutputStream writerOutputStream = new WriterOutputStream(stringWriter);

    HttpHeaders headers = new HttpHeaders();

    URI savedUri;

    ClientHttpRequestFactory rf = new ClientHttpRequestFactory() {
        @Override
        public ClientHttpRequest createRequest(URI uri, HttpMethod httpMethod) throws IOException {
            savedUri = uri;
            return new ClientHttpRequest() {
                @Override
                public OutputStream getBody() throws IOException {
                    return writerOutputStream;
                }

                @Override
                public HttpHeaders getHeaders() {
                    return headers;
                }

                @Override
                public URI getURI() {
                    return uri;
                }

                @Override
                public String getMethodValue() {
                    return httpMethod.name();
                }

                @Override
                public ClientHttpResponse execute() throws IOException {

                    writerOutputStream.close();

                    return new ClientHttpResponse() {
                        @Override
                        public HttpHeaders getHeaders() {
                            return new HttpHeaders();
                        }

                        @Override
                        public InputStream getBody() throws IOException {
                            return new ReaderInputStream(new StringReader("test"));
                        }

                        @Override
                        public String getStatusText() throws IOException {
                            return "OK";
                        }

                        @Override
                        public HttpStatus getStatusCode() throws IOException {
                            return HttpStatus.OK;
                        }

                        @Override
                        public int getRawStatusCode() throws IOException {
                            return 200;
                        }

                        @Override
                        public void close() {
                        }
                    };
                }
            };
        }
    };
    RestTemplate rt = new RestTemplate(rf);

    @Test
    public void test1() {
        String resp = rt.getForObject("http://whatever", String.class);
        Assert.assertEquals("test", resp);
    }

    @Test
    public void test2() {
        rt.getForObject("http://localhost/expr={expr}", String.class, "x/y");
        Assert.assertEquals("http://localhost/expr=x%2Fy", savedUri.toString());
    }

    @Test
    public void test3() {
        rt.getForObject("http://localhost/expr={expr}", String.class, "x+y");
        Assert.assertEquals("http://localhost/expr=x%2By", savedUri.toString());
    }

}

发生了什么:

应该发生什么:

test3() 应该通过。+应该编码为 %2B,因为它+是 URL 中的特殊字符,并且被许多服务器端 Web 框架解释为空格。

这里发生了什么,是否有通用修复?

标签: javaspringresttemplate

解决方案


是否+应该编码存在不确定性。较旧的 RFC 说是,新的 RFC 说可能。

尝试设置编码模式如下:

  DefaultUriBuilderFactory builderFactory = new DefaultUriBuilderFactory();
  builderFactory.setEncodingMode(EncodingMode.VALUES_ONLY);
  restTemplate.setUriTemplateHandler(builderFactory);

有关讨论,请参阅SPR- 19394和SPR-20750


推荐阅读