最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

springboot?接收LocalDateTime方式

 更新時間:2022年07月04日 11:44:49   作者:雨夜歸人93  
這篇文章主要介紹了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>

添加一個配置類

@Configuration
public class DateConfiguration {
?? ?@Bean
?? ?public ObjectMapper objectMapper(){
?? ??? ?return new ObjectMapper()
?? ??? ??? ??? ?.registerModule(new ParameterNamesModule())
?? ??? ??? ??? ?.registerModule(new Jdk8Module())
?? ??? ??? ??? ?.registerModule(new JavaTimeModule());
?? ?}
}

基礎(chǔ)配置完成,使用時在對應(yīng)字段添加@DateTimeFormat 進行反序列化或者@JsonFormat序列化。

2.非json請求時間戳轉(zhuǎn)換

本類型指在前端非json請求,傳遞參數(shù)為時間戳,然后轉(zhuǎn)為LocalDateTime。

可在上文基礎(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請求時間戳轉(zhuǎn)換

本類型指在前端json請求,傳遞參數(shù)為時間戳,然后轉(zhuǎn)為LocalDateTime。

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.序列化擴展

有時返回前端數(shù)據(jù),要包裝下信息(比如返回全路徑地址及某些參數(shù)),直接硬編碼不夠優(yōu)雅。這時可以通過序列化操作,實現(xiàn)ContextualSerializer接口,要進行一些額外操作,。

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();
?? ?}
}

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • springboot接口參數(shù)校驗JSR303的實現(xiàn)

    springboot接口參數(shù)校驗JSR303的實現(xiàn)

    本文主要介紹了springboot接口參數(shù)校驗JSR303的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-08-08
  • Java中CompletableFuture?的詳細(xì)介紹

    Java中CompletableFuture?的詳細(xì)介紹

    這篇文章主要介紹了Java中的CompletableFuture,通過創(chuàng)建?CompletableFuture?的對象的工廠方法展開詳細(xì)的內(nèi)容介紹,需要的小伙伴可以參考一下
    2022-05-05
  • JAVA面試題 簡談你對synchronized關(guān)鍵字的理解

    JAVA面試題 簡談你對synchronized關(guān)鍵字的理解

    這篇文章主要介紹了JAVA面試題 請談?wù)勀銓ychronized關(guān)鍵字的理解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-07-07
  • RabbitMQ 最常用的三大模式實例解析

    RabbitMQ 最常用的三大模式實例解析

    這篇文章主要介紹了RabbitMQ 最常用的三大模式實例解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-12-12
  • Java實現(xiàn)拓?fù)渑判虻氖纠a

    Java實現(xiàn)拓?fù)渑判虻氖纠a

    這篇文章我們要講的是拓?fù)渑判?,這是一個針對有向無環(huán)圖的算法,主要是為了解決前驅(qū)后繼的關(guān)系,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2022-05-05
  • 一文教會你使用Java中的枚舉Enmu

    一文教會你使用Java中的枚舉Enmu

    枚舉是 Java 中的一種特殊類型,它用于表示一組固定值,這篇文章就是來和大家講講枚舉的作用與具體使用,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2023-06-06
  • java讀取某個文件夾下的所有文件實例代碼

    java讀取某個文件夾下的所有文件實例代碼

    這篇文章主要介紹了java讀取某個文件夾下的所有文件實例代碼的相關(guān)資料,需要的朋友可以參考下
    2017-03-03
  • Spring Boot集成教程之異步調(diào)用Async

    Spring Boot集成教程之異步調(diào)用Async

    在項目中,當(dāng)訪問其他人的接口較慢或者做耗時任務(wù)時,不想程序一直卡在耗時任務(wù)上,想程序能夠并行執(zhí)行,我們可以使用多線程來并行的處理任務(wù),也可以使用spring提供的異步處理方式@Async。需要的朋友們下面來一起看看吧。
    2018-03-03
  • SpringCloud集成Sleuth和Zipkin的思路講解

    SpringCloud集成Sleuth和Zipkin的思路講解

    Zipkin 是 Twitter 的一個開源項目,它基于 Google Dapper 實現(xiàn),它致力于收集服務(wù)的定時數(shù)據(jù),以及解決微服務(wù)架構(gòu)中的延遲問題,包括數(shù)據(jù)的收集、存儲、查找和展現(xiàn),這篇文章主要介紹了SpringCloud集成Sleuth和Zipkin,需要的朋友可以參考下
    2022-11-11
  • Java數(shù)據(jù)結(jié)構(gòu)之順序表和鏈表精解

    Java數(shù)據(jù)結(jié)構(gòu)之順序表和鏈表精解

    我在學(xué)習(xí)完順序表后一直對順序表和鏈表的概念存在一些疑問,這里給出一些分析和看法,通讀本篇對大家的學(xué)習(xí)或工作具有一定的價值,需要的朋友可以參考下
    2021-09-09

最新評論

龙川县| 辽阳县| 安岳县| 密山市| 宁强县| 利辛县| 浠水县| 南岸区| 余江县| 甘泉县| 财经| 响水县| 罗田县| 普格县| 麦盖提县| 宾阳县| 宜城市| 宝兴县| 克什克腾旗| 平塘县| 绵竹市| 冷水江市| 合作市| 五家渠市| 马边| 乡城县| 昭觉县| 察哈| 邮箱| 安多县| 固始县| 呼伦贝尔市| 亚东县| 教育| 扎赉特旗| 徐闻县| 西丰县| 湖南省| 卓尼县| 子长县| 扎赉特旗|