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

Spring?@DateTimeFormat日期格式化時注解場景分析

 更新時間:2023年05月25日 16:15:17   作者:zhuzicc  
這篇文章主要介紹了Spring?@DateTimeFormat日期格式化時注解場景分析,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下

總結(jié)寫前面

關(guān)于它 @DateTimeFormat

  • 可以接收解析前端傳入字符時間數(shù)據(jù);
  • 不能格式化接收的字符時間類型數(shù)據(jù),需要的轉(zhuǎn)換格式得配置;
  • 入?yún)⒏袷奖仨毰c后端注解格式保持一致,否則會報錯;

為什么用

場景:跟前端交互時,接收字符類型的時間值,就需要使用 @DateTimeFormat 注解來解析,否則就會報錯;

@RestController
@RequestMapping("/demo")
public class DemoTestController {
    @PostMapping("/testOne")
    public DemoTest testOne(DemoTest demoTest){
        return demoTest;
    }
}
@Data
public class DemoTest {
    private Date nowTime;
}

請求示例結(jié)果:

Resolved [org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 1 errors<EOL>Field error in object 'demoTest' on field 'nowTime': rejected value [2022-11-20 16:42:26,2022-11-20 16:42:01]; codes [typeMismatch.demoTest.nowTime,typeMismatch.nowTime,typeMismatch.java.util.Date,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [demoTest.nowTime,nowTime]; arguments []; default message [nowTime]]; default message [Failed to convert property value of type 'java.lang.String[]' to required type 'java.util.Date' for property 'nowTime'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [java.util.Date] for value '2022-11-20 16:42:26'; nested exception is java.lang.IllegalArgumentException]]

怎么用

場景一

接收非 JSON 格式請求參數(shù)。

@RestController
@RequestMapping("/demo")
public class DemoTestController {
     @PostMapping("/testOne")
    public DemoTest testOne(DemoTest demoTest){
        return demoTest;
    }
}
@Data
public class DemoTest {
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date nowTime;
}

請求示例結(jié)果:

  • 請求:POST
  • 數(shù)據(jù)格式:form-data

在這里插入圖片描述

從結(jié)果可以看出,@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") 可以保證接收解析前端傳入的字符時間參數(shù),但是并不能完成時間格式化操作,如果需要獲取想要的時間格式,是需要自己手動轉(zhuǎn)換的。

場景二

接收 JSON 格式請求數(shù)據(jù),與場景一的區(qū)別是請求的數(shù)據(jù)格式:

  • 場景一:form-data
  • 場景二:JSON
@RestController
@RequestMapping("/demo")
public class DemoTestController {
    @PostMapping("/testTwo")
    public DemoTest testTwo(DemoTest demoTest){
        return demoTest;
    }
}
@Data
public class DemoTest {
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date nowTime;
}

請求示例結(jié)果:

  • 請求:POST
  • 數(shù)據(jù)格式:JSON

在這里插入圖片描述

從結(jié)果可以看出,返回數(shù)據(jù) nowTime 是空的,因為這里的Controller層沒有使用 @RequestBody 去接收 JSON 格式的數(shù)據(jù),而 Spring 默認(rèn)的轉(zhuǎn)換器類型是不包含 JSON 的(有興趣的可以看下 org.springframework.core.convert.support 包,這里面包含Spring支持的默認(rèn)轉(zhuǎn)換器)。

場景三

場景三跟場景二的區(qū)別就是,在 Controller 層方法入?yún)⑴浜鲜褂?@RequestBody 去接收 JSON 格式,使用該注解會自動調(diào)用對應(yīng)的JSON轉(zhuǎn)換器。

@RestController
@RequestMapping("/demo")
public class DemoTestController {
    @PostMapping("/testThree")
    public DemoTest testThree(@RequestBody DemoTest demoTest){
        return demoTest;
    }
}
@Data
public class DemoTest {
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date nowTime;
}

請求示例結(jié)果:

  • 請求:POST
  • 數(shù)據(jù)格式:JSON

在這里插入圖片描述

