[英]Java 8 Exception: com.fasterxml.jackson.datatype.jsr310.deser.InstantDeserializer has no default (no arg) constructor
I have a model class that has a field:我有一个 model class 有一个字段:
@JsonDeserialize(using = InstantDeserializer.class)
@JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSZ")
private OffsetDateTime offsetDt;
When a request of this model is sent to the server, my controller throws an exception:当这个 model 的请求发送到服务器时,我的 controller 抛出异常:
Caused by: java.lang.IllegalArgumentException:
Class com.fasterxml.jackson.datatype.jsr310.deser.InstantDeserializer
has no default (no arg) constructor
The pom.xml has the dependency of version 2.8.11: pom.xml 有 2.8.11 版本的依赖:
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
</dependency>
I understand that it is caused by @JsonDeserialize requiring no-arg constructor, but is there a workaround?我知道这是由@JsonDeserialize 需要无参数构造函数引起的,但是有解决方法吗?
The error says that you need a class with no arg constructor, so you could extend from InstantDeserializer
. 该错误表明您需要一个没有arg构造函数的类,因此可以从
InstantDeserializer
扩展。 (Take as example the code in InstantDeserializer
for the arguments of super()
) (以
InstantDeserializer
中的super()
参数为例)
public class DefaultInstantDeserializer extends InstantDeserializer<OffsetDateTime> {
public DefaultInstantDeserializer() {
super(OffsetDateTime.class, DateTimeFormatter.ISO_OFFSET_DATE_TIME,
OffsetDateTime::from,
a -> OffsetDateTime.ofInstant(Instant.ofEpochMilli(a.value), a.zoneId),
a -> OffsetDateTime.ofInstant(Instant.ofEpochSecond(a.integer, a.fraction), a.zoneId),
(d, z) -> d.withOffsetSameInstant(z.getRules().getOffset(d.toLocalDateTime())),
true);
}
}
Then you can use it: 然后,您可以使用它:
@JsonDeserialize(using = DefaultInstantDeserializer.class)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.