简体   繁体   English

从Servlet调用Jackson解析器时获取ClassNotFoundException:com.fasterxml.jackson.core.JsonProcessingException

[英]Getting ClassNotFoundException: com.fasterxml.jackson.core.JsonProcessingException when calling Jackson JSON parser from a servlet

I have a servlet, powered by Tomcat 8, which calls for a static method to perform JSON parsing with Jackson 2.9.6. 我有一个由Tomcat 8驱动的servlet,它需要一个静态方法来用Jackson 2.9.6执行JSON解析。

During the code execution: 在代码执行期间:

JSONData parsedJSONData = PDFGenerator.parseJSONDocument(jsonDocument);

I get: 我得到:

java.lang.ClassNotFoundException: com.fasterxml.jackson.core.JsonProcessingException

It's important to note, that servlet class and PDFGenerator class are located in different packages but with public access modifiers. 值得注意的是,servlet类和PDFGenerator类位于不同的程序包中,但具有public访问修饰符。

I tried to add dependencies to the root of the project via pom.xml : 我试图通过pom.xml将依赖项添加到项目的根目录:

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
    <version>2.9.6</version>
</dependency>

but it doesn't help. 但这没有帮助。

In my project I'm not using Maven, rather just added needed libraries via classpath . 在我的项目中,我没有使用Maven,而是通过classpath添加了所需的库。

What's strange is that such exception appears only when I try to call PDFGenerator.parseJSONDocument(jsonDocument) from a servlet, execution from a regular Java-class works smoothly. 奇怪的是,只有当我尝试从Servlet调用PDFGenerator.parseJSONDocument(jsonDocument)时,此类异常才会出现,而从常规Java类执行的过程PDFGenerator.parseJSONDocument(jsonDocument)顺利。

My question: 我的问题:
What and where should I add in order to be able calling a static method with the JSON parser functionality based on a FasterXML/Jackson JSON library? 为了能够使用基于FasterXML / Jackson JSON库的JSON解析器功能调用静态方法,我应该在什么地方添加什么内容?

This problem is almost certainly caused by classloaders within your servlet container (Tomcat 8.x) being unable to locate the library/JAR for Jackson. 这个问题几乎可以肯定是由您的servlet容器(Tomcat 8.x)中的类加载器无法找到Jackson的库/ JAR引起的。 The way classes/libs are loaded within servlet containers is different than a standard classpath declaration from a standalone Java application. 在Servlet容器中装入类/库的方式与来自独立Java应用程序的标准类路径声明不同。 This is why you are seeing the error from the servlet engine but not during your direct execution of the class. 这就是为什么您从servlet引擎看到错误,而不是在直接执行类的过程中看到错误的原因。 Here is the general documentation on how Tomcat 8.x handles classloading: 以下是有关Tomcat 8.x如何处理类加载的常规文档:

https://tomcat.apache.org/tomcat-8.0-doc/class-loader-howto.html https://tomcat.apache.org/tomcat-8.0-doc/class-loader-howto.html

As a general rule, to help prevent such issues in the first place, its a good idea to deploy your servlets within a "webapp" container including all required libraries (the appropriate versions of required JAR's, etc.). 作为一般规则,首先要帮助防止此类问题,将servlet部署在“ webapp”容器中(包括所有必需的库(所需JAR的适当版本等))是一个好主意。 Doing this isolates your servlets under a single classloader and automatically locates them in the correct hierarchy. 这样做会将您的servlet隔离在单个类加载器下,并自动将它们定位在正确的层次结构中。 See this portion of the linked documentation: 请参阅链接文档的这一部分:

WebappX — A class loader is created for each web application that is deployed in a single Tomcat instance. WebappX —为部署在单个Tomcat实例中的每个Web应用程序创建一个类加载器。 All unpacked classes and resources in the /WEB-INF/classes directory of your web application, plus classes and resources in JAR files under the /WEB-INF/lib directory of your web application, are made visible to this web application, but not to other ones. Web应用程序的/ WEB-INF / classes目录中的所有解压缩的类和资源,以及Web应用程序的/ WEB-INF / lib目录下的JAR文件中的类和资源,对于此Web应用程序都是可见的,但不是给其他人。

FYI, there are other ways to do this with enterprise-class web applications, but we're not talking about that here. 仅供参考,企业级Web应用程序还有其他方法可以做到这一点,但是我们在这里不做讨论。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java.lang.ClassNotFoundException: com.fasterxml.jackson.core.JsonProcessingException with ANT web project - java.lang.ClassNotFoundException: com.fasterxml.jackson.core.JsonProcessingException with ANT web project 线程“main”中的异常 java.lang.NoClassDefFoundError: com/fasterxml/jackson/core/JsonProcessingException - Exception in thread "main" java.lang.NoClassDefFoundError: com/fasterxml/jackson/core/JsonProcessingException Jackson NoClassDefFoundError: com/fasterxml/jackson/core/JsonFactory - Jackson NoClassDefFoundError: com/fasterxml/jackson/core/JsonFactory Jackson 对 Eclipse 的依赖问题:java.lang.ClassNotFoundException:com.fasterxml.jackson.core.JsonFactory - Jackson dependency issue on Eclipse: java.lang.ClassNotFoundException: com.fasterxml.jackson.core.JsonFactory Jackson dependency issue: java.lang.ClassNotFoundException: com.fasterxml.jackson.core.JsonFactory - Jackson dependency issue: java.lang.ClassNotFoundException: com.fasterxml.jackson.core.JsonFactory com.fasterxml.jackson.core.JsonParseException是否为* .json.swp? - com.fasterxml.jackson.core.JsonParseException for *.json.swp? NoClassDefFoundError com/fasterxml/jackson/core/TreeNode — json 模式验证 - NoClassDefFoundError com/fasterxml/jackson/core/TreeNode — json schema validation com.fasterxml.jackson.core.Version的目的 - Purpose of com.fasterxml.jackson.core.Version 如何克服异常-“ java.lang.ClassNotFoundException:com.fasterxml.jackson.core.Versioned” - How to get past the exception - “java.lang.ClassNotFoundException: com.fasterxml.jackson.core.Versioned” 异常:java.lang.ClassNotFoundException:com.fasterxml.jackson.core.exc.InputCoercionException - Exception : java.lang.ClassNotFoundException:com.fasterxml.jackson.core.exc.InputCoercionException
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM