只是想知道这样的代码是否不应该使用不同的 JAX-RS 实现返回相同的结果?我不太喜欢 Java。
import java.util.*;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Invocation.Builder;
import javax.ws.rs.client.WebTarget;
public class RestClientServiceImpl {
public static void main(String[] args) {
String url = "https://foo.bar/test/api/users?q=key:value";
ClientBuilder builder = ClientBuilder.newBuilder();
Client client = builder.build();
System.out.println(String.format("Before doing WebTarget %s ", url));
WebTarget target = client.target(url);
System.out.println(String.format("After doing WebTarget %s ", target.getUri()));
}
}
https://eclipse-ee4j.github.io/jersey/download.html https://repo1.maven.org/maven2/org/glassfish/jersey/bundles/jaxrs-ri/2.40/jaxrs-ri-2.40.zip
$ libs="jaxrs-ri/api/*:jaxrs-ri/ext/*:jaxrs-ri/lib/*" ; javac -cp .:${libs} RestClientServiceImpl.java ; java -cp .:${libs} RestClientServiceImpl
Before doing WebTarget https://foo.bar/test/api/users?q=key:value
After doing WebTarget https://foo.bar/test/api/users?q=key:value
https://cxf.apache.org/download.html https://dlcdn.apache.org/cxf/3.6.2/apache-cxf-3.6.2.zip
$ libs="apache-cxf-3.6.2/lib/*" ; javac -cp .:${libs} RestClientServiceImpl.java ; java -cp .:${libs} RestClientServiceImpl
Before doing WebTarget https://foo.bar/test/api/users?q=key:value
After doing WebTarget https://foo.bar/test/api/users?q=key%3Avalue
差异在于冒号编码。
用户 UriBuilder 相反,因为不同的实现以不同的方式对待它
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.UriBuilder;
public class RestClientServiceImpl {
public static void main(String[] args) {
String url = "https://foo.bar/test/api/users?q=key:value";
UriBuilder uri = UriBuilder.fromUri(url);
ClientBuilder builder = ClientBuilder.newBuilder();
Client client = builder.build();
System.out.println(String.format("Before doing WebTarget %s ", url));
WebTarget target = client.target(uri.build());
System.out.println(String.format("After doing WebTarget %s ", target.getUri()));
}
}