Jersey REST get returns java.lang.NoSuchMethodError

dendini picture dendini · Nov 11, 2013 · Viewed 10.1k times · Source

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

Answer

dendini picture dendini · Nov 11, 2013

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!