2

I'm developing a web service that relies on Jersey servlet, and uses Jersey json converter.

I have a PatientDTO that has a field java.util.Date birthDate

Here are my methods

@GET
@Path("/{param}")
@Produces(MediaType.APPLICATION_JSON)
public Response getPatient(@PathParam("param") String id) {

    PatientDTO patientDTO;
    PatientManager manager = new PatientManagerFacade();

    patientDTO = manager.getPatientById(id);

    if (patientDTO == null) {
        return Response.status(204).build();
    }

    return Response.status(200).entity(patientDTO).build();
}

@POST
@Path("/add")
@Consumes(MediaType.APPLICATION_JSON)
public Response addPatient(PatientDTO patient) {
    PatientManager manager = new PatientManagerFacade();
    String id = manager.addPatient(patient);
    return Response.status(200).entity(id).build();
}

The problem is the following

For my POST method I'm giving the following json, and it gets parsed just fine, the timespan gets casted to java.util.Date

{
  "firstName": "John",
  "lastName": "Smith",
  "birthDate": 722725200000,
  "gender": "MALE",
  "age": 18
}

But when I'm calling the return method, it gives me the following json

{
  "id": "NM-001",
  "firstName": "John",
  "lastName": "Smith",
  "birthDate": "1992-11-26",
  "gender": "MALE",
  "age": 23
}

But I want the birthDate to be a timespan too. How do I achieve this? Any annotations telling actually how to parse the field, or something like that?

Thanks in advance

vcmkrtchyan
  • 2,536
  • 5
  • 30
  • 59
  • See [here](http://stackoverflow.com/a/28310779/2587435) for how you can configure Jackson, and try configuring [this feature](http://fasterxml.github.io/jackson-databind/javadoc/2.4/com/fasterxml/jackson/databind/SerializationFeature.html#WRITE_DATE_KEYS_AS_TIMESTAMPS) – Paul Samsotha Jan 13 '16 at 08:46

0 Answers0