Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: ObjectMapper class: readValue method fails

  1. #1
    Junior Member
    Join Date
    Dec 2017
    Location
    Saylorsburg, PA, USA
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default ObjectMapper class: readValue method fails

    I'm putting together a demo website using JSP/JQuery on the client side and java on the server side. I am calling Jquery's .ajax() method to pass data to a servlet, trying to pass JSON data from the client to the server.

    I found this example and modeled my code on it:

    https://hmkcode.com/java-servlet-sen...g-jquery-ajax/

    Rather that using the 'Article' class mentioned at that URL, I used my own example class 'EmailRecord' shown at the end of this post. I have tested this class and it passes my test.

    My copy of the example java code looks like the code shown below, except I have, for brevity, eliminated my debug statements and sanity checking. In my actual code, I test that my objects are not null, so I know that values for 'br' and 'mapper' are valid. In my test code, I print out the value of the variable 'json', which is:

    returnVal=JJA20&email=j%40j.c

    I also call 'mapper.copy()', also not shown in the code below, to verify that my 'mapper' variable is indeed a valid handle to an ObjectMapper instance.

        // 1. get received JSON data from request
        String json = ""; 
        BufferedReader br = 
            new BufferedReader(new InputStreamReader(request.getInputStream()));
        json = br.readLine();
     
        // 2. initiate jackson mapper
        ObjectMapper mapper = new ObjectMapper();
     
        // 3. Convert received JSON to EmailRecord
        EmailRecord er = mapper.readValue(json, EmailRecord.class);
     
        // 4. Set response type to JSON
        response.setContentType("application/json");
     
        // 5. Add article to List<Article> (not needed in my case)
     
        // 6. Send List<Article> as JSON to client
        mapper.writeValue(response.getOutputStream(), er);
     
        return;

    When I compile this code and run it, if fails at the line:
         EmailRecord er = mapper.readValue(json, EmailRecord.class);

    After failling, I tried wrapping the line in a try/catch operation. It did indeed throws an exception, but so far I have not figured out how to get the stacktrace. The most likely reason I can think for the failure is that the servelet is not finding the 2nd argument, 'EmailRecord.class'. This class can be found at localhost:8080/bip/WEB-INF/classes/bfs/bip/Element.class, where 'bip' is my test website. I assume the mapper knows to look there, but this is only an assumption. If not true, that explains the failure.

    So here are my questions:

    1) Where does ObjectMapper readValue look for EmailRecord.class?

    2) Is there a way to get a stacktrace from the exception that this thrown? I tried the following

            try {
                er = mapper.readValue(json, EmailRecord.class);
            }   
            catch ( Exception e ) { 
                e.printStackTrace();
            }

    but I don't know where the stacktrace will be directed.

    3) For what other reasons would the readValue method fail?

    BTW, the reason I am not attaching any source code is that when I push the "Manage Attachments" button on the web page, nothing happens. I tried it in both Firefox and Opera and got the same results.

    Below is the EmailRecord.java class that I previously mentioned.

    package bfs.bip;
     
    import java.io.*;
     
    public class EmailRecord
    {
        private String d_addr;
        private String d_returnVal;
     
    public EmailRecord()
    {
        setAddr(""); 
        setReturnVal("");
    }
     
    public void setAddr(String addr)
    {
        d_addr = addr;
    }
     
    public String getAddr()
    {
        return(d_addr);
    }
    public void setReturnVal(String val)
    {
        d_returnVal = val;
    }
     
    public String getReturnVal()
    {
        return(d_returnVal);
    }
     
    public static void main(String args[])
    {
     
        EmailRecord er = null;
        er = new EmailRecord();
     
        er.setReturnVal("Looks Good");
        System.out.println("Return Value = " + er.getReturnVal());
        er.setAddr("j@j.c");
        System.out.println("email = " + er.getAddr());
    }
     
    }
    Last edited by Jim Anderson; July 23rd, 2020 at 12:10 PM. Reason: CLOSED

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: ObjectMapper class: readValue method fails

    I don't know if anyone on this site is knowledgeable in JQuery, ajax or JSON.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Dec 2017
    Location
    Saylorsburg, PA, USA
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: ObjectMapper class: readValue method fails

    Thank you for letting me know. I'm am marking this as 'Solved' and consider it closed. I can't see how to simple close it or remove it.

Similar Threads

  1. Call class method from another class
    By NewbieJavaProgrammer in forum Object Oriented Programming
    Replies: 1
    Last Post: November 21st, 2012, 06:56 AM
  2. create a test class (main method) to start(run) the class in Java
    By curious725 in forum Java Theory & Questions
    Replies: 5
    Last Post: August 1st, 2012, 03:21 AM
  3. Trying to compile Fails
    By d_davis90 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: June 14th, 2012, 09:32 AM
  4. Test Fails but I don't know why, please help
    By cutekill0 in forum What's Wrong With My Code?
    Replies: 8
    Last Post: September 22nd, 2011, 12:31 PM
  5. Accessing a method of one class in another class
    By Sai in forum What's Wrong With My Code?
    Replies: 6
    Last Post: March 23rd, 2010, 04:06 PM