NoSuchMethodError: MultivaluedMap.addAll in Jersey Client
Asked Answered
G

3

24

I'm trying to use Jersey Client to simulate HTTP requests to my web service. I tried to implement the simple example from the documentation. Here's my short code:

public void restoreTest(String sessionId) throws Exception {
    Client client = ClientBuilder.newClient();
    WebTarget target = client.target(idsUrl).path("restore");
    Form form = new Form();
    form.param("sessionId", sessionId);
    target.request(MediaType.APPLICATION_FORM_URLENCODED_TYPE);
}

I didn't even implement the whole example, because currently I get an exception in the last line:

java.lang.NoSuchMethodError: javax.ws.rs.core.MultivaluedMap.addAll(Ljava/lang/Object;[Ljava/lang/Object;)V
    at org.glassfish.jersey.client.ClientRequest.accept(ClientRequest.java:254)
    at org.glassfish.jersey.client.JerseyWebTarget.request(JerseyWebTarget.java:232)
    at org.glassfish.jersey.client.JerseyWebTarget.request(JerseyWebTarget.java:60)
    at org.icatproject.idsclient.TestingClient.restoreTest(TestingClient.java:112)
    at org.icatproject.ids.ids2.ArchiveTest.restoreThenArchiveDataset(ArchiveTest.java:55)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:616)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:76)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

I only added this dependency to my pom.xml:

<dependency>
    <groupId>org.glassfish.jersey.core</groupId>
    <artifactId>jersey-client</artifactId>
    <version>2.2</version>
</dependency>

I tried to google the problem, as well as debug the application, but I can't really see what's wrong with it.

EDIT

All Maven dependencies:

<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.8</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.eclipse.persistence</groupId>
    <artifactId>eclipselink</artifactId>
    <version>2.0.2</version>
</dependency>
<dependency>
    <groupId>org.eclipse.persistence</groupId>
    <artifactId>javax.persistence</artifactId>
    <version>2.0.0</version>
</dependency>
<dependency>
    <groupId>com.sun.jersey</groupId>
    <artifactId>jersey-bundle</artifactId>
    <version>1.8</version>
</dependency>
<dependency>
    <groupId>com.google.protobuf</groupId>
    <artifactId>protobuf-java</artifactId>
    <version>2.4.1</version>
</dependency>
<dependency>
    <groupId>javax</groupId>
    <artifactId>javaee-api</artifactId>
    <version>6.0</version>
    <scope>provided</scope>
</dependency>
<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.1</version>
</dependency>
<dependency>
    <groupId>commons-lang</groupId>
    <artifactId>commons-lang</artifactId>
    <version>2.3</version>
</dependency>
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>1.6.4</version>
</dependency>
<dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.16</version>
</dependency>
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-log4j12</artifactId>
    <version>1.6.4</version>
</dependency>
<dependency>
    <groupId>org.glassfish.jersey.core</groupId>
    <artifactId>jersey-client</artifactId>
    <version>2.2</version>
</dependency>
Gaspard answered 2/9, 2013 at 13:11 Comment(2)
Can you show the rest of your pom dependencies?Gardener
@orid Added the dependencies.Gaspard
L
42

This looks like an inconsistency pertaining to the JAX-RS API version (which contains the MultiValuedMap).

You are using client jersey-client v2.2, which is compiled against v2.0 of the JAX-RS API. But your runtime states to run with Java EE 6, which defines JAX-RS API v1.1. So your code expects v2.0 of the JAX-RS API, but gets v1.1 at runtime.

This is the MultiValuedMap API for Java EE 6:

http://docs.oracle.com/javaee/6/api/javax/ws/rs/core/MultivaluedMap.html (no addAll method).

And for Java EE 7:

http://docs.oracle.com/javaee/7/api/javax/ws/rs/core/MultivaluedMap.html (this one includes the addAll method).

As you are using Java EE 6, you should be using jersey-client v1.8, not 2.2. Or you should be including the Java EE 7 API in your runtime classpath, and not 6.

Leapt answered 3/9, 2013 at 9:17 Comment(6)
Thank you! It has to work on Glassfish3, so I decided to keep to JEE6 and use jersey-client 1.8. One more thing that was wrong with my dependencies was the provided scope of javaee-api. Jersey Client needs concrete implementation of it if it is to work outside the server (e.g. during integration tests). This helped: #12143372Gaspard
Mixing jersey-client 1.8 and 2.x could cause such issue. After cleaning up the dependencies and only using jersery-client 2.x, the same problem is gone in my applicationSuperphysical
I thought I removed all jersey 1.x dependencies but found that I still had javax.ws.rs.jsr311-api version 1.1.1 in my dependencies. After removing it the problem was gone...Borchers
For mvn, run mvn dependency tree and for gradle, run gradle dependencies and survey the list of dependencies to identify potential conflicts.Captivate
@Alexander Bollaert : How can you identify the runtime version ?Eserine
If you're running on a J2EE certified server, the specification version pegs the component API versions. Check en.wikipedia.org/wiki/Java_Platform,_Enterprise_Edition (Web Profile).Leapt
G
8

The offending class comes from this dependency

<dependency>
    <groupId>javax</groupId>
    <artifactId>javaee-api</artifactId>
    <version>6.0</version>
    <scope>provided</scope>
</dependency>

It has jax-rs 1.1 core classes inside, specifically MultivaluedMap interface without addAll method.

Either disable it (It seems you can if only using Jersey), or upgrade to version to 7.0

Gardener answered 3/9, 2013 at 9:19 Comment(1)
Ori Dar: how did/would you exactly disable the jax-rs 1.1 from the dependency? I tried excluding artifactId core from group javax.ws.rs, also jsr311-api from same group as separate try and once I had even javaee-api dependency set to 7.0. Neither case, no one has helped alone. Should some combo or different exclusion to be used?Behm
M
0

In my case this looks to be an incompatibility issue with Jersey Client and Jersey Core. I got past it by disabling the client:

<dependencies>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpcore</artifactId>
            <version>4.4.4</version>
        </dependency>
        <dependency>
            <groupId>pl.pragmatists</groupId>
            <artifactId>JUnitParams</artifactId>
            <version>1.0.5</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.7.12</version>
            <scope>test</scope>
        </dependency>
        <!--<dependency>-->
            <!--<groupId>com.sun.jersey</groupId>-->
            <!--<artifactId>jersey-client</artifactId>-->
            <!--<version>1.19</version>-->
        <!--</dependency>-->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.14</version>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
        <dependency>
            <groupId>com.googlecode.json-simple</groupId>
            <artifactId>json-simple</artifactId>
            <version>1.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.4</version>
        </dependency>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>2.45.0</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.16.8</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>com.saucelabs</groupId>
            <artifactId>sauce_junit</artifactId>
            <version>2.1.18</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>javax.ws.rs</groupId>
            <artifactId>javax.ws.rs-api</artifactId>
            <version>2.0.1</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.core</groupId>
            <artifactId>jersey-client</artifactId>
            <version>2.16</version>
        </dependency>
    </dependencies>
Montagu answered 22/2, 2017 at 16:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.