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