解析 JSON 对象时出现 NoClassDefFoundError JsonAutoDetect [英] NoClassDefFoundError JsonAutoDetect while parsing JSON object

查看:36
本文介绍了解析 JSON 对象时出现 NoClassDefFoundError JsonAutoDetect的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Amazon 的云服务开发 Web 应用程序,我需要使用 JSON 对象.我的项目是如何设置的,我有一个 HTML 表单,用户将在其中填写他们的信息并提交.提交后,数据将被放入在线数据库,并向他们发送确认电子邮件.在将数据提交到数据库之前,我需要将所有数据放入一个 JSON 对象中.我的 servlet,用 Java 完成,如下所示:

I am developing a webapp using Amazon's cloud services and I need to make use of JSON objects. How my project is set up is, I have an HTML form where the user will fill in their information and submit. Once submitted the data will be placed into an online database and a confirmation email is sent to them. Before I can submit the data to the data base I need to place all of it into a JSON object. My servlet, done in Java, looks like this:

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;
    }
}

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

When I submit the form using this code I get the following error message:

java.lang.NoClassDefFoundError: com/fasterxml/jackson/annotation/JsonAutoDetectcom.fasterxml.jackson.databind.introspect.VisibilityChecker$Std.(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)

java.lang.NoClassDefFoundError: com/fasterxml/jackson/annotation/JsonAutoDetect com.fasterxml.jackson.databind.introspect.VisibilityChecker$Std.(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 中进行开发,我知道构建路径时不时会出错,所以我还将这些库放在了 WebContentWEB-INFlib 文件夹中.这已经解决了我之前遇到的问题.但是,这一次似乎并没有解决我的问题.我什至打开了 jar 文件来物理检查所需的库和类是否在那里,并且它们在那里.我试过到处搜索并寻找不同的方法来包含库,但似乎没有任何效果.

Now I know that error means that it isn't finding the Jackson databind library. I am developing in Eclipse and I know the build path messes up from time to time so I also placed the libraries in the WebContentWEB-INFlib folder. This has solved issues I've had before. However, this does not seem to solve my problem this time around. I have even opened the jar files to physically check that the library and class needed are there, and they are. I've tried searching all over and looking into different ways to include the library but nothing seems to work.

推荐答案

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

For those who come here in the future, the answer is:

如果你只复制了jackson corejackson databind,你需要jackson annotations来使用ObjectMapper>.

If you've only copied jackson core and jackson databind, you need jackson annotations to use the ObjectMapper.

例如,确保你有这样的东西:

For example, make sure you have something like this:

[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

这篇关于解析 JSON 对象时出现 NoClassDefFoundError JsonAutoDetect的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