Hello,
I am having trouble receiving JSON data from an HTML form and mapping it
into a Java Object.
The line that is causing me error is:
Episode episode = mapper.readValue(json , Episode.class);
Which results in: HTTP Status 500 - Unrecognized token 'start_date': was
expecting ('true', 'false' or 'null')
Some background about the code:
A snippet of my Episode Class:
package com.fh;
public class Episode {
private String start_date;
private String end_date;
private String report_date;
private String attend_date;
private String proximity;...
public String getProximity() {
return proximity;
}
public void setProximity(String proximity) {
this.proximity = proximity;
etc.....
And the servlet class:
Enter code here..@WebServlet(name = "newfh", urlPatterns = {"/form"})
public class cmfhapp extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse
response)
throws ServletException, IOException {
System.out.println("doGet hit");
request.getRequestDispatcher("/index1.jsp").forward(request,
response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse
response)
throws ServletException, IOException {
// GET RECEIVED JSON DATA
BufferedReader br = new BufferedReader((new InputStreamReader(
request.getInputStream())));
String json = "";
if (br != null) {
json = br.readLine();
}
System.out.println("Print proximity Value " +request.getParameter(
"proximity"));
ObjectMapper mapper = new ObjectMapper();
Episode episode = mapper.readValue(json , Episode.class);
System.out.println(episode.getProximity());
System.out.println("doPost hit");
request.getRequestDispatcher("/login.html").forward(request,
response);
}
}.
The JSON data is being sent from the form with:
$.fn.serializeObject = function()
{
var o = {};
var a = this.serializeArray();
$.each(a, function() {
if (o[this.name] !== undefined) {
if (!o[this.name].push) {
o[this.name] = [o[this.name]];
}
o[this.name].push(this.value || '');
} else {
o[this.name] = this.value || '';
}
});
return o;
};
<script>
$(document).ready(function() {
// click on button submit
$("#submit").on('click', function(e){
// send ajax
$.ajax({
url: "form", // url where to submit the request
type : "POST", // type of action POST || GET
dataType : "json", // data type
data : JSON.stringify($('#formfv').serializeObject()), // post
data || get data
contentType : "application/json",
success : function(result) {
console.log(result);
},
error: function(xhr, resp, text) {
console.log(xhr, resp, text);
}
})
});
});
</script>
I think the JSON data is being received correctly as the value of json when
printed in console looks like:
{
"start_date": "",
"end_date": "",
"report_date": "",
"attend_date": "",
"proximity": "Nearby",
"locationtype": "",
"reported_by": "Family",
"location": "",
And this validates on JSO linter.
So why am I getting: HTTP Status 500 - Unrecognized token 'start_date': was
expecting ('true', 'false' or 'null')
Cheers
Al
--
You received this message because you are subscribed to the Google Groups
"jackson-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
For more options, visit https://groups.google.com/d/optout.