springboot?接收LocalDateTime方式
本文基于jdk8。
1.標(biāo)準(zhǔn)日期格式轉(zhuǎn)換
本類型是指前端傳遞類似"yyyy-MM-dd HH:mm:ss"格式字符串,后端以 LocalDateTime類型接收。
spring默認(rèn)的使用jackson,故添加maven依賴,可參考官方文檔:
<dependency> ? ? <groupId>com.fasterxml.jackson.module</groupId> ? ? <artifactId>jackson-module-parameter-names</artifactId> </dependency> <dependency> ? ? <groupId>com.fasterxml.jackson.datatype</groupId> ? ? <artifactId>jackson-datatype-jdk8</artifactId> </dependency> <dependency> ? ? <groupId>com.fasterxml.jackson.datatype</groupId> ? ? <artifactId>jackson-datatype-jsr310</artifactId> </dependency>
添加一個(gè)配置類
@Configuration
public class DateConfiguration {
?? ?@Bean
?? ?public ObjectMapper objectMapper(){
?? ??? ?return new ObjectMapper()
?? ??? ??? ??? ?.registerModule(new ParameterNamesModule())
?? ??? ??? ??? ?.registerModule(new Jdk8Module())
?? ??? ??? ??? ?.registerModule(new JavaTimeModule());
?? ?}
}基礎(chǔ)配置完成,使用時(shí)在對(duì)應(yīng)字段添加@DateTimeFormat 進(jìn)行反序列化或者@JsonFormat序列化。
2.非json請(qǐng)求時(shí)間戳轉(zhuǎn)換
本類型指在前端非json請(qǐng)求,傳遞參數(shù)為時(shí)間戳,然后轉(zhuǎn)為L(zhǎng)ocalDateTime。
可在上文基礎(chǔ)上添加配置,示例如下:
@Configuration
public class DateConfiguration {
?? ?@Bean
?? ?public ObjectMapper objectMapper(){
?? ??? ?return new ObjectMapper()
?? ??? ??? ??? ?.registerModule(new ParameterNamesModule())
?? ??? ??? ??? ?.registerModule(new Jdk8Module())
?? ??? ??? ??? ?.registerModule(new JavaTimeModule());
?? ?}
? ? @Bean
? ? public Formatter<LocalDateTime> localDateTimeFormatter() {
? ? ? ? return new Formatter<LocalDateTime>() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public LocalDateTime parse(String text, Locale locale) ?{
? ? ? ? ? ? ? ? return Instant
? ? ? ? ? ? ? ? ? ? ? ? .ofEpochMilli(Long.parseLong(text))
? ? ? ? ? ? ? ? ? ? ? ? .atZone(ZoneOffset.ofHours(8))
? ? ? ? ? ? ? ? ? ? ? ? .toLocalDateTime();
? ? ? ? ? ? }
? ? ? ? ? ? @Override
? ? ? ? ? ? public String print(LocalDateTime object, Locale locale) {
? ? ? ? ? ? ? ? return DateTimeFormatter.ISO_DATE.format(object);
? ? ? ? ? ? }
? ? ? ? };
? ? }
}3.json請(qǐng)求時(shí)間戳轉(zhuǎn)換
本類型指在前端json請(qǐng)求,傳遞參數(shù)為時(shí)間戳,然后轉(zhuǎn)為L(zhǎng)ocalDateTime。
1.自定義解析注解
@Retention (RetentionPolicy.RUNTIME)
@JacksonAnnotationsInside
@JsonDeserialize(using = CustomLocalDateTimeDeserializer.class)
public @interface StampToLocalDateTime {
}2.自定義解析類
public class CustomLocalDateTimeDeserializer extends JsonDeserializer<LocalDateTime> ?{
? ? @Override
? ? public LocalDateTime deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException{
? ? ? ? if (StringUtils.isEmpty(jsonParser.getText())) {
? ? ? ? ? ? return null;
? ? ? ? }
? ? ? ? return Instant
? ? ? ? ? ? ? ? .ofEpochMilli(Long.parseLong(jsonParser.getText()))
? ? ? ? ? ? ? ? .atZone(ZoneOffset.ofHours(8))
? ? ? ? ? ? ? ? .toLocalDateTime();
? ? }
}在需要使用的字段添加@StampToLocalDateTime即可。示例如下
public class DemoReq ?{
?? ?
?? ?@StampToLocalDateTime
? ? private LocalDateTime signTime;
}4.序列化擴(kuò)展
有時(shí)返回前端數(shù)據(jù),要包裝下信息(比如返回全路徑地址及某些參數(shù)),直接硬編碼不夠優(yōu)雅。這時(shí)可以通過(guò)序列化操作,實(shí)現(xiàn)ContextualSerializer接口,要進(jìn)行一些額外操作,。
1.自定義注解
@Retention (RetentionPolicy.RUNTIME)
@JacksonAnnotationsInside
@JsonSerializer(using = FullUrlSerializer.class)
public @interface FullUrl {
??? ?String value() default "";
}2.自定義序列類
public class FullUrlSerializer extends JsonSerializer<String> implements ContextualSerializer {
? ? private String params;
? ? @Value("${domain}")
? ? private String domain;
? ? public FullUrlSerializer() {
? ? }
? ? public FullUrlSerializer(String params) {
? ? ? ? this.params = params;
? ? }
? ? @Override
? ? public JsonSerializer<?> createContextual(SerializerProvider prov, BeanProperty property) throws JsonMappingException {
? ? ? ? if (property == null) {
? ? ? ? ? ? return prov.findNullValueSerializer(null);
? ? ? ? }
? ? ? ? if (Objects.equals(property.getType().getRawClass(), String.class)) {
? ? ? ? ? ? FullUrl fullUrl = property.getAnnotation(FullUrl.class);
? ? ? ? ? ? if (fullUrl == null) {
? ? ? ? ? ? ? ? fullUrl = property.getContextAnnotation(FullUrl.class);
? ? ? ? ? ? }
? ? ? ? ? ? if (fullUrl != null) {
? ? ? ? ? ? ? ? return new FullUrlSerializer(fullUrl.value());
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? return prov.findValueSerializer(property.getType(), property);
? ? }
? ? @Override
? ? public void serialize(String value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
? ? ? ? String url = "";
? ? ? ? if (!StringUtils.isEmpty(value)) {
? ? ? ? ? ? url = domain.concat(value);
? ? ? ? ? ? if (!StringUtils.isEmpty(params)) {
? ? ? ? ? ? ? ? url.contains(params);
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? gen.writeString (url);
? ? }
}5.swagger支持
要使swagger支持LocalDateTime等類型可以設(shè)置directModelSubstitute,示例如下:
@Configuration
public abstract class SwaggerConfiguration {
?? ?@Bean
?? ?public Docket createRestApi() {
?? ??? ?return new Docket(DocumentationType.SWAGGER_2)
?? ??? ??? ??? ?.directModelSubstitute(LocalDateTime.class,String.class)
?? ??? ??? ??? ?.directModelSubstitute(LocalDate.class, String.class)
?? ??? ??? ??? ?.directModelSubstitute(LocalTime.class, String.class)
?? ??? ??? ??? ?.directModelSubstitute(ZonedDateTime.class,String.class)
?? ??? ??? ??? ?.build();
?? ?}
}以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- SpringBoot接收LocalDateTime參數(shù)的方式
- Springboot中LocalDateTime對(duì)象返回給前端格式化解決方案
- springboot中如何配置LocalDateTime JSON返回時(shí)間戳
- SpringBoot?LocalDateTime格式轉(zhuǎn)換方案詳解(前端入?yún)?
- SpringBoot如何對(duì)LocalDateTime進(jìn)行格式化并解析
- Springboot?格式化LocalDateTime的方法
- SpringBoot整合Mybatis?LocalDateTime?映射失效的解決
- springboot mybatis里localdatetime序列化問(wèn)題的解決
- SpringBoot整合LocalDateTime的過(guò)程
相關(guān)文章
springboot接口參數(shù)校驗(yàn)JSR303的實(shí)現(xiàn)
本文主要介紹了springboot接口參數(shù)校驗(yàn)JSR303的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08
Java中CompletableFuture?的詳細(xì)介紹
這篇文章主要介紹了Java中的CompletableFuture,通過(guò)創(chuàng)建?CompletableFuture?的對(duì)象的工廠方法展開詳細(xì)的內(nèi)容介紹,需要的小伙伴可以參考一下2022-05-05
JAVA面試題 簡(jiǎn)談你對(duì)synchronized關(guān)鍵字的理解
這篇文章主要介紹了JAVA面試題 請(qǐng)談?wù)勀銓?duì)Sychronized關(guān)鍵字的理解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-07-07
Java實(shí)現(xiàn)拓?fù)渑判虻氖纠a
這篇文章我們要講的是拓?fù)渑判?,這是一個(gè)針對(duì)有向無(wú)環(huán)圖的算法,主要是為了解決前驅(qū)后繼的關(guān)系,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2022-05-05
java讀取某個(gè)文件夾下的所有文件實(shí)例代碼
這篇文章主要介紹了java讀取某個(gè)文件夾下的所有文件實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2017-03-03
Spring Boot集成教程之異步調(diào)用Async
在項(xiàng)目中,當(dāng)訪問(wèn)其他人的接口較慢或者做耗時(shí)任務(wù)時(shí),不想程序一直卡在耗時(shí)任務(wù)上,想程序能夠并行執(zhí)行,我們可以使用多線程來(lái)并行的處理任務(wù),也可以使用spring提供的異步處理方式@Async。需要的朋友們下面來(lái)一起看看吧。2018-03-03
SpringCloud集成Sleuth和Zipkin的思路講解
Zipkin 是 Twitter 的一個(gè)開源項(xiàng)目,它基于 Google Dapper 實(shí)現(xiàn),它致力于收集服務(wù)的定時(shí)數(shù)據(jù),以及解決微服務(wù)架構(gòu)中的延遲問(wèn)題,包括數(shù)據(jù)的收集、存儲(chǔ)、查找和展現(xiàn),這篇文章主要介紹了SpringCloud集成Sleuth和Zipkin,需要的朋友可以參考下2022-11-11
Java數(shù)據(jù)結(jié)構(gòu)之順序表和鏈表精解
我在學(xué)習(xí)完順序表后一直對(duì)順序表和鏈表的概念存在一些疑問(wèn),這里給出一些分析和看法,通讀本篇對(duì)大家的學(xué)習(xí)或工作具有一定的價(jià)值,需要的朋友可以參考下2021-09-09

