Jersey REST获取返回java.lang.NoSuchMethodError。

4

我正在尝试从一个可用的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服务器自带javax.ws.rs-api.jar,因此我没有添加任何库来运行上述内容。
我需要在库中添加Jersey.jar吗?或者我漏掉了什么?
谢谢。

当您调用http://localhost:8080/TestApplication/webresources/people/get/all时,浏览器是否会返回结果? - D00de
2个回答

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。对我来说很有道理,因为我宁愿我的rest-server在当前的jar下运行。

0

看起来Jersey开发人员非常注重Maven,并且不鼓励手动包含jar文件,因此如果您没有使用Maven,而是在NetBeans中包含jar文件,那么您需要添加jaxrs-ri的整个内容,包括ext/目录中的jar文件,以上代码才能运行。

另外,如果您在同一项目中使用Jersey的客户端和服务器端,则手动包含jar文件可能会破坏服务器端,最好将它们分开!


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接