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