有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

使用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. # 1 楼答案

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

    新泽西1号。x和泽西2。使用不同的包名:

    • 泽西1号。x:com.sun.jersey
    • 泽西2号。x:org.glassfish.jersey

    新泽西1号。x泽西2。实现不同版本的JAX-RS规范:

    泽西1号。x依赖性

    使用球衣1。x,在^{中需要以下依赖项:

    <!  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依赖性

    如果你想使用球衣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>
    

    了解更多关于泽西2的信息。x依赖项here