Java Restful webservices - NoClassDefFoundError: org/glassfish/jersey/ExtendedConfig

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30304153/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 09:25:59  来源:igfitidea点击:

Restful webservices - NoClassDefFoundError: org/glassfish/jersey/ExtendedConfig

javaweb-servicesrestjerseyjax-rs

提问by user3641702

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

所以我在这个线程中创建了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.

我在 Netbeans 上创建了一个新的 web 项目,我正在使用 Apache Tomcat。这是使用 Web 服务的代码。我已经通过一些教程来生成此代码。

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.access0(URLClassLoader.java:71)
    at java.net.URLClassLoader.run(URLClassLoader.java:361)
    at java.net.URLClassLoader.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.run(URLClassLoader.java:366)
    at java.net.URLClassLoader.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.

我想这是一个libs问题,但我无法解决它。仅使用 JAX-RS 2.0 是不够的,所以我也在使用 Jersey 库。

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.

这里有什么问题?这是使用此网络服务的正确方法吗?我看过其他一些版本,但不确定要使用哪个版本。

采纳答案by user3641702

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.

正如我怀疑的那样,这是一个库问题。此代码和这些导入仅适用于 Jersey 2.X(截至今天,它的版本为 2.17)。我需要在包中包含所有库,即所有核心 Jersey 模块 jar 以及所有必需的第 3 方依赖项。我不需要包含任何 JAX-RS 2.0 库或 Jersey 1.X jar。

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.

我做的另一件事是在 Netbeans 中使用 New RESTful Java Client 向导,然后将 Jersey 1.X jar 添加到我的项目中(截至今天,它的版本为 1.19)。API 有点不同,但也能正常工作。

回答by Luis Rodríguez

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.

尝试将正确版本的 jersey-common.jar 包含到应用程序的 lib 文件夹中。

回答by Kevin Crain

Seems that you are missing the jersey-commonjar from your classpath

似乎jersey-common您的类路径中缺少jar

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

您可以通过以下网址在 maven 上下载 jar : - http://mvnrepository.com/artifact/org.glassfish.jersey.core/jersey-common