欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Spring?Boot?中的?@DateTimeFormat?和?@JsonFormat?的用法及作用詳解

 更新時(shí)間:2024年11月28日 11:41:24   作者:小林想被監(jiān)督學(xué)習(xí)  
本文介紹了SpringBoot中的@DateTimeFormat和@JsonFormat注解的用法,解釋了它們?cè)谔幚砣掌诤蜁r(shí)間數(shù)據(jù)時(shí)的作用,并通過實(shí)例代碼展示了如何在REST控制器中使用這些注解,感興趣的朋友跟隨小編一起看看吧

        在開發(fā) Spring Boot 應(yīng)用時(shí),處理日期和時(shí)間數(shù)據(jù)是一個(gè)常見的需求。Spring Boot 提供了兩個(gè)注解 @DateTimeFormat@JsonFormat 來幫助我們處理這些問題。這兩個(gè)注解分別用于將日期字符串解析為日期對(duì)象以及將日期對(duì)象格式化為字符串。本文將詳細(xì)介紹這兩個(gè)注解的用法及作用,并通過實(shí)例代碼進(jìn)行說明。

@DateTimeFormat 注解

@DateTimeFormat 是一個(gè) Spring 框架提供的注解,用于格式化和解析日期時(shí)間字段。它主要用于將請(qǐng)求參數(shù)或表單數(shù)據(jù)中的日期字符串解析為 Java 的日期對(duì)象。

用法

@DateTimeFormat 可以應(yīng)用于以下數(shù)據(jù)類型:

  • java.util.Date
  • java.util.Calendar
  • java.time.LocalDate
  • java.time.LocalDateTime
  • java.time.ZonedDateTime

該注解的常用屬性包括:

  • pattern:指定日期格式的模式字符串,例如 "yyyy-MM-dd"
  • iso:指定標(biāo)準(zhǔn)的 ISO 日期時(shí)間格式??蛇x值為 DateTimeFormat.ISO.DATE、DateTimeFormat.ISO.TIMEDateTimeFormat.ISO.DATE_TIME。

示例

假設(shè)我們有一個(gè)處理日期的 REST 控制器:

package com.example.demo.controller;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
@RestController
public class DateController {
    @GetMapping("/date")
    public String handleDate(@RequestParam("date") @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate date) {
        return "Parsed date: " + date.format(DateTimeFormatter.ISO_DATE);
    }
    @GetMapping("/datetime")
    public String handleDateTime(@RequestParam("datetime") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime dateTime) {
        return "Parsed datetime: " + dateTime.format(DateTimeFormatter.ISO_DATE_TIME);
    }
}

在上述示例中:

  • /date 端點(diǎn)處理格式為 "yyyy-MM-dd" 的字符串,并將其解析為 LocalDate 對(duì)象。
  • /datetime 端點(diǎn)處理 ISO 標(biāo)準(zhǔn)格式的日期時(shí)間字符串,并將其解析為 LocalDateTime 對(duì)象。

你可以通過以下 URL 測(cè)試這些端點(diǎn):

http://localhost:8080/date?date=2023-10-01
http://localhost:8080/datetime?datetime=2023-10-01T10:15:30

@JsonFormat 注解

@JsonFormat 是 Jackson 庫提供的注解,用于序列化和反序列化 JSON 數(shù)據(jù)中的日期時(shí)間字段。它可以幫助我們將日期時(shí)間對(duì)象格式化為特定的字符串格式,或?qū)⑻囟ǜ袷降淖址馕鰹槿掌跁r(shí)間對(duì)象。

用法

@JsonFormat 可以應(yīng)用于類的字段或方法上。其常用屬性包括:

  • pattern:指定日期時(shí)間格式的模式字符串。
  • shape:指定日期時(shí)間的格式化類型。常用值為 JsonFormat.Shape.STRING。
  • timezone:指定時(shí)區(qū)。

示例

假設(shè)我們有一個(gè)包含日期時(shí)間字段的實(shí)體類,并使用 @JsonFormat 注解格式化日期時(shí)間:

package com.example.demo.model;
import com.fasterxml.jackson.annotation.JsonFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
public class Event {
    private String name;
    @JsonFormat(pattern = "yyyy-MM-dd")
    private LocalDate date;
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "UTC")
    private LocalDateTime dateTime;
    // getters and setters
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public LocalDate getDate() {
        return date;
    }
    public void setDate(LocalDate date) {
        this.date = date;
    }
    public LocalDateTime getDateTime() {
        return dateTime;
    }
    public void setDateTime(LocalDateTime dateTime) {
        this.dateTime = dateTime;
    }
}

接著,我們創(chuàng)建一個(gè) REST 控制器來測(cè)試該實(shí)體類的序列化和反序列化:

package com.example.demo.controller;
import com.example.demo.model.Event;
import org.springframework.web.bind.annotation.*;
import java.time.LocalDate;
import java.time.LocalDateTime;
@RestController
public class EventController {
    @GetMapping("/event")
    public Event getEvent() {
        Event event = new Event();
        event.setName("Spring Boot Workshop");
        event.setDate(LocalDate.of(2023, 10, 1));
        event.setDateTime(LocalDateTime.of(2023, 10, 1, 10, 15, 30));
        return event;
    }
    @PostMapping("/event")
    public String createEvent(@RequestBody Event event) {
        // 持久化邏輯...
        return "Event created: " + event.getName();
    }
}

在上述示例中:

  • /event GET 端點(diǎn)返回一個(gè) Event 對(duì)象,其中日期和日期時(shí)間字段將根據(jù) @JsonFormat 注解指定的格式序列化為 JSON 字符串。
  • /event POST 端點(diǎn)接受一個(gè) JSON 請(qǐng)求體,并將其反序列化為 Event 對(duì)象。

