运行 使用 Jersey 的 Rest 客户端
Running Rest client using Jersey
在网络服务器上部署了简单的网络服务,其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)
你的依赖很乱。请注意以下事项:
Jersey 1.x 和 Jersey 2.x 使用不同的包名:
- 球衣 1.x:
com.sun.jersey
- 球衣 2.x:
org.glassfish.jersey
Jersey 1.x 和 Jersey 2.x 实现不同版本的 JAX-RS 规范:
- 球衣1.x:JSR 311 (
jsr311-api
神器)
- 球衣2.x:JSR 339 (
javax.ws.rs-api
神器)
泽西岛 1.x 依赖关系
要使用 Jersey 1.x,您的 pom.xml
中需要以下依赖项:
<!-- server -->
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-server</artifactId>
<version>1.19</version>
</dependency>
<!-- client -->
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-client</artifactId>
<version>1.19</version>
</dependency>
阅读更多有关 Jersey 1.x 依赖项的信息 here。
泽西岛 2.x 依赖关系
如果您想使用 Jersey 2.x,您必须将以下依赖项添加到您的 pom.xml
:
<!-- server -->
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<!-- if your container implements Servlet API older than 3.0,
use "jersey-container-servlet-core" -->
<artifactId>jersey-container-servlet</artifactId>
<version>2.22.1</version>
</dependency>
<!-- client -->
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-client</artifactId>
<version>2.22.1</version>
</dependency>
阅读更多有关 Jersey 2.x 依赖项的信息 here。
在网络服务器上部署了简单的网络服务,其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)
你的依赖很乱。请注意以下事项:
Jersey 1.x 和 Jersey 2.x 使用不同的包名:
- 球衣 1.x:
com.sun.jersey
- 球衣 2.x:
org.glassfish.jersey
Jersey 1.x 和 Jersey 2.x 实现不同版本的 JAX-RS 规范:
- 球衣1.x:JSR 311 (
jsr311-api
神器) - 球衣2.x:JSR 339 (
javax.ws.rs-api
神器)
泽西岛 1.x 依赖关系
要使用 Jersey 1.x,您的 pom.xml
中需要以下依赖项:
<!-- server -->
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-server</artifactId>
<version>1.19</version>
</dependency>
<!-- client -->
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-client</artifactId>
<version>1.19</version>
</dependency>
阅读更多有关 Jersey 1.x 依赖项的信息 here。
泽西岛 2.x 依赖关系
如果您想使用 Jersey 2.x,您必须将以下依赖项添加到您的 pom.xml
:
<!-- server -->
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<!-- if your container implements Servlet API older than 3.0,
use "jersey-container-servlet-core" -->
<artifactId>jersey-container-servlet</artifactId>
<version>2.22.1</version>
</dependency>
<!-- client -->
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-client</artifactId>
<version>2.22.1</version>
</dependency>
阅读更多有关 Jersey 2.x 依赖项的信息 here。