我有一个运行在WildFly上并使用RestEasy和Jackson进行REST API和JSON处理的JAX-RS应用程序。我正在尝试将LocalDate序列化和反序列化为ISO格式的字符串。一种方法是使用批注定义如下的序列化和反序列化方法。
public class MyClassWithLocalDate{
@JsonSerialize(using = LocalDateSerializer.class)
@JsonDeserialize(using = LocalDateDeserializer.class)
private LocalDate date;
private BigDecimal amount;
// constructor, getters and setters
}
我的书写了我的LocalDateSerializer,以ISO格式创建输出日期。在已部署的应用程序中可以正常工作:
@GET
@Produces("application/json")
@Path("/localdate")
public Response getLocalDate() {
MyClassWithLocalDate result = new MyClassWithLocalDate();
result.setDate(LocalDate.now());
result.setAmount(BigDecimal.TEN);
return Response.ok().entity(result).build();
}
上面的代码使用LocalDateSerializer类对LocalDate值进行序列化,从而生成紧凑的JSON {"date":"2018-07-11", "amount":10}
。但是,我想通过使用Arquillian创建部署,然后以以下方式使用RestEasy客户端访问API来为API创建测试:
@Test
@RunAsClient
public void testLocalDate() throws Exception {
Client client = ClientBuilder.newClient();
MyClassWithLocalDate myClass = new DistributionDate();
myClass.setDate(LocalDate.now());
myClass.setAmount(BigDecimal.ONE);
WebTarget targetCreate = client.target("http://localhost:8080/test/localdate"));
Response response = targetCreate.request().post(Entity.json(myClass));
此测试使用与部署的应用程序相同的代码库,尤其是相同的MyClassWithLocalDate。但是,当myClass在post(Entity.json(myClass))
中序列化时,它不使用LocalDateSerializer,而是使用默认的极其冗长的LocalDate序列化程序,生成JSON {"date":{"year":2018, "month":"JULY", "dayOfMonth":11, "dayOfWeek":"WEDNESDAY", "era":"CE", "dayOfYear":192, "leapYear":false, "monthValue":7, "chronology":{"id":"ISO", "calendarType":"iso8601"}}, "amount":10}
。发布帖子时,如何使测试类使用LocalDateSerializer序列化LocalDate?注释显然是不够的。