当前位置: 首页 > 面试题库 >

NoSuchMethodError:泽西岛客户端中的MultivaluedMap.addAll

麹凯捷
2023-03-14
问题内容

我正在尝试使用Jersey客户端模拟对我的Web服务的HTTP请求。我尝试实现文档中的简单示例。这是我的短代码:

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);
}

我什至没有实现整个示例,因为当前我在最后一行收到一个异常:

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)

我只将此依赖项添加到我的pom.xml

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

我试图用谷歌搜索问题,以及调试应用程序,但我真的看不出问题出在哪里。

编辑

所有Maven依赖项:

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

问题答案:

这看起来像与JAX-RS API版本(包含MultiValuedMap)有关的不一致。

您正在使用客户端jersey-client v2.2,它是根据JAX-RS API的v2.0编译的。但是您的运行时html" target="_blank">声明要与定义JAX-RS API
v1.1的Java EE 6一起运行。因此,您的代码期望使用JAX-RS API的v2.0,但在运行时获得v1.1。

这是用于Java EE 6的MultiValuedMap API:

http://docs.oracle.com/javaee/6/api/javax/ws/rs/core/MultivaluedMap.html(无addAll方法)。

对于Java EE 7:

http://docs.oracle.com/javaee/7/api/javax/ws/rs/core/MultivaluedMap.html(其中包括addAll方法)。

在使用Java EE 6时,应该使用jersey-client v1.8,而不是2.2。或者,您应该在运行时类路径中包含Java EE 7
API,而不是6。



 类似资料:
  • 我想执行我们的服务器支持的 PATCH 请求,以便使用 Jersey 客户端进行测试。我的代码如下,有人可以让我知道下面的代码有什么问题吗? 这是完全的例外,

  • 我目前使用的是球衣 我现在要做的是设置泽西,这样当查询参数进来时(比如缩进),我可以告诉Jackson以“更漂亮的格式,也就是缩进”序列化JSON。您可以通过使用SerializationConfig.Feature.INDENT_OUTPUT配置JSON映射器来轻松地告诉Jackson这样做。 问题是,我如何在每个请求的基础上获取一个queryparam并使用它来修改Jackson的输出?

  • 问题内容: 我是球衣/ JAX-RS实施的新手。请在下面找到我的泽西岛客户代码以下载文件: 我的服务器端代码是: 在我的客户代码中,我收到200 OK的响应,但是我无法将文件保存在硬盘上。在下面的行中,我提到了需要保存文件的路径和位置。不知道这里出了什么问题,将不胜感激。谢谢!! 问题答案: 我不知道是否让Jersey让您简单地回应一个文件,就像这里一样: 你 可以 肯定使用StreamingOu

  • 我使用客户机对一个API进行REST调用,该API返回mulipart中的一个Json和一个PDF文件作为响应的第一和第二部分。 使用上面的客户端解析这个多部分响应的正确方法是什么?

  • 我正在编写一个rest客户机,它使用服务器的POST restful sevice。现在服务需要2个参数作为'form-data'中请求的一部分。 如果您有跨邮递员rest客户机,我们有一个选项来设置表单数据,并给出键值对参数。 现在,如何发送两个参数,即“文件名”,“文件版本”和它们的值作为表单数据的一部分?

  • 我正在编写一个连接到我自己的泽西Rest客户端的Android应用程序。HTTP get命令工作正常,但我在POST上遇到问题,我试图向服务器发送一些东西。我收到一个405退回,所以服务器似乎无法将请求与资源方法匹配起来。有什么想法吗?下面的测试代码… Rest服务器 ANDROID客户端 TCPMon流量显示以下POST /Maintenance_Server/rest/responsesHTT