解析JSON对象时出现NoClassDefFoundError JsonAutoDetect

时间:2012-04-10 18:24:06

标签: java eclipse json web-applications noclassdeffounderror

我正在使用亚马逊的云服务开发一个webapp,我需要使用JSON对象。我的项目是如何设置的,我有一个HTML表单,用户将填写他们的信息并提交。提交后,数据将被放入在线数据库,并向他们发送确认电子邮件。在我将数据提交到数据库之前,我需要将所有数据放入JSON对象中。我的servlet,用Java完成,看起来像这样:

public class FormHandling extends HttpServlet {
    //doGet function
    public void doGet (HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
        // In this part of the code I take in the user data and build an HTML
        // page and return it to the user

        // Create String Mapping for User Data object
        Map<String,Object> userData = new HashMap<String,Object>();

        // Map the names into one struct. In the code below we are creating 
        // values for the nameStruct from the information the user has
        // submitted on the form.
        Map<String,String> nameStruct = new HashMap<String,String>();
        nameStruct.put("fName", request.getParameter("fname"));
        nameStruct.put("lName", request.getParameter("lname"));
        nameStruct.put("mInit", request.getParameter("minit"));

        // Map the three email's together (providing there are 3).
        // This is the same logic as the names.
        Map<String,String> emailStruct = new HashMap<String,String>();
        emailStruct.put("email", request.getParameter("email1"));
        emailStruct.put("email", request.getParameter("email2"));
        emailStruct.put("email", request.getParameter("email3"));

        // Put Name Hash Value Pair inside User Data Object
        userData.put("name", nameStruct);
        userData.put("emails", emailStruct);
        userData.put("age", 22);

        // Create the Json data from the userData
        createJson(userData);

        // Close writer
        out.close();
    }

    public File createJson(Map<String,Object> userData) 
        throws JsonGenerationException, JsonMappingException, IOException {
        File user = new File("user.json");

        // Create mapper object
        ObjectMapper mapper = new ObjectMapper();

        // Add the userData information to the json file
        mapper.writeValue(user, userData);
        return user;
    }
}

当我使用此代码提交表单时,收到以下错误消息:

  

java.lang.NoClassDefFoundError:com / fasterxml / jackson / annotation / JsonAutoDetect       com.fasterxml.jackson.databind.introspect.VisibilityChecker $标准。(VisibilityChecker.java:169)       com.fasterxml.jackson.databind.ObjectMapper。(ObjectMapper.java:197)       edu.tcnj.FormHandling.createJson(FormHandling.java:115)       edu.tcnj.FormHandling.doGet(FormHandling.java:104)       edu.tcnj.FormHandling.doPost(FormHandling.java:131)       javax.servlet.http.HttpServlet.service(HttpServlet.java:641)       javax.servlet.http.HttpServlet.service(HttpServlet.java:722)

现在我知道错误意味着它没有找到Jackson数据库库。我在Eclipse中开发,我知道构建路径不时会混乱,所以我也将库放在WebContent \ WEB-INF \ lib文件夹中。这解决了我以前遇到过的问题。但是,这次似乎并没有解决我的问题。我甚至打开了jar文件来物理检查所需的库和类是否存在,它们是。我已经尝试过搜索并查看不同的方法来包含库但似乎没有任何工作。

4 个答案:

答案 0 :(得分:127)

对于将来来这里的人,答案是:

如果您只复制了jackson corejackson databind,则需要jackson annotations才能使用ObjectMapper

例如,请确保您拥有以下内容:

[16:32:01]:/android-project/libs     master]$ ls -lAF
total 2112
-rw-r--r--@ 1 jeffamaphone  staff   33525 Jun 18 16:06 jackson-annotations-2.0.2.jar
-rw-r--r--@ 1 jeffamaphone  staff  193693 Jun  7 16:42 jackson-core-2.0.2.jar
-rw-r--r--@ 1 jeffamaphone  staff  847121 Jun 18 15:22 jackson-databind-2.0.2.jar

答案 1 :(得分:64)

为maven用户扩展@ jeffamaphone的优秀答案

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

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

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

答案 2 :(得分:7)

或以下gradle配置:

compile 'com.fasterxml.jackson.core:jackson-core:2.2.2'
compile 'com.fasterxml.jackson.core:jackson-databind:2.2.2'
compile 'com.fasterxml.jackson.core:jackson-annotations:2.2.2'

答案 3 :(得分:1)

如果您使用maven并且已经安装了所有库,就像我的情况一样,一个简单的mvn cleanmvn install已修复问题。