Here is a simplified version of my problem. Consider the following REST services...
@Stateless
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@Path("/test")
public class TestResource {
@POST
public Response test(Calendar c) {
System.out.println(c);
return Response.ok(Response.Status.OK).build();
}
@GET
@Path("/getCalendar")
public Response getCalendar() {
return Response.ok(toJSONString(Calendar.getInstance())).build();
}
public String toJSONString(Object object) {
GsonBuilder builder = new GsonBuilder();
builder.setDateFormat("yyy-MM-dd'T'HH:mm:ss.SSS'Z'");
Gson gson = builder.create();
return gson.toJson(object);
}
}
and of course, this...
@ApplicationPath("/rest")
public class RestActivator extends Application {
}
I am using Postman, and at first I get a JSON representation of the Calendar object by sending a GET request to 'http://localhost:8080/ProjectName/rest/test/getCalendar'. I then copy the JSON that gets returned which is
{
"year": 2015,
"month": 5,
"dayOfMonth": 29,
"hourOfDay": 10,
"minute": 7,
"second": 24
}
Then using Postman I send a POST to 'http://localhost:8080/ProjectName/rest/' with the data that was returned to me above. Then the 'javax.ws.rs.NotSupportedException: Cannot consume content type' gets thrown.
How can I fix this so the service can handle Calendar objects (and classes that have Calendar objects as fields)?
Edit:
Yes I am setting the correct content type which is application/json.
Here is the response I am getting...
com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.util.Calendar out of START_OBJECT token at [Source: io.undertow.servlet.spec.ServletInputStreamImpl@4cf87599; line: 1, column: 1]
Update: To get this to work I used @peeskillets solution.