'Jersey REST get returns java.lang.NoSuchMethodError

I'm trying to get a response from a working REST webservice.

I have come up with the following looking at the documentation:

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

However I am returned a not very friendly:

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

I'm using NetBeans and didn't add any library as it seems the GlassFish Server comes with javax.ws.rs-api.jar which make the above run.

Do I need to add Jersey.jar as well inside libraries or am I missing something? Thank you



Solution 1:[1]

Seems Jersey developers are very maven-centric and discourage including the jars manually, so if you're not using maven but instead are including jars inside NetBeans you'll need to add the whole content of jaxrs-ri, including jars inside ext/ directory for the above code to run.

On a side note if you're using client and server side of jersey inside the same project the manual inclusion of jars may break he server side, so better keep them separate!

Solution 2:[2]

I had this problem while trying to run the above code in Eclipse.

1) The first problem was that org.jboss.resteasy.jaxrs-api was included, which solved the compile problems but not the runtime problems, giving the exception noted above. The solution to this problem is simply to include org.jboss.resteasy.rest-client into your pom.

2) A second problem was that I was started getting a java.lang.NoSuchMethodError: org.jboss.resteasy.spi.ResteasyProviderFactory.<init>(Lorg/jboss/resteasy/spi/ResteasyProviderFactory;)V exception. The problem was that in Eclipse I had defined jBoss EAP version 6.3 in the servers, and even though the project wasn't using it, the jars were still being included into the classpath, causing a conflict during runtime.

The solution then is either to open a new workspace or a new copy of Eclipse that doesn't have a the jBoss EAP server defined, or to update the resteasy modules in your current jBoss EAP 6.3 server. This is the ultimate recommendation in this thread: RESTEasy Client + NoSuchMethodError. Makes sense to me because I would rather my rest-server be running under the current jars anyway.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 dendini
Solution 2 Community