简体   繁体   English

杰克逊的 InvalidDefinitionException

[英]InvalidDefinitionException with jackson

In a spring boot 2.2.2, java 11 a object is received via Ibm mq.在 Spring Boot 2.2.2、java 11 中,通过 Ibm mq 接收对象。

Object received have LocalDate data type.接收到的对象具有 LocalDate 数据类型。

Project have spring-boot-starter-web starter in maven.项目在 maven 中有 spring-boot-starter-web starter。

I see theses jar in the project我在项目中看到了这些 jar

jackson datatype -jdk 8-2.10.1 jackson-datatype-jsr310-2.10.1杰克逊数据类型-jdk 8-2.10.1 杰克逊数据类型-jsr310-2.10.1

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class BillingEvent {
    public Long Id;
    public LocalDate billingCreatedDate;
}

In my properties I have在我的属性中,我有

spring.jackson.serialization.WRITE_DATES_AS_TIMESTAMPS=false spring.jackson.serialization.WRITE_DATES_AS_TIMESTAMPS=false

Error I get我得到的错误

com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of java.time.LocalDate (no Creators, like default construct, exist): no String-argument constructor/factory method to deserialize from String value ('2019-09-02') com.fasterxml.jackson.databind.exc.InvalidDefinitionException:无法构造java.time.LocalDate实例(没有创建者,如默认构造,存在):没有从字符串值反序列化的字符串参数构造函数/工厂方法('2019-09 -02')

For me it was enough to add setter to your BillingEvent , like:对我来说,将 setter 添加到您的BillingEvent就足够了,例如:

public void setBillingCreatedDate(String str) {
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");        
    billingCreatedDate = LocalDate.parse(str, formatter);
}

More about formatting here: String to LocalDate更多关于此处格式化的信息: String to LocalDate

Based on comments:基于评论:

this Is there a jackson datatype module for JDK8 java.time?是 JDK8 java.time 的 jackson 数据类型模块吗? might help you and if not it is not for help, below an example of working impl.可能会帮助你,如果不是,它不是帮助,下面是一个工作实现的例子。 (without any checks or so): (没有任何检查左右):

public class LocalDateDeserializer extends JsonDeserializer<LocalDate> {

    private final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");

    @Override
    public LocalDate deserialize(JsonParser p, DeserializationContext ctxt)
            throws IOException, JsonProcessingException {        
          return LocalDate.parse(p.readValueAs(String.class), formatter);
    }

}

you could use it also elsewhere like you would use it in your BillingEvent :您也可以在其他地方使用它,就像在BillingEvent使用它BillingEvent

@JsonDeserialize(using = LocalDateDeserializer.class)
public LocalDate billingCreatedDate;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用jackson数据绑定序列化对象时的Java InvalidDefinitionException - Java InvalidDefinitionException when serializing object with jackson databind com.fasterxml.jackson.databind.exc.InvalidDefinitionException - com.fasterxml.jackson.databind.exc.InvalidDefinitionException 在 Jackson 中反序列化时出现运行时错误 - InvalidDefinitionException - Runtime error when deserialiasing in Jackson - InvalidDefinitionException 杰克逊:使用[Ljava.lang.Double的InvalidDefinitionException - jackson: InvalidDefinitionException using [Ljava.lang.Double com.fasterxml.jackson.databind.exc.InvalidDefinitionException:未找到序列化程序 - com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found Java.lang.NoClassDefFoundError: com/fasterxml/jackson/databind/exc/InvalidDefinitionException - Java.lang.NoClassDefFoundError: com/fasterxml/jackson/databind/exc/InvalidDefinitionException 带有 jackson-datatype-jsr310 版本 2.12.3 的 InvalidDefinitionException - InvalidDefinitionException with jackson-datatype-jsr310 version 2.12.3 com.fasterxml.jackson.databind.exc.InvalidDefinitionException:无法构造实例 - com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of Jackson 添加附加字段到 JSON 抛出 com.fasterxml.jackson.databind.exc.InvalidDefinitionException - Jackson adding additional fields to JSON throws com.fasterxml.jackson.databind.exc.InvalidDefinitionException com.fasterxml.jackson.databind.exc.InvalidDefinitionException:无法构造`org.springframework.web.multipart.MultipartFile`的实例 - com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `org.springframework.web.multipart.MultipartFile`
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM