简体   繁体   English

Java 8 异常:com.fasterxml.jackson.datatype.jsr310.deser.InstantDeserializer 没有默认值

[英]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.

相关问题 Class com.fasterxml.jackson.datatype.joda.deser.DateTimeDeserializer 没有默认(无参数)构造函数 - Class com.fasterxml.jackson.datatype.joda.deser.DateTimeDeserializer has no default (no arg) constructor 线程“main”中的异常 java.lang.NoClassDefFoundError: com/fasterxml/jackson/datatype/jsr310/JavaTimeModule - Exception in thread "main" java.lang.NoClassDefFoundError: com/fasterxml/jackson/datatype/jsr310/JavaTimeModule 如何使用 Jackson DataType: JSR310 Deser 独立? - How to use Jackson DataType: JSR310 Deser standalone? 它是jackson-datatype-jsr310解串器中的错误吗? - Is it a bug in jackson-datatype-jsr310 deserializer? NoSuchMethodError:com.fasterxml.jackson.module.scala.deser.BigDecimalDeserializer - NoSuchMethodError: com.fasterxml.jackson.module.scala.deser.BigDecimalDeserializer com.fasterxml.jackson.databind.deser.UnresolvedForwardReference与@JsonIdentityInfo - com.fasterxml.jackson.databind.deser.UnresolvedForwardReference with @JsonIdentityInfo Jackson Deserializer没有默认(无arg)构造函数 - Jackson Deserializer has no default(no arg) constructor Logstash logback 编码器因 java.util.ServiceConfigurationError 失败(无法实例化jackson.datatype.jsr310.JavaTimeModule) - Logstash logback encoder failing with java.util.ServiceConfigurationError (jackson.datatype.jsr310.JavaTimeModule could not be instantiated) 使用jackson-datatype-jsr310在Spring配置中进行RESTful服务 - RESTful service in Spring configuration with jackson-datatype-jsr310 带有 jackson-datatype-jsr310 版本 2.12.3 的 InvalidDefinitionException - InvalidDefinitionException with jackson-datatype-jsr310 version 2.12.3
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM