如何将Jackson JSON映射器与Java 8 LocalDateTime一起使用?
org.codehaus.jackson.map.JsonMappingException:无法从JSON String实例化类型[simple type,class java.time.LocalDateTime]的值; 没有单字符串构造函数/工厂方法(通过引用链:MyDTO ["field1"] - > SubDTO ["date"])
我的json序列化有问题ZonedDateTime.当转换为json时,它产生了一个巨大的对象,我不希望每次都传输所有这些数据.所以我尝试将其格式化为ISO,但它不起作用.我怎样才能让它格式化?
这是我的实体类:
@MappedSuperclass
public abstract class AuditBase {
@Id
@GeneratedValue
private Long id;
@CreatedDate
private ZonedDateTime createdDate;
@LastModifiedDate
private ZonedDateTime lastModifiedDate;
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
public ZonedDateTime getLastModifiedDate() {
return lastModifiedDate;
}
public void setLastModifiedDate(ZonedDateTime lastModifiedDate) {
this.lastModifiedDate = lastModifiedDate;
}
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
public ZonedDateTime getCreatedDate() {
return createdDate;
}
public void setCreatedDate(ZonedDateTime createdDate) {
this.createdDate = createdDate;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@PrePersist
public void …Run Code Online (Sandbox Code Playgroud) 我已经通过链接: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)