我正在尝试从一个正常工作的REST get服务中获得响应。
通过查看文档,我得出了以下结论:
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,或者我是否遗漏了什么?谢谢
发布于 2014-11-01 05:52:14
当我尝试在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中,我在服务器中定义了JAREAP6.3版,即使项目没有使用它,jBoss仍然包含在类路径中,这会在运行时造成冲突。
然后,解决方案是打开一个新的工作区,或者打开一个没有定义resteasy EAP服务器的Eclipse的新副本,或者更新当前jBoss EAP6.3服务器中的resteasy模块。这是这个帖子的终极推荐:RESTEasy Client + NoSuchMethodError。这对我来说是有意义的,因为我更希望我的rest服务器在当前的jars下运行。
https://stackoverflow.com/questions/19903942
复制相似问题