使用Jersey运行Rest客户端的java
简单web服务部署在web服务器上,其url为
http://localhost:8080/jersey-example-new/rs/account/details/param
现在我尝试通过Jersey客户端使用这项服务:
ClientConfig config = new DefaultClientConfig();
Client client = Client.create(config);
WebResource service = client.resource(UriBuilder.fromUri("http://localhost:8080/jersey-example-new/rs/account/details/andy").build());
项目依赖关系:
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>2.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-server</artifactId>
<version>1.9.1</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-client</artifactId>
<version>1.9.1</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.1.3</version>
</dependency>
我得到了一个错误:
Exception in thread "main" java.lang.AbstractMethodError:
javax.ws.rs.core.UriBuilder.uri(Ljava/lang/String;)Ljavax/ws/rs/core/UriBuilder;
at javax.ws.rs.core.UriBuilder.fromUri(UriBuilder.java:119) at
com.javacodegeeks.jersey.main.RestClient.main(RestClient.java:21)
# 1 楼答案
你的依赖关系很混乱。注意以下几点:
新泽西1号。x和泽西2。使用不同的包名:
com.sun.jersey
org.glassfish.jersey
新泽西1号。x和泽西2。实现不同版本的JAX-RS规范:
泽西1号。x依赖性
使用球衣1。x,在^{中需要以下依赖项:
了解更多关于Jersey 1的信息。x依赖项here
泽西2号。x依赖性
如果你想使用球衣2。x,您必须将以下依赖项添加到
pom.xml
:了解更多关于泽西2的信息。x依赖项here