gpt4 book ai didi

spring - 找不到适合类型 [简单类型,类 java.time.LocalDateTime] 的构造函数

转载 作者:IT王子 更新时间:2023-10-29 06:06:25 25 4
gpt4 key购买 nike

我的程序是用 Java 8 编写的,当我使用 LocalDateTime 类型时,它会给我以下错误:

No suitable constructor found for type [simple type, class java.time.LocalDateTime]: can not instantiate from JSON object (missing default constructor or creator, or perhaps need to add/enable type information?)
at [Source: [B@5976fe6f; line: 1, column: 80] (through reference chain: com.boot.framwork.entity.UserInfo["accessTime"])

实体是这样的:

public class UserInfo implements Serializable {

private static final long serialVersionUID = 1L;

private String username;

private int age;

private LocalDateTime accessTime;

private Date time;

private List<String> list = new ArrayList<String>();

private boolean isMarried;

public UserInfo() {

}

它在我不使用 LocalDateTime 时有效

最佳答案

在我看来,您没有 JSR310 支持。 Jackson 需要一些额外的配置才能识别 Java 8 Date&Time API 数据类型。您可以通过项目依赖项添加它,只需添加对 this 的依赖即可

<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
<version>2.7.4</version>
</dependency>

或者如果你使用 Gradle

compile group: 'com.fasterxml.jackson.datatype', name: 'jackson-datatype-jsr310', version: '2.7.4'

由于您使用的是 SpringBoot,这应该足够了,因此不必手动创建一个类型为 JSR310Module 的 Bean。

关于spring - 找不到适合类型 [简单类型,类 java.time.LocalDateTime] 的构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37801871/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com