如何解決LocalDateTime傳值JSON格式化問題
LocalDateTime傳值JSON格式化問題
LocalDateTime是JDK8中提供的新功能,極大的優(yōu)化了原生日期時間類的使用。
但是第一次使用該類可能會在傳值過程中出現(xiàn)格式化的小問題(如:JSON無法解析前端所傳格式,序列化時LocalDateTime成為數(shù)組等),以下提供簡單的解決方案。
推薦方法
在WebMvcConfigurer實現(xiàn)類下完成以下兩步
1).注冊一個Converter<String, LocalDateTime>實現(xiàn)類,其作用是處理于url所攜帶的參數(shù)上(如:@RequestParam、@PathVariable )的LocalDateTime參數(shù);
2).增加一個序列化、反序列化器,作用為處理實體類的的LocalDateTime屬性。
P.S.經(jīng)過多輪測試,得出了以上方法,由于該方法需要以上兩個步驟,作為完美主義者,我曾嘗試用一個步驟解決,但并未如愿以償,若你有更好的方法,還望不吝賜教,先行謝過。
源碼如下:
// import ...
@Configuration
@EnableWebMvc
public class WebMvcConfig implements WebMvcConfigurer {
?? ?
?? ?/**
?? ? * 自定義String轉(zhuǎn)LocalDateTime方法,此方法將會作用于url所攜帶的參數(shù)上
?? ? */
? ? static class StringToLocalDateTimeConverter implements Converter<String, LocalDateTime> {
? ? ? ? @Override
? ? ? ? public LocalDateTime convert(String s) {
? ? ? ? ? ? DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
? ? ? ? ? ? return LocalDateTime.parse(s, formatter);
? ? ? ? }
? ? }
?? ?/**
?? ? * 將上述自定義方法進行添加
?? ? */
? ? @Override
? ? public void addFormatters(FormatterRegistry registry) {
? ? ? ? registry.addConverter(new StringToLocalDateTimeConverter());
? ? }
?? ?
?? ?/**
?? ? * 增加序列化與反序列化器,它們將作用于實體類的LocalDateTime屬性。
?? ? */
? ? @Override
? ? public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
? ? ? ? DateTimeFormatter pattern = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
? ? ? ? ObjectMapper objectMapper = new ObjectMapper();
? ? ? ? JavaTimeModule module = new JavaTimeModule();
? ? ? ? module.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(pattern));
? ? ? ? module.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(pattern));
? ? ? ? objectMapper.registerModule(module);
? ? ? ? converters.add(new MappingJackson2HttpMessageConverter(objectMapper));
? ? }
}其它方法
url參數(shù):
給參數(shù)加上@DateTimeFormat(此注解來自Spring,無需引入其他包),在pattern中標注約定好的格式即可。
public void Test(@RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime time) {
?? ?...
}實體類:
在實體類中的屬性上添加@JsonFormat(此注解來自Jackson,該包被Spring所依賴,無需導入),同樣在pattern中標注好格式。
@Data
public class Demo {
? ? @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
? ? private LocalDateTime time;
? ??
}由于這種方法需要對代碼中所有的LocalDateTime都進行標注,相對麻煩,且耦合度高,所以不作推薦。
LocalDateTime的json格式化問題
LocalDateTime 的 json 格式化存在問題如圖所示:

解決方式一
添加json格式化配置文件:
@Configuration
public class LocalDateTimeSerializerConfig {
@Value("${spring.jackson.date-format:yyyy-MM-dd HH:mm:ss}")
private String pattern;
public LocalDateTimeSerializer localDateTimeDeserializer() {
return new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(pattern));
}
@Bean
public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer() {
return builder -> builder.serializerByType(LocalDateTime.class, localDateTimeDeserializer());
}
}
解決方式二
@JsonFormat 注解
上面的方案全局生效,當全局的格式化方式無法滿足我們需求時,我們對日期格式要做特殊的處理:在類的屬性上添加注解
@JsonFormat(pattern = "yyyy-MM-dd") @ApiModelProperty(value = "創(chuàng)建時間") private LocalDateTime createTime;
加上效果后:

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
java線程之用Thread類創(chuàng)建線程的方法
本篇文章介紹了,Thread類創(chuàng)建線程的方法。需要的朋友參考下2013-05-05
Apache?Hudi異步Clustering部署操作的掌握
這篇文章主要介紹了Apache?Hudi異步Clustering部署操作的掌握,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步早日升職加薪2022-03-03
C#使用MySQLConnectorNet和MySQLDriverCS操作MySQL的方法
這篇文章主要介紹了C#使用MySQLConnectorNet和MySQLDriverCS操作MySQL的方法,相比普通方法能夠在Windows下簡化很多操作步驟,需要的朋友可以參考下2016-04-04
SpringBoot中@EnableAutoConfiguration注解的實現(xiàn)
Spring Boot@EnableAutoConfiguration是一個強大的工具,可以簡化配置過程,從而實現(xiàn)快速開發(fā),本文主要介紹了SpringBoot中@EnableAutoConfiguration注解的實現(xiàn),感興趣的可以了解一下2024-01-01
Java微服務(wù)實戰(zhàn)項目尚融寶接口創(chuàng)建詳解
這篇文章主要介紹了Java微服務(wù)實戰(zhàn)項目尚融寶的接口創(chuàng)建流程,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-08-08

