Java UriComponents构建器/UriComponents编码行为不正确

Java UriComponents构建器/UriComponents编码行为不正确,java,spring,url,spring-mvc,Java,Spring,Url,Spring Mvc,我有以下代码包org.springframework.web.util UriComponentsBuilder.fromUriString("/test/").queryParam("rt","http://a.com?u=1").build(false).encode().toUriString() 实际结果: /测试/?rt= 我所期望的是: /测试/?rt=http%3a%2f%2fa.com%3fu%3d1 有什么想法吗?据我所知,UriComponentsBuilder不会自动对查

我有以下代码包org.springframework.web.util

UriComponentsBuilder.fromUriString("/test/").queryParam("rt","http://a.com?u=1").build(false).encode().toUriString()
实际结果: /测试/?rt=

我所期望的是: /测试/?rt=http%3a%2f%2fa.com%3fu%3d1

有什么想法吗?

据我所知,UriComponentsBuilder不会自动对查询参数进行编码,只会对其实例化时使用的原始HttpUrl进行编码。换句话说,您仍然需要显式编码:

UriComponentsBuilder是根据RFC3986对您的URI进行编码的,请参阅,特别是第3.4节,该节介绍了URI的“查询”组件

在查询组件中,允许使用字符“/”和“:”,不需要转义

以“/”字符为例:“/”字符没有层次结构,并且“/”字符没有特殊含义。查询组件由未转换的“?”和可选字符明确分隔。所以它不需要编码

UriComponentsBuilder.fromUriString("/test/").queryParam("rt",URLEncoder.encode("http://a.com?u=1", "UTF-8")).build(false).encode().toUriString();

输出:/test/?rt=http%253A%252F%252Fa.com%253Fu%253D1

谢谢。但您可以看到,UriComponentBuilder仍然执行类似于编码的操作。它将“=”更改为“%3D”。