1

i implement an simple REST-WebService with JAX-RS 2.0 and Glassfish 4. My problem is, if i send an Date or Calendar to the WS, the pojo lost the Dateinformation an the values are NULL. In my WebService i set the Date tmp.setDate(1) and in my JUnit-Test-Case i get the information [Thu Jan 01 02:00:00 CET 1970] Any ideas why i lost the dateinformation by the request?

My Client (JUNIT)

@Test
public void testPOST() {
client = ClientBuilder.newClient();
this.root = this.client.target(SERVER + "/restExample/example");

    OriginalSimpleDTO dto = new OriginalSimpleDTO();
    dto.setDate(new Date(1));

    final Entity<OriginalSimpleDTO> entity = Entity.entity(dto, mediaType);
    Response response = this.root.request().post(entity);
    System.out.println("POST Status " + response.getStatus());
    OriginalSimpleDTO tmp = response.readEntity(OriginalSimpleDTO.class);
    System.out.println("POST tmp-Date [" + tmp.getDate() + "]");

}

My WebSerice-Resource

@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response testPost(OriginalSimpleDTO dto) {
    System.out.println("dto date [" + dto.getDate() + "]");

    OriginalSimpleDTO tmp = new OriginalSimpleDTO();
    tmp.setDate(new Date(1));

    return Response.ok(tmp).build(); 
}

My Pojo

@XmlRootElement
@JsonInclude (Include.NON_NULL)
public class OriginalSimpleDTO implements Serializable {
    private static final long serialVersionUID = 1L;
    private Date datum;

    //default Constructor
   public OriginalSimpleDTO() {}

   @JsonSerialize (using = DateSerializer.class)
   public Date getDatum() {
      return datum;
   }

   @JsonDeserialize (using = DateDeserializer.class)
   public void setDatum(Date datum) {
      this.datum = datum;
   }
}
Tim
  • 643
  • 3
  • 12
  • 23

2 Answers2

0

It depends on the different versions of Jackson-Provider inside the glassfish 4, not correct initialize the provider.

See REST webservice don't use my JsonSerializer

Community
  • 1
  • 1
Tim
  • 643
  • 3
  • 12
  • 23
0

Okay the solution that I posted before was not correct, the correct answer was to use a ISO 8601 format so what really worked was the following sting below

Using Glassfish4, JAX-RS2.0, NO other custom code (de-serializer)

Domain class

@Temporal(TemporalType.TIMESTAMP)
Date timeMiliStart;
WebResource

WebResource

@Path("/post/json")
@POST   
@Consumes({MediaType.APPLICATION_JSON})
public Response createBaseJson(Base base){

JSON (Postman POST) Call attempted that kept failing

{"baseStr":"My new Car","languageId":1,"timeMiliStart":"2013-11-15 15:42:13.300","timeMiliEnd":"2013-11-15 15:42:16.521"}

JSON (Postman POST) Call attempted that succeeded

{"baseStr":"My new Car","languageId":1,"timeMiliStart":"1972-10-02T21:16:31.347+01:00","timeMiliEnd":"1997-07-16T19:20:30.45+01:00"}
user3008410
  • 644
  • 7
  • 15