我使用 JAX-RS 2.0 和 Glassfish 4 实现了一个简单的 REST-WebService。我的问题是,如果我向 WS 发送日期或日历,pojo 会丢失日期信息,并且值为 NULL。在我的 WebService 中,我设置了日期tmp.setDate(1)
,在我的 JUnit-Test-Case 中,我得到了信息[Thu Jan 01 02:00:00 CET 1970]
有什么想法为什么我通过请求丢失了日期信息?
我的客户 (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() + "]");
}
我的网络服务-资源
@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();
}
我的波乔
@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;
}
}