這里可以看到,請求報錯400,導(dǎo)致400的原因比較多,這里只說明一下場景三,場景三中使用 @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") 注解格式與請求入?yún)⒏袷讲灰恢拢詴?dǎo)致請求報錯;

在這里插入圖片描述

大概意思就是說,Spring 框架在嘗試轉(zhuǎn)換參數(shù)的過程中,沒有找到合適接收格式導(dǎo)致轉(zhuǎn)換失敗。(注意!注意!注意!講三遍,所以前端入?yún)⒏袷奖仨毰c后端約定格式保持一致,否則會報錯)。

場景四

場景四的目的是為了解決場景一中時間格式化的問題。

關(guān)于 @JsonFormat 注解,可以看看我的另一篇blog中有做分享,感興趣的大佬可以去看看,附上傳送門:@JsonFormat 和 @DateTimeFormat 時間格式化注解詳解(不看血虧)

@RestController
@RequestMapping("/demo")
public class DemoTestController {
    @PostMapping("/testThree")
    public DemoTest testThree(@RequestBody DemoTest demoTest){
        return demoTest;
    }
}
@Data
public class DemoTest {
    @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date nowTime;
}

請求示例結(jié)果:

  • 請求:POST
  • 數(shù)據(jù)格式:form-data

在這里插入圖片描述

場景五

方式一

針對場景四的數(shù)據(jù)請求格式是 form-data,場景五來說明 JSON 同樣適用。

@RestController
@RequestMapping("/demo")
public class DemoTestController {
    @PostMapping("/testThree")
    public DemoTest testThree(@RequestBody DemoTest demoTest){
        return demoTest;
    }
}
@Data
public class DemoTest {
    @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date nowTime;
}

請求示例結(jié)果:

  • 請求:POST
  • 數(shù)據(jù)格式:JSON

在這里插入圖片描述

方式二

可以繼承 Spring 提供的org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer 來進(jìn)行全局配置。

@RestController
@RequestMapping("/demo")
public class DemoTestController {
    @PostMapping("/testThree")
    public DemoTest testThree(@RequestBody DemoTest demoTest){
        return demoTest;
    }
}
@Data
public class DemoTest {
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date nowTime;
}
@Configuration
public class CustomsDateConvert implements Jackson2ObjectMapperBuilderCustomizer {
    @Override
    public void customize(Jackson2ObjectMapperBuilder jacksonObjectMapperBuilder) {
        String dateFormat = "yyyy-MM-dd HH";
        // 針對于Date類型,文本格式化
        jacksonObjectMapperBuilder.simpleDateFormat(dateFormat);
        // 針對于JDK新時間類。序列化時帶有T的問題,自定義格式化字符串
        JavaTimeModule javaTimeModule = new JavaTimeModule();
        javaTimeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(dateFormat)));
        javaTimeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern(dateFormat)));
        jacksonObjectMapperBuilder.modules(javaTimeModule);
    }
}
/**
 * 解決Jackson2ObjectMapperBuilderCustomizer失效問題
 */
@Configuration
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET)
public class ConvertConfiguration implements WebMvcConfigurer {
    @Autowired(required = false)
    private MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter;
    @Override
    public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        converters.removeIf(converter -> converter instanceof MappingJackson2HttpMessageConverter);
        if (Objects.isNull(mappingJackson2HttpMessageConverter)) {
            converters.add(0, new MappingJackson2HttpMessageConverter());
        } else {
            converters.add(0, mappingJackson2HttpMessageConverter);
        }
    }
}

請求示例結(jié)果:

  • 請求:POST
  • 數(shù)據(jù)格式:JSON

在這里插入圖片描述