你可以通過以下方式測(cè)試這些端點(diǎn):

GET 請(qǐng)求

curl -X GET http://localhost:8080/event

返回結(jié)果:

{
    "name": "Spring Boot Workshop",
    "date": "2023-10-01",
    "dateTime": "2023-10-01 10:15:30"
}

POST 請(qǐng)求

curl -X POST http://localhost:8080/event -H "Content-Type: application/json" -d '{
    "name": "Spring Boot Workshop",
    "date": "2023-10-01",
    "dateTime": "2023-10-01 10:15:30"
}'

返回結(jié)果:

Event created: Spring Boot Workshop

小結(jié)

        在本文中,我們?cè)敿?xì)介紹了 Spring Boot 中的 @DateTimeFormat@JsonFormat 注解的用法及作用。@DateTimeFormat 主要用于將請(qǐng)求參數(shù)或表單數(shù)據(jù)中的日期字符串解析為日期對(duì)象,而 @JsonFormat 則用于序列化和反序列化 JSON 數(shù)據(jù)中的日期時(shí)間字段。

        通過這些注解,我們可以更方便地處理日期和時(shí)間數(shù)據(jù),確保數(shù)據(jù)在不同層次間傳遞時(shí)的格式一致性。這對(duì)于開發(fā)高質(zhì)量的 Spring Boot 應(yīng)用至關(guān)重要

到此這篇關(guān)于Spring Boot 中的 @DateTimeFormat 和 @JsonFormat 的用法及作用的文章就介紹到這了,更多相關(guān)Spring Boot @DateTimeFormat 和 @JsonFormat內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 50 道Java 線程面試題(經(jīng)典)

    50 道Java 線程面試題(經(jīng)典)

    java 線程面試題是比較熱門的面試題,下面小編給大家分享了50道java線程面試題必掌握,大家來一起學(xué)習(xí)吧為面試好好準(zhǔn)備吧
    2016-11-11
  • Java8新特性之接口中的默認(rèn)方法和靜態(tài)方法詳解

    Java8新特性之接口中的默認(rèn)方法和靜態(tài)方法詳解

    今天帶大家學(xué)習(xí)的是Java8新特性的相關(guān)知識(shí),文章圍繞著Java接口中的默認(rèn)方法和靜態(tài)方法展開,文中有非常詳細(xì)的的代碼示例,需要的朋友可以參考下
    2021-06-06
  • @JsonFormat 實(shí)現(xiàn)日期格式自動(dòng)格式化

    @JsonFormat 實(shí)現(xiàn)日期格式自動(dòng)格式化

    這篇文章主要介紹了@JsonFormat 實(shí)現(xiàn)日期格式自動(dòng)格式化,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • SpringBoot整合Redis實(shí)現(xiàn)附近位置查找(LBS)功能

    SpringBoot整合Redis實(shí)現(xiàn)附近位置查找(LBS)功能

    Redis 提供了 GEO 數(shù)據(jù)結(jié)構(gòu),可以高效地存儲(chǔ)和查詢地理位置數(shù)據(jù),本文將介紹如何使用 Spring Boot + Redis 來實(shí)現(xiàn)附近位置查找,需要的可以了解下
    2025-03-03
  • JAVA集合框架專題

    JAVA集合框架專題

    這篇文章主要介紹了JAVA集合框架的相關(guān)知識(shí),文中講解非常細(xì)致,幫助大家更好的理解學(xué)習(xí)JAVA框架,感興趣的朋友快來了解下
    2020-06-06
  • 關(guān)于Gateway路由匹配規(guī)則解讀

    關(guān)于Gateway路由匹配規(guī)則解讀

    本文詳細(xì)介紹了SpringCloudGateway的路由匹配規(guī)則,包括基本概念、常用屬性、實(shí)際應(yīng)用以及注意事項(xiàng),路由匹配規(guī)則決定了請(qǐng)求如何被轉(zhuǎn)發(fā)到目標(biāo)服務(wù),是Gateway的核心功能之一,在配置路由時(shí)需要注意順序、性能和安全性
    2025-02-02
  • 使用Mybatis Generator結(jié)合Ant腳本快速自動(dòng)生成Model、Mapper等文件的方法

    使用Mybatis Generator結(jié)合Ant腳本快速自動(dòng)生成Model、Mapper等文件的方法

    這篇文章主要介紹了使用Mybatis Generator結(jié)合Ant腳本快速自動(dòng)生成Model、Mapper等文件的方法的相關(guān)資料,需要的朋友可以參考下
    2016-06-06
  • Java并發(fā)編程示例(五):線程休眠與恢復(fù)

    Java并發(fā)編程示例(五):線程休眠與恢復(fù)

    這篇文章主要介紹了Java并發(fā)編程示例(五):線程休眠與恢復(fù),在本節(jié),我們將開發(fā)一個(gè)程序,使用sleep()方法來實(shí)現(xiàn)每秒鐘打印一次當(dāng)前時(shí)間,需要的朋友可以參考下
    2014-12-12
  • java如何用Processing生成馬賽克風(fēng)格的圖像

    java如何用Processing生成馬賽克風(fēng)格的圖像

    這篇文章主要介紹了如何用java如何用Processing生成馬賽克風(fēng)格的圖像,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-03-03
  • Java Swing組件文件選擇器JFileChooser簡(jiǎn)單用法示例

    Java Swing組件文件選擇器JFileChooser簡(jiǎn)單用法示例

    這篇文章主要介紹了Java Swing組件文件選擇器JFileChooser簡(jiǎn)單用法,結(jié)合實(shí)例形式分析了Swing組件中的文件選擇器JFileChooser的簡(jiǎn)單使用方法,需要的朋友可以參考下
    2017-11-11

最新評(píng)論