gpt4 book ai didi

java - 使用 Jersey 运行 Rest 客户端

转载 作者:行者123 更新时间:2023-11-29 07:37:37 25 4
gpt4 key购买 nike

在网络服务器上部署了一个简单的网络服务,它的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.xJersey 2.x 使用不同的包名称:

  • Jersey 1.x:com.sun.jersey
  • Jersey 2.x:org.glassfish.jersey

Jersey 1.xJersey 2.x 实现了不同版本的 JAX-RS 规范:

Jersey 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 .

Jersey 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 .

关于java - 使用 Jersey 运行 Rest 客户端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33756774/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com