Quick Intro (can be skipped) : Hi, there's a lot of questions and answers about this topic, but sometimes the solution is so simple that you just don't even think about, and because I've lost a lot of time I want to post a complement to all answers.
Problem : You have a JSON REST service that handles POST requests to save a JSON object, but that object contains a Date field that is not parsed out-of-the-box by Genson.
The Java Object :
public class MyObj {
// The field you want to serialize/deserialize
private Date date;
// Constructor with no arguments needed by Genson
public MyObj() {}
}
The REST service with Jersey :
@Path("/api/my-obj")
public class MyObjAPI {
@POST
@Consumes("application/json")
public Response insert(MyObj myObj) {
// do what you want with myObj, it's ready to use with the date
return Response.created('url/to/created/object').build();
}
}
The client in javascript with jQuery :
// This is the JSON Object to POST
var myObj = {
date: new Date()
};
$.ajax({
method: 'POST',
url: '/api/my-obj',
data: JSON.stringify(myObj),
dataType: 'json',
processData: false,
contentType: 'application/json'
});