springboot中,json和LocalDateTime的转换报错

Ranger
  • 47

两个springboot的java-web应用A和B,A使用RestTemplate向B发送http请求,B返回一个带有LocalDateTime的实体类,我用Postman测试过,此时返回的数据是json,如下:

{
    "verificationTime": {
        "hour": 23,
        "minute": 9,
        "second": 56,
        "nano": 531000000,
        "dayOfYear": 266,
        "dayOfWeek": "SATURDAY",
        "month": "SEPTEMBER",
        "dayOfMonth": 23,
        "year": 2017,
        "monthValue": 9,
        "chronology": {
            "id": "ISO",
            "calendarType": "iso8601"
        }
}

A应该接收到这个json后,转换为对应的实体类(就是包含LocalDateTime的对象),但是A这时报错了,错误提示是:
org.springframework.http.converter.HttpMessageNotReadableException:
JSON parse error:
Can not construct instance of java.time.LocalDateTime:
no suitable constructor found, can not deserialize from Object value (missing default constructor or creator, or perhaps need to add/enable type information?);
nested exception is com.fasterxml.jackson.databind.JsonMappingException:
Can not construct instance of java.time.LocalDateTime:
no suitable constructor found, can not deserialize from Object value (missing default constructor or creator, or perhaps need to add/enable type information?)
at [Source: java.io.PushbackInputStream@7cd9fc42; line: 1, column: 129] (through reference chain: com.demo.domain.impl.VerificationResults["verificationTime"])

实体类如下:

@Table
@Entity
public class VerificationResults
{
    @Id
    @GeneratedValue
    private int id;

    @Column(nullable = false)
    private LocalDateTime verificationTime;

    public VerificationResults()
    {
    }

    public int getId()
    {
        return id;
    }

    public void setId(int id)
    {
        this.id = id;
    }

    public LocalDateTime getVerificationTime()
    {
        return verificationTime;
    }

    public void setVerificationTime(LocalDateTime verificationTime)
    {
        this.verificationTime = verificationTime;
    }
}
回复
阅读 10.7k
1 个回答

这里有相关的一些介绍: https://github.com/FasterXML/...
或者尝试添加

<dependency>
    <groupId>com.fasterxml.jackson.datatype</groupId>
    <artifactId>jackson-datatype-jsr310</artifactId>
</dependency>
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
宣传栏