在我的 Spring Boot 项目中,我有两个 DTO 正在尝试验证,LocationDto 和 BuildingDto。LocationDto 有一个 BuildingDto 类型的嵌套对象。
这些是我的 DTO:
位置Dto
public class LocationDto {
@NotNull(groups = { Existing.class })
@Null(groups = { New.class })
@Getter
@Setter
private Integer id;
@NotNull(groups = { New.class, Existing.class })
@Getter
@Setter
private String name;
@NotNull(groups = { New.class, Existing.class, LocationGroup.class })
@Getter
@Setter
private BuildingDto building;
@NotNull(groups = { Existing.class })
@Getter
@Setter
private Integer lockVersion;
}
Run Code Online (Sandbox Code Playgroud)
建筑Dto
public class BuildingDto {
@NotNull(groups = { Existing.class, LocationGroup.class })
@Null(groups = …Run Code Online (Sandbox Code Playgroud) 我有两个 Spring Boot 应用程序,它们通过 JMS 消息传递和 ActiveMQ 进行通信。
一个应用程序向另一个应用程序发送一个包含 LocalDateTime 属性的对象。此对象被序列化为 JSON,以便发送到其他应用程序。
我面临的问题是 Jackson 在尝试将传入的 json 映射到我的对象时无法反序列化 LocalDateTime 属性。LocalDateTime 属性在到达“侦听器应用程序”时具有以下格式:
"lastSeen":{
"nano":0,
"year":2019,
"monthValue":4,
"dayOfMonth":8,
"hour":15,
"minute":6,
"second":0,
"month":"APRIL",
"dayOfWeek":"MONDAY",
"dayOfYear":98,
"chronology":{
"id":"ISO",
"calendarType":"iso8601"
}
}
Run Code Online (Sandbox Code Playgroud)
我得到的例外如下:
org.springframework.jms.support.converter.MessageConversionException: Failed to convert JSON message content; nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of java.time.LocalDateTime
我能够通过使用以下注释暂时解决这个问题:
@JsonSerialize(as = LocalDateTimeSerializer.class)
@JsonDeserialize(using = LocalDateTimeDeserializer.class, as = LocalDateTime.class)
private LocalDateTime lastSeen;
Run Code Online (Sandbox Code Playgroud)
但它们属于jackson 数据类型 jsr310,现在已弃用。
有什么方法/替代方法可以在不使用上述注释的情况下反序列化这个 LocalDateTime 属性?或者我如何使用推荐的jackson-modules-java8 让它工作?