com.fasterxml.jackson.databind.exc.InvalidDefinitionException:无法构造`java.time.Instant`的实例(无创建者,如默认构造):

PAA*_*PAA 5 json jackson

我已经通过链接:JSON 解析错误:无法构造 java.time.LocalDate 的实例:没有从字符串值反序列化的字符串参数构造函数/工厂方法,但得到以下错误。

错误:

com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `java.time.Instant` (no Creators, like default construct, exist): no String-argument constructor/factory method to deserialize from String value ('2019-08-28T15:15:44.183Z')
Run Code Online (Sandbox Code Playgroud)

爪哇

@Builder
@Data
@AllArgsConstructor
@NoArgsConstructor
@JsonIgnoreProperties(ignoreUnknown = true)
public class StudentResponse {
    private Integer a;
    private String b;
    private String c;
    private String d;
    private String e;
    private Instant lastUpdateDate;
}
Run Code Online (Sandbox Code Playgroud)

Man*_*dis 7

我能够解析一个 JSON,其中Instant字段是"2019-08-28T15:15:44.183Z"

首先,确保你有一个依赖。例如在build.gradle

implementation 'com.fasterxml.jackson.datatype:jackson-datatype-jsr310'
Run Code Online (Sandbox Code Playgroud)

然后配置ObjectMapper为使用此依赖项,而不是(反)序列化为Instant默认情况下的时间戳。我将我的设置ObjectMapper为:

ObjectMapper mapper  = new ObjectMapper()
        .findAndRegisterModules()
        .disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
Run Code Online (Sandbox Code Playgroud)