Restful webservices - NoClassDefFoundError: org/gl

2019-04-10 09:30发布

问题:

So I created the webservice in this thread and finally I managed to solve the problem. Now I'm trying to consume this webservice.

I've created a new web project on Netbeans and I'm using Apache Tomcat. Here's the code to consume the webservice. I've gone through some tutorials to produce this code.

package com.client;

import java.net.URI;

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriBuilder;

import org.glassfish.jersey.client.ClientConfig;

public class HelloClient {


  public void consumeRest(){

    ClientConfig config = new ClientConfig();

    Client client = ClientBuilder.newClient(config);

    WebTarget target = client.target(getBaseURI());

    System.out.println("response");

    System.out.println(target.path("hello").path("world").request()

    .accept(MediaType.APPLICATION_JSON).get(Response.class)

    .toString());

    }


  private URI getBaseURI() {

    return UriBuilder.fromUri("http://localhost:8084/restful_example").build();

  }

} 

I've created a main class to call the consume method. When I run it, I get this exception:

Exception in thread "main" java.lang.NoClassDefFoundError: org/glassfish/jersey/ExtendedConfig
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
    at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
    at com.client.HelloClient.consumeRest(HelloClient.java:29)
    at com.client.main.main(main.java:16)
Caused by: java.lang.ClassNotFoundException: org.glassfish.jersey.ExtendedConfig
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
    ... 14 more
Java Result: 1

I guess it's a libs problem but I couldn't solve it. Using only JAX-RS 2.0 is not sufficient, so I'm also using Jersey libs.

What's wrong here? Is this the correct way to consume this webservice? I've seen some other versions and I'm not sure which one to use.

回答1:

Seems that you are missing the jersey-common jar from your classpath

You can download the jar on maven at the following url: - http://mvnrepository.com/artifact/org.glassfish.jersey.core/jersey-common



回答2:

As I suspected, it was a libs problem. This code and these imports work only with Jersey 2.X (it's on version 2.17 as of today). I needed to include all the libs in the bundle, ie, all the core Jersey module jars as well as all the required 3rd-party dependencies. I don't need to include any JAX-RS 2.0 libs or Jersey 1.X jars.

Another thing I did was using the New RESTful Java Client wizard in Netbeans and then adding the Jersey 1.X jars to my project (it's on version 1.19 as of today). The API is a little different but works as well.



回答3:

To remove the particular message:

NoClassDefFoundError: org/glassfish/jersey/ExtendedConfig

Try to include the correct version of jersey-common.jar into the lib folder of your application.