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

Spring?@DateTimeFormat日期格式化時(shí)注解場(chǎng)景分析

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

總結(jié)寫前面

關(guān)于它 @DateTimeFormat

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

為什么用

場(chǎng)景:跟前端交互時(shí),接收字符類型的時(shí)間值,就需要使用 @DateTimeFormat 注解來(lái)解析,否則就會(huì)報(bào)錯(cuò);

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

請(qǐng)求示例結(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]]

怎么用

場(chǎng)景一

接收非 JSON 格式請(qǐng)求參數(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;
}

請(qǐng)求示例結(jié)果:

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

在這里插入圖片描述

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

場(chǎng)景二

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

  • 場(chǎng)景一:form-data
  • 場(chǎng)景二: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;
}

請(qǐng)求示例結(jié)果:

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

在這里插入圖片描述

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

場(chǎng)景三

場(chǎng)景三跟場(chǎng)景二的區(qū)別就是,在 Controller 層方法入?yún)⑴浜鲜褂?@RequestBody 去接收 JSON 格式,使用該注解會(huì)自動(dòng)調(diào)用對(duì)應(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;
}

請(qǐng)求示例結(jié)果:

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

在這里插入圖片描述

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

在這里插入圖片描述

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

場(chǎng)景四

場(chǎng)景四的目的是為了解決場(chǎng)景一中時(shí)間格式化的問(wèn)題。

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

@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;
}

請(qǐng)求示例結(jié)果:

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

在這里插入圖片描述

場(chǎng)景五

方式一

針對(duì)場(chǎng)景四的數(shù)據(jù)請(qǐng)求格式是 form-data,場(chǎng)景五來(lái)說(shuō)明 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;
}

請(qǐng)求示例結(jié)果:

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

在這里插入圖片描述

方式二

可以繼承 Spring 提供的org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer 來(lái)進(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";
        // 針對(duì)于Date類型,文本格式化
        jacksonObjectMapperBuilder.simpleDateFormat(dateFormat);
        // 針對(duì)于JDK新時(shí)間類。序列化時(shí)帶有T的問(wèn)題,自定義格式化字符串
        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失效問(wèn)題
 */
@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);
        }
    }
}

請(qǐng)求示例結(jié)果:

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

在這里插入圖片描述

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

相關(guān)文章

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

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

    這篇文章主要給大家介紹了關(guān)于Java正則表達(dá)式的基本用法和實(shí)例的相關(guān)資料,大家在使用Java正則表達(dá)式的時(shí)候可查閱這篇文章,文中通過(guò)實(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ī)?lái)一篇dom4j創(chuàng)建和解析xml文檔的實(shí)現(xiàn)方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-06-06
  • 關(guān)于Spring Actuator的簡(jiǎn)單測(cè)試

    關(guān)于Spring Actuator的簡(jiǎn)單測(cè)試

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

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

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

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

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

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

    這篇文章主要介紹了Springboot 整合RabbitMq(用心看完這一篇就夠了),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(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)加載類時(shí),就會(huì)執(zhí)行該代碼塊,下面通過(guò)本文給大家分享Java關(guān)鍵字final、static使用總結(jié),感興趣的朋友一起看看吧
    2017-07-07
  • SpringBoot整合EasyExcel?3.x的完整示例

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

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

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

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

最新評(píng)論

河西区| 东光县| 长岭县| 大邑县| 宾阳县| 宁远县| 溆浦县| 东明县| 仪征市| 霍州市| 特克斯县| 杭锦后旗| 寿光市| 湖北省| 溧阳市| 靖州| 沾益县| 壤塘县| 岳阳市| 太湖县| 隆化县| 高州市| 陈巴尔虎旗| 金湖县| 策勒县| 连城县| 平凉市| 丹江口市| 静乐县| 南充市| 依兰县| 潜山县| 昌宁县| 泰州市| 醴陵市| 迁安市| 宁国市| 高雄县| 赤壁市| 大方县| 怀安县|