1. 创建日期config类
@Configuration
public class LocalDateTimeSerializerConfig {
@Value("${spring.jackson.date-format:yyyy-MM-dd HH:mm:ss}")
private String pattern;
@Bean
public LocalDateTimeSerializer localDateTimeDeserializer() {
return new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(pattern));
}
@Bean
public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer() {
return builder -> builder.serializerByType(LocalDateTime.class, localDateTimeDeserializer());
}
}
2. 测试
创建实体类
@Data
public class TestEntity {
private String name;
private LocalDateTime dateTimes;
}
controller使用
@GetMapping("/test")
public TestEntity test(){
TestEntity testEntity=new TestEntity();
testEntity.setName("admin");
testEntity.setDateTimes(LocalDateTime.now());
return testEntity;
}
运行效果
{"name":"admin","dateTimes":"2018-10-09 17:39:07"}