到此這篇關(guān)于Spring @DateTimeFormat日期格式化時注解場景分析的文章就介紹到這了,更多相關(guān)Spring @DateTimeFormat日期格式化內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Java正則表達(dá)式的基本用法和實(shí)例大全

    Java正則表達(dá)式的基本用法和實(shí)例大全

    這篇文章主要給大家介紹了關(guān)于Java正則表達(dá)式的基本用法和實(shí)例的相關(guān)資料,大家在使用Java正則表達(dá)式的時候可查閱這篇文章,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-03-03
  • SpringBoot使用ApplicationEvent&Listener完成業(yè)務(wù)解耦

    SpringBoot使用ApplicationEvent&Listener完成業(yè)務(wù)解耦

    這篇文章主要介紹了SpringBoot使用ApplicationEvent&Listener完成業(yè)務(wù)解耦示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-05-05
  • dom4j創(chuàng)建和解析xml文檔的實(shí)現(xiàn)方法

    dom4j創(chuàng)建和解析xml文檔的實(shí)現(xiàn)方法

    下面小編就為大家?guī)硪黄猟om4j創(chuàng)建和解析xml文檔的實(shí)現(xiàn)方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-06-06
  • 關(guān)于Spring Actuator的簡單測試

    關(guān)于Spring Actuator的簡單測試

    這篇文章主要介紹了關(guān)于Spring Actuator的簡單測試,Spring-Actuator 是spring下的程序監(jiān)控系統(tǒng),通過簡單的配置就可以查看程序的相關(guān)信息,本文提供了相關(guān)配置,需要的朋友可以參考下
    2023-10-10
  • Java數(shù)組(Array)最全匯總(中篇)

    Java數(shù)組(Array)最全匯總(中篇)

    這篇文章主要介紹了Java數(shù)組(Array)最全匯總(中篇),本文章內(nèi)容詳細(xì),通過案例可以更好的理解數(shù)組的相關(guān)知識,本模塊分為了三部分,本次為中篇,需要的朋友可以參考下
    2023-01-01
  • SpringBoot實(shí)現(xiàn)API接口限流

    SpringBoot實(shí)現(xiàn)API接口限流

    訪問速率限制是一種API訪問限制的策略,它限制客戶端在一定時間內(nèi)調(diào)用API的次數(shù),本文就來介紹一下Spring Boot中使用Bucket4j實(shí)現(xiàn)限流,感興趣的可以了解一下
    2025-08-08
  • Springboot 整合RabbitMq(用心看完這一篇就夠了)

    Springboot 整合RabbitMq(用心看完這一篇就夠了)

    這篇文章主要介紹了Springboot 整合RabbitMq(用心看完這一篇就夠了),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-12-12
  • Java關(guān)鍵字final、static使用總結(jié)

    Java關(guān)鍵字final、static使用總結(jié)

    final方法不能被子類的方法覆蓋,但可以被繼承。用static修飾的代碼塊表示靜態(tài)代碼塊,當(dāng)Java虛擬機(jī)(JVM)加載類時,就會執(zhí)行該代碼塊,下面通過本文給大家分享Java關(guān)鍵字final、static使用總結(jié),感興趣的朋友一起看看吧
    2017-07-07
  • SpringBoot整合EasyExcel?3.x的完整示例

    SpringBoot整合EasyExcel?3.x的完整示例

    EasyExcel 是一個基于 Java 的、快速、簡潔、解決大文件內(nèi)存溢出的 Excel 處理工具,它能讓你在不用考慮性能、內(nèi)存的等因素的情況下,快速完成 Excel 的讀、寫等功能,這篇文章主要介紹了SpringBoot整合EasyExcel3.x的過程,需要的朋友可以參考下
    2023-07-07
  • Spring Boot 配置MySQL數(shù)據(jù)庫重連的操作方法

    Spring Boot 配置MySQL數(shù)據(jù)庫重連的操作方法

    這篇文章主要介紹了Spring Boot 配置MySQL數(shù)據(jù)庫重連的操作方法,需要的朋友可以參考下
    2018-04-04

最新評論

栾川县| 芦山县| 松潘县| 聂拉木县| 张家港市| 萍乡市| 景洪市| 米易县| 焦作市| 大石桥市| 扎兰屯市| 千阳县| 合山市| 思茅市| 新泰市| 星子县| 河津市| 昭觉县| 夏津县| SHOW| 澄江县| 新安县| 新晃| 绵竹市| 铁力市| 会理县| 平原县| 玛多县| 乃东县| 新巴尔虎左旗| 台中市| 新密市| 厦门市| 金堂县| 巨鹿县| 白朗县| 镶黄旗| 长寿区| 策勒县| 北宁市| 漳州市|