【问题标题】:Running Rest client using Jersey使用 Jersey 运行 Rest 客户端
【发布时间】:2015-11-17 21:21:33
【问题描述】:

简单的网络服务部署在网络服务器上,其网址为

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)

【问题讨论】:

  • 当你运行它时,你的类路径中有一些 api-jar 吗?即使您针对 api 进行编译,也必须使用实现运行。
  • @folkol 我已经用我拥有的依赖项更新了这个问题。
  • 你的程序运行得如何?您确定执行代码时 api-jar 不在类路径中吗?
  • @folkol 我通过 Eclipse 作为主要方法运行它。
  • Andreas Schwarz:api jar 不应该在那里。删除“javax.ws.rs-api”,看看是否有效。

标签: java jersey


【解决方案1】:

你的依赖很混乱。请注意以下几点:

Jersey 1.xJersey 2.x 使用不同的包名:

  • 球衣 1.x:com.sun.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

【讨论】:

  • 我已将依赖项更改为 1.19 并打开 javax.ws.rs 并且似乎可以正常工作,但是当我获得 json 数据 System.out.println(service.path("restPath").path("resourcePath").accept(MediaType.APPLICATION_JSON).get(String.class)); 它返回 Exception in thread "main" com.sun.jersey.api.client.UniformInterfaceException: GET http://localhost:8080/jersey-example-new/rs/account/details/andy/restPath/resourcePath returned a response status of 404 Not Found跨度>
  • @AndreasSchwarz 仔细检查您的网址。可能是错的。
  • @AndreasSchwarz 如果 URL 在浏览器中有效,它也应该在您的 REST 客户端中有效。我无法弄清楚您在做什么:在您提到的问题中,您正在尝试访问 URL localhost:8080/jersey-example-new/rs/account/details/param。在您的评论中,网址不同:localhost:8080/jersey-example-new/rs/account/details/andy/…
  • @AndreasSchwarz 正如我之前告诉您的,您正在浏览器和 REST 客户端中尝试不同的 URL。请记住,如果您的端点与请求的 URL 不匹配,您将收到 404 Not Found 错误。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-11-09
  • 2016-11-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-09
  • 2014-03-10
相关资源
最近更新 更多