springboot配置时间字符串之间的转换器
1、后端将时间转成String传给前端在基础配置类baseConfig类中Alt+insert,然后Override如下这个方法
LocalDate等都是Java8的新特性,需要借助javaTimeModule,这个本身有其对应的格式规则,但是不是我们想要的,我们需要覆盖重写原来的
@Override protected void configureMessageConverters(List<HttpMessageConverter<?>> converters) { ObjectMapper objectMapper = new ObjectMapper(); objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); objectMapper.disable(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE); JavaTimeModule javaTimeModule = new JavaTimeModule(); javaTimeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))); javaTimeModule.addSerializer(LocalDate.class, new LocalDateSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd"))); objectMapper.registerModule(javaTimeModule); converters.add(new MappingJackson2HttpMessageConverter(objectMapper)); } 123456789101112131415 2、后端接收前端传过来的String转成