【发布时间】:2020-10-16 00:01:51
【问题描述】:
我在将 JSON 映射到具有 LocalDateTime 的 DTO 时遇到问题。
添加到 build.gradle
implementation 'com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.11.0'
这是我的 DTO 中的变量:
@Data
public class MyDto {
private Long teamId;
private Map<String, List<Long>> details;
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime dateOccurred;
}
并添加了这个
public class MyApplication {
@Autowired
private ObjectMapper objectMapper;
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
@PostConstruct
public void setUp() {
objectMapper.registerModule(new JavaTimeModule());
}
}
我错过了什么吗?我收到此错误
org.springframework.messaging.converter.MessageConversionException: Could not read JSON: Cannot construct instance of `java.time.LocalDateTime` (no Creators, like default construct, exist): no String-argument constructor/factory method to deserialize from String value ('2020-06-16 11:12:46')
谢谢!
【问题讨论】:
-
在这里查看答案:stackoverflow.com/a/29959842/3415090 这更多地与弹簧启动有关,但我认为添加提到的额外杰克逊依赖项可能会解决您的问题。
-
分享
LocalDateTime所属的整个bean。 -
@GoviS 更新了帖子
-
@mohammedkhan 我试过了,但还是一样。 :(
-
@mohammedkhan 嗨,谢谢!现在可以了!我错误地添加了
LocalDateDeerializer而不是LocalDateTimeDeserializer。非常感谢!