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

在SpringBoot中配置日期格式化的方法詳解

 更新時(shí)間:2023年10月20日 08:56:03   作者:深碼青年  
通常情況下,發(fā)起一個(gè) Http 請求,Spring Boot 會根據(jù)請求路徑映射到指定 Controller 上的某個(gè)方法的參數(shù)上,接著,Spring 會自動進(jìn)行類型轉(zhuǎn)換,對于日期類型的參數(shù),Spring 默認(rèn)是沒有配置如何將字符串轉(zhuǎn)換成日期類型的,本文將給大家介紹在SpringBoot中配置日期格式化的方法

我們先了解下,為什么需要配置日期格式化?

通常情況下,發(fā)起一個(gè) Http 請求,Spring Boot 會根據(jù)請求路徑映射到指定 Controller 上的某個(gè)方法的參數(shù)上,接著,Spring 會自動進(jìn)行類型轉(zhuǎn)換。

對于日期類型的參數(shù),Spring 默認(rèn)是沒有配置如何將字符串轉(zhuǎn)換成日期類型的

未配置日期格式化會如何?

我們新建一個(gè) Web 項(xiàng)目,并定義一個(gè)接口:

package site.exception.springbootdateformat.controller;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import site.exception.springbootdateformat.entity.User;

/**
 * @time 21:03
 * @discription
 **/
@RestController
public class UserController {

    /**
     * 定義一個(gè)創(chuàng)建用戶的接口
     * @param user
     * @return
     */
    @PostMapping("/user")
    public String createUser(User user) {
        // 打印創(chuàng)建時(shí)間
        System.out.println(user.getCreateTime().toString());
        return "Create user success !";
    }
}

User.java:

package site.exception.springbootdateformat.entity;

import java.io.Serializable;
import java.util.Date;

/**
 * @time 21:00
 * @discription
 **/
public class User implements Serializable {
    /**
     * 用戶名
     */
    private String username;
    /**
     * 密碼
     */
    private String password;
    /**
     * 創(chuàng)建時(shí)間
     */
    private Date createTime;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }
}

用 Postman 工具發(fā)起對該接口的表單請求,看看結(jié)果:

服務(wù)報(bào)錯(cuò),無法將字符串類型轉(zhuǎn)換成 java.util.Date 類型。

配置日期格式化

要讓 Spring Boot 能夠按照指定的格式進(jìn)行日期類型轉(zhuǎn)換,需要做以下步驟:

  • 定義一個(gè) MvcConfig 類,讓其實(shí)現(xiàn) WebMvcConfigurer 接口;
  • 重寫 addFormatters 方法;
  • 添加一個(gè) DateFormatter;
package site.exception.springbootdateformat.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
import org.springframework.format.datetime.DateFormatter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;


/**
 * @author www.exception.site (exception 教程網(wǎng))
 * @date 2019/2/16
 * @time 20:55
 * @discription
 **/
@Configuration
public class MvcConfig implements WebMvcConfigurer {

    /**
     * 配置日期格式化
     * @param registry
     */
    @Override
    public void addFormatters(FormatterRegistry registry) {
        registry.addFormatter(new DateFormatter("yyyy-MM-dd HH:mm:ss"));
    }
}

接下來,再次以表單的形式訪問 /user 接口,看看效果:

正確輸出 Create user Success !  接口,并且控制臺,也打印了 createTime 值,說明 Spring Boot 已經(jīng)自動幫我們做了轉(zhuǎn)換操作:

至此,如何在 Spring Boot 中配置全局的日期格式化就完成了!

注意:本節(jié)中的轉(zhuǎn)換僅支持表單形式請求,不支持 application/json 的請求方式!

以上就是在SpringBoot中配置日期格式化的方法詳解的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot日期格式化的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • java中Unsafe的使用講解

    java中Unsafe的使用講解

    這篇文章主要介紹了java中Unsafe的使用講解,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-11-11
  • 解決Java壓縮zip異常java.util.zip.ZipException:duplicate entry:問題

    解決Java壓縮zip異常java.util.zip.ZipException:duplicate entry

    這篇文章主要介紹了解決Java壓縮zip異常java.util.zip.ZipException:duplicate entry:問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-12-12
  • Spring中@PropertySource配置的用法

    Spring中@PropertySource配置的用法

    這篇文章主要介紹了Spring中@PropertySource配置的用法,@PropertySource 和 @Value
    組合使用,可以將自定義屬性文件中的屬性變量值注入到當(dāng)前類的使用@Value注解的成員變量中,需要的朋友可以參考下
    2023-11-11
  • Java根據(jù)URL下載文件到本地的2種方式(大型文件與小型文件)

    Java根據(jù)URL下載文件到本地的2種方式(大型文件與小型文件)

    這篇文章主要給大家介紹了關(guān)于Java根據(jù)URL下載文件到本地的2種方式,分別是大型文件與小型文件,避免內(nèi)存溢出OOM,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2024-01-01
  • Spring中Bean的生命周期實(shí)例解析

    Spring中Bean的生命周期實(shí)例解析

    這篇文章主要介紹了Spring中Bean的生命周期實(shí)例解析,我們定義一個(gè)自定義的MySpringBeanPostProcessor,主要是重寫了BeanPostProcessor接口的postProcessBeforeInitialization與postProcessAfterInitialization方法,需要的朋友可以參考下
    2023-12-12
  • springboot+zookeeper實(shí)現(xiàn)分布式鎖的示例代碼

    springboot+zookeeper實(shí)現(xiàn)分布式鎖的示例代碼

    本文主要介紹了springboot+zookeeper實(shí)現(xiàn)分布式鎖的示例代碼,文中根據(jù)實(shí)例編碼詳細(xì)介紹的十分詳盡,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-03-03
  • IntelliJ IDEA 熱部署插件JRebel的使用

    IntelliJ IDEA 熱部署插件JRebel的使用

    這篇文章主要介紹了IntelliJ IDEA 熱部署插件JRebel的使用,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-12-12
  • 零基礎(chǔ)搭建boot+MybatisPlus的詳細(xì)教程

    零基礎(chǔ)搭建boot+MybatisPlus的詳細(xì)教程

    這篇文章主要介紹了零基礎(chǔ)搭建boot+MybatisPlus,首先需要創(chuàng)建數(shù)據(jù)庫表和創(chuàng)建boot項(xiàng)目使用mybatisplus操作數(shù)據(jù)庫,本文通過示例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2022-03-03
  • SpringBoot配置web訪問H2的方法

    SpringBoot配置web訪問H2的方法

    這篇文章主要介紹了SpringBoot配置web訪問H2的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-08-08
  • spring mvc實(shí)現(xiàn)登錄賬號單瀏覽器登錄

    spring mvc實(shí)現(xiàn)登錄賬號單瀏覽器登錄

    這篇文章主要為大家詳細(xì)介紹了spring mvc實(shí)現(xiàn)登錄賬號單瀏覽器登錄,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-04-04

最新評論

泽普县| 和龙市| 泗水县| 巨野县| 康定县| 德江县| 石林| 澳门| 墨脱县| 三河市| 涞源县| 曲周县| 天长市| 凌源市| 松桃| 县级市| 桂阳县| 上杭县| 台安县| 屯门区| 新营市| 尖扎县| 蓬溪县| 长海县| 资阳市| 左权县| 钦州市| 杭锦旗| 桃江县| 天津市| 博爱县| 静宁县| 秭归县| 碌曲县| 德州市| 德钦县| 镇平县| 勐海县| 鄯善县| 阿鲁科尔沁旗| 阳东县|