将 Instant.ofEpochSecond() 对象格式反序列化为 Instant?
Deserialize Instant.ofEpochSecond() object format to Instant?
我定义了以下模型
public class ItemDetail {
private final String name;
private final String id;
private final Instant someDate;
}
我将 someDate
设置为 setSomeDate(Instant.ofEpochSecond(resultSet.getLong("someDate"))
。我正在从数据库中读取这个。
我正在将此模型序列化为 return 以下响应,其中分配了 someDate
Instant.ofEpochSecond()
{
"name": "Some nights",
"id": "XYZZ01AS",
"someDate": {
"nano": 0,
"epochSecond": 1292486400
}
}
我的客户端代码解析了这个响应,我对输出没问题。
现在,我想对此进行测试,并想在我的测试中反序列化此响应。假设上述响应存储在 response
变量中。
String response = ..... //string containing json response same as above format
ItemDetail itemDetail = objectMapper.readValue(response, ItemDetail.class);
即使在注册 new ObjectMapper().registerModule(new JavaTimeModule())
之后这也不起作用(很明显)。
它抛出以下错误
com.fasterxml.jackson.databind.exc.MismatchedInputException: Unexpected token (START_OBJECT), expected one of [VALUE_STRING, VALUE_NUMBER_INT, VALUE_NUMBER_FLOAT] for java.time.Instant value
有没有办法在不实现自定义反序列化器的情况下完成这项工作?
注意:我只是想将此作为测试的一部分,并不打算对定义的模型 ItemDetail 进行任何更改。
你可以像这样给我们一个 Jackson MixIn
public abstract class InstantMixIn {
public InstantMixIn(
@JsonProperty("epochSecond") long epochSecond,
@JsonProperty("nano") int nanos) {
}
}
以下代码显示了此 MixIn 在您的示例中的用法
ItemDetail itemDetail = new ItemDetail("XYZZ01AS", "Some nights", Instant.ofEpochSecond(1292486400L));
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.addMixIn(Instant.class, InstantMixIn.class);
String response = objectMapper.writeValueAsString(itemDetail);
System.out.println(response);
itemDetail = objectMapper.readValue(response, ItemDetail.class);
JavaTimeModule 应该在序列化期间处于活动状态 new ObjectMapper().registerModule(new JavaTimeModule());
。否则,Objectmapper 会将 Instant 数据类型转换为具有 epochSecond
和 nano
的嵌套对象
我定义了以下模型
public class ItemDetail {
private final String name;
private final String id;
private final Instant someDate;
}
我将 someDate
设置为 setSomeDate(Instant.ofEpochSecond(resultSet.getLong("someDate"))
。我正在从数据库中读取这个。
我正在将此模型序列化为 return 以下响应,其中分配了 someDate
Instant.ofEpochSecond()
{
"name": "Some nights",
"id": "XYZZ01AS",
"someDate": {
"nano": 0,
"epochSecond": 1292486400
}
}
我的客户端代码解析了这个响应,我对输出没问题。
现在,我想对此进行测试,并想在我的测试中反序列化此响应。假设上述响应存储在 response
变量中。
String response = ..... //string containing json response same as above format
ItemDetail itemDetail = objectMapper.readValue(response, ItemDetail.class);
即使在注册 new ObjectMapper().registerModule(new JavaTimeModule())
之后这也不起作用(很明显)。
它抛出以下错误
com.fasterxml.jackson.databind.exc.MismatchedInputException: Unexpected token (START_OBJECT), expected one of [VALUE_STRING, VALUE_NUMBER_INT, VALUE_NUMBER_FLOAT] for java.time.Instant value
有没有办法在不实现自定义反序列化器的情况下完成这项工作?
注意:我只是想将此作为测试的一部分,并不打算对定义的模型 ItemDetail 进行任何更改。
你可以像这样给我们一个 Jackson MixIn
public abstract class InstantMixIn {
public InstantMixIn(
@JsonProperty("epochSecond") long epochSecond,
@JsonProperty("nano") int nanos) {
}
}
以下代码显示了此 MixIn 在您的示例中的用法
ItemDetail itemDetail = new ItemDetail("XYZZ01AS", "Some nights", Instant.ofEpochSecond(1292486400L));
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.addMixIn(Instant.class, InstantMixIn.class);
String response = objectMapper.writeValueAsString(itemDetail);
System.out.println(response);
itemDetail = objectMapper.readValue(response, ItemDetail.class);
JavaTimeModule 应该在序列化期间处于活动状态 new ObjectMapper().registerModule(new JavaTimeModule());
。否则,Objectmapper 会将 Instant 数据类型转换为具有 epochSecond
和 nano