为什么RestTemplate不对'+'符号进行urlencode,而是对其他所有内容进行urlencode?

时间:2019-06-02 23:22:37

标签: java spring resttemplate

我发现了一个奇怪的问题,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较大引用的一部分,因为它也出现在完全不同的位置here

问题详细信息

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

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());
    }

}

发生了什么事

  • 分隔符号/已正确编码为%2F-test2()通过了

  • 附加符号+未编码为%2B-它仍然为+并且test3()失败

应如何处理:

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

这是怎么回事,是否有通用修复程序?

1 个答案:

答案 0 :(得分:1)

不确定是否+应该被编码。较旧的RFC表示同意,较新的RFC表示可能。

尝试如下设置编码模式:

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

请参见SPR-19394SPR-20750进行讨论。