我正在尝试从一个有效的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或者我错过了什么? 谢谢
答案 0 :(得分:0)
似乎泽西岛开发人员非常以maven为中心并且不鼓励手动包括罐子,所以如果你不使用maven而是在NetBeans中包含jar,你需要添加jaxrs-ri的全部内容,包括jars里面用于运行上述代码的ext /目录。
另一方面,如果你在同一个项目中使用jersey的客户端和服务器端,手动包含jar可能会破坏服务器端,所以最好将它们分开!
答案 1 :(得分: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。对我有意义,因为我宁愿我的休息服务器在当前的罐子下运行。