是的,我同意URI.resolve(URI)方法与RFC 3986不兼容.原来的问题本身就提供了大量的研究,有助于这个结论.首先,让我们清除任何混乱.
如Raedwald所解释的(在现在删除的答案中),以/结尾或不以/结尾的基本路径有区别:
> fizz相对于/ foo / bar是:/ foo / fizz
> fizz相对于/ foo / bar /是:/ foo / bar / fizz
虽然正确,但这不是一个完整的答案,因为原来的问题不是询问一个path(即上面的“fizz”).相反,该问题涉及相对URI引用的单独的query component. URI类constructor used in the example code接受五个不同的String参数,除了queryString参数之外的所有参数都被传递为空. (请注意,Java接受一个空字符串作为路径参数,这在逻辑上会导致一个“空”路径组件,因为“the path component is never undefined”虽然它是“may be empty (zero length)”.)这将在以后很重要.
在earlier comment年,Sajan Chandran指出,java.net.URI class被记录为实现RFC 2396,而不是RFC 3986的问题.前者在2005年被后者废弃了.URI类Javadoc没有提到新的RFC可以被解释作为其不兼容的更多证据.我们再多一点:
> JDK-6791060是一个开放的问题,建议这个类“应该更新为RFC 3986”.那里的一个评论警告说,“RFC3986并没有完全倒退
兼容2396“.
>以前尝试将URI类的部分更新为符合RFC 3986(例如JDK-6348622),但是后来的兼容性为rolled back. (另见JDK邮件列表中的this discussion)
>尽管路径“合并”逻辑听起来相似,如noted by SubOptimal所示,较新的RFC中指定的伪代码与actual implementation不匹配.在伪代码中,当相对URI的路径为空时,生成的目标路径按原样复制基本URI.在这些条件下不执行“合并”逻辑.与该规范相反,Java的URI实现修剪了最后一个字符之后的基本路径,如问题所述.
如果您想要RFC 3986行为,则可以使用URI类的替代方法. Java EE 6实现提供javax.ws.rs.core.UriBuilder,(在泽西1.18)似乎按照预期的行为(见下文).至少就RFC的编码而言,就编码不同URI组件而言至关重要.
在J2EE之外,Spring 3.0引入了UriUtils,具体记录了“基于RFC 3986的编码和解码”. Spring 3.1不推荐使用其中的一些功能,并引入了UriComponentsBuilder,但是它不符合任何特定的RFC,不幸的是.
测试程序,展示不同的行为:
import java.net.*;
import java.util.*;
import java.util.function.*;
import javax.ws.rs.core.UriBuilder; // using Jersey 1.18
public class StackOverflow22203111 {
private URI withResolveURI(URI base, String targetQuery) {
URI reference = queryOnlyURI(targetQuery);
return base.resolve(reference);
}
private URI withUriBuilderReplaceQuery(URI base, String targetQuery) {
UriBuilder builder = UriBuilder.fromUri(base);
return builder.replaceQuery(targetQuery).build();
}
private URI withUriBuilderMergeURI(URI base, String targetQuery) {
URI reference = queryOnlyURI(targetQuery);
UriBuilder builder = UriBuilder.fromUri(base);
return builder.uri(reference).build();
}
public static void main(String... args) throws Exception {
final URI base = new URI("http://example.com/something/more/long");
final String queryString = "query=http://local:282/rand&action=aaaa";
final String expected =
"http://example.com/something/more/long?query=http://local:282/rand&action=aaaa";
StackOverflow22203111 test = new StackOverflow22203111();
Map> strategies = new LinkedHashMap<>();
strategies.put("URI.resolve(URI)", test::withResolveURI);
strategies.put("UriBuilder.replaceQuery(String)", test::withUriBuilderReplaceQuery);
strategies.put("UriBuilder.uri(URI)", test::withUriBuilderMergeURI);
strategies.forEach((name, method) -> {
System.out.println(name);
URI result = method.apply(base, queryString);
if (expected.equals(result.toString())) {
System.out.println(" MATCHES: " + result);
}
else {
System.out.println(" EXPECTED: " + expected);
System.out.println(" but WAS: " + result);
}
});
}
private URI queryOnlyURI(String queryString)
{
try {
String scheme = null;
String authority = null;
String path = null;
String fragment = null;
return new URI(scheme, authority, path, queryString, fragment);
}
catch (URISyntaxException syntaxError) {
throw new IllegalStateException("unexpected", syntaxError);
}
}
}
输出:
URI.resolve(URI)
EXPECTED: http://example.com/something/more/long?query=http://local:282/rand&action=aaaa
but WAS: http://example.com/something/more/?query=http://local:282/rand&action=aaaa
UriBuilder.replaceQuery(String)
MATCHES: http://example.com/something/more/long?query=http://local:282/rand&action=aaaa
UriBuilder.uri(URI)
MATCHES: http://example.com/something/more/long?query=http://local:282/rand&action=aaaa