Jersey 500错误NoClassDefFoundError:org / json / JSONException

时间:2016-04-01 16:07:08

标签: java servlets jersey

在您将其标记为DUPLICATE之前,请理解我已检查了所有相关链接但无效。

我正在拨打以下服务。

@Path("/courses")
public class CourseService {

//  @GET
//  @Produces(MediaType.TEXT_PLAIN)
//  public String hello(){
//      return "Hello World !!!";
//  }

    private DataService _service;

    public CourseService(String jsonData) throws ParseException, IOException, JSONException {
        _service = new DataService(jsonData);
    }

    @GET
    @Produces(MediaType.APPLICATION_XML)
    public ArrayList<Course> getAllCourses(){
        return _service.get_handler().getAllCourses();

    }
}

如果我只运行代码的注释部分,我的应用程序运行正常并生成&#34; Hello World&#34;

但是当我运行我的实际服务时,它会给我以下错误。

type Exception report

message Servlet.init() for servlet Jersey Web Application threw exception

description The server encountered an internal error that prevented it from fulfilling this request.

exception

javax.servlet.ServletException: Servlet.init() for servlet Jersey Web Application threw exception
    org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:522)
    org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1095)
    org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:672)
    org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1502)
    org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1458)
    java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    java.lang.Thread.run(Thread.java:745)
root cause

java.lang.NoClassDefFoundError: org/json/JSONException
    java.lang.Class.getDeclaredConstructors0(Native Method)
    java.lang.Class.privateGetDeclaredConstructors(Class.java:2671)
    java.lang.Class.getConstructors(Class.java:1651)
    org.glassfish.jersey.server.model.MethodHandler$ClassBasedMethodHandler.<init>(MethodHandler.java:265)
    org.glassfish.jersey.server.model.MethodHandler.create(MethodHandler.java:155)
    org.glassfish.jersey.server.model.ResourceMethod$Builder.createInvocable(ResourceMethod.java:550)
    org.glassfish.jersey.server.model.ResourceMethod$Builder.build(ResourceMethod.java:536)
    org.glassfish.jersey.server.model.Resource$Builder.processMethodBuilders(Resource.java:663)
    org.glassfish.jersey.server.model.Resource$Builder.buildResourceData(Resource.java:599)
    org.glassfish.jersey.server.model.Resource$Builder.build(Resource.java:655)
    org.glassfish.jersey.server.model.Resource.from(Resource.java:798)
    org.glassfish.jersey.server.ApplicationHandler.initialize(ApplicationHandler.java:465)
    org.glassfish.jersey.server.ApplicationHandler.access$500(ApplicationHandler.java:184)
    org.glassfish.jersey.server.ApplicationHandler$3.call(ApplicationHandler.java:350)
    org.glassfish.jersey.server.ApplicationHandler$3.call(ApplicationHandler.java:347)
    org.glassfish.jersey.internal.Errors.process(Errors.java:315)
    org.glassfish.jersey.internal.Errors.process(Errors.java:297)
    org.glassfish.jersey.internal.Errors.processWithException(Errors.java:255)
    org.glassfish.jersey.server.ApplicationHandler.<init>(ApplicationHandler.java:347)
    org.glassfish.jersey.servlet.WebComponent.<init>(WebComponent.java:392)
    org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:177)
    org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:369)
    javax.servlet.GenericServlet.init(GenericServlet.java:158)
    org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:522)
    org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1095)
    org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:672)
    org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1502)
    org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1458)
    java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    java.lang.Thread.run(Thread.java:745)

这是我的项目目录的结构

enter image description here 我使用TestClient测试了我的CourseService,它运行正常。我做错了什么。

2 个答案:

答案 0 :(得分:0)

您不应该像这样定义依赖关系。所有依赖项都应该使用maven或gradle之类的东西来管理。话虽如此,你是否将jar添加到类路径?

由于您使用的是maven,因此您需要将其添加到import UIKit import CoreData class AddTableViewController: UITableViewController { // MARK: - Table view data source @IBOutlet var photoImageView:UIImageView! var content:Agenda! var imageName:String = "" override func viewDidLoad() { super.viewDidLoad() photoImageView.image = UIImage(named: imageName) } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } } 部分。例如:

<dependencies>

Eclipse应该自动将类似上面的依赖项添加到类路径中。

查看关于maven的官方教程之一:https://maven.apache.org/guides/getting-started/maven-in-five-minutes.html

答案 1 :(得分:-1)

java.lang.NoClassDefFoundError:org / json / JSONException

请确保定义并访问JSONException。