Jersey REST获取返回java.lang.NoSuchMethodError

问题描述 投票:4回答:2

我正在尝试从一个有效的REST Web服务获得响应。

我想出了以下文档:

void postRest()
{
    Client client = ClientBuilder.newClient();
    WebTarget webTarget = client.target("http://localhost:8080/TestApplication/");
    WebTarget resourceWebTarget = webTarget.path("webresources");
    WebTarget peopleWebTarget = resourceWebTarget.path("people/get/all");

    Invocation invocation = peopleWebTarget.request("text/xml").header("Content-Type", "application/xml").buildGet();
    String response = invocation.invoke(String.class);

    System.out.println(response);
}

但是我回来的不是很友好:

Exception in thread "main" java.lang.NoSuchMethodError: org.glassfish.jersey.message.internal.MessagingBinders$MessageBodyProviders.<init>(Ljava/util/Map;Ljavax/ws/rs/RuntimeType;)V
    at org.glassfish.jersey.client.ClientBinder.configure(ClientBinder.java:118)
    at org.glassfish.hk2.utilities.binding.AbstractBinder.bind(AbstractBinder.java:169)
    at org.glassfish.jersey.internal.inject.Injections.bind(Injections.java:157)
    at org.glassfish.jersey.internal.inject.Injections._createLocator(Injections.java:147)
    at org.glassfish.jersey.internal.inject.Injections.createLocator(Injections.java:137)
    at org.glassfish.jersey.client.ClientConfig$State.initRuntime(ClientConfig.java:352)
    at org.glassfish.jersey.client.ClientConfig$State.access$000(ClientConfig.java:85)
    at org.glassfish.jersey.client.ClientConfig$State$3.get(ClientConfig.java:117)
    at org.glassfish.jersey.client.ClientConfig$State$3.get(ClientConfig.java:114)
    at org.glassfish.jersey.internal.util.collection.Values$LazyValue.get(Values.java:275)
    at org.glassfish.jersey.client.ClientConfig.getRuntime(ClientConfig.java:669)
    at org.glassfish.jersey.client.ClientRequest.getConfiguration(ClientRequest.java:276)
    at org.glassfish.jersey.client.JerseyInvocation.validateHttpMethodAndEntity(JerseyInvocation.java:124)
    at org.glassfish.jersey.client.JerseyInvocation.<init>(JerseyInvocation.java:97)
    at org.glassfish.jersey.client.JerseyInvocation.<init>(JerseyInvocation.java:90)
    at org.glassfish.jersey.client.JerseyInvocation$Builder.buildGet(JerseyInvocation.java:201)
    at org.glassfish.jersey.client.JerseyInvocation$Builder.buildGet(JerseyInvocation.java:154)
    at client.RestClient.postRest(RestClient.java:24)
    at client.RestClient.main(RestClient.java:14)
Java Result: 1

我正在使用NetBeans并且没有添加任何库,因为GlassFish Server似乎带有javax.ws.rs-api.jar,这使得上面的运行。

我是否需要在库中添加Jersey.jar或者我错过了什么?谢谢

rest java-ee glassfish-4 jersey-client jersey-2.0
2个回答
0
投票

看起来Jersey开发人员非常以maven为中心,不鼓励手动包括jar,所以如果你不使用maven而是在NetBeans中包含jars,你需要添加jaxrs-ri的全部内容,包括在ext /目录中的jars以上代码运行。

另外,如果你在同一个项目中使用了jersey的客户端和服务器端,手动包含jar可能会破坏服务器端,所以最好将它们分开!


0
投票

我在尝试在Eclipse中运行上面的代码时遇到了这个问题。

1)第一个问题是包含了org.jboss.resteasy.jaxrs-api,它解决了编译问题,但没有解决运行时问题,给出了上面提到的例外情况。解决这个问题的方法就是将org.jboss.resteasy.rest-client包含在你的pom中。

2)第二个问题是我开始得到java.lang.NoSuchMethodError: org.jboss.resteasy.spi.ResteasyProviderFactory.<init>(Lorg/jboss/resteasy/spi/ResteasyProviderFactory;)V例外。问题是在Eclipse中我在服务器中定义了jBoss EAP版本6.3,即使项目没有使用它,jar仍然被包含在类路径中,导致运行时发生冲突。

然后解决方案是打开一个新工作区或没有定义jBoss EAP服务器的Eclipse的新副本,或更新当前jBoss EAP 6.3服务器中的resteasy模块。这是此主题中的最终建议:RESTEasy Client + NoSuchMethodError。对我有意义,因为我宁愿我的休息服务器无论如何都要在当前的jar下运行。

© www.soinside.com 2019 - 2024. All rights reserved.