Java中注解@JsonFormat的用法詳解
一、@JsonFormat是什么?
通常日期格式都是以時(shí)間戳的形式存放在數(shù)據(jù)庫(kù)里,當(dāng)前端頁(yè)面通過(guò)接口查詢時(shí),我們會(huì)將一個(gè)對(duì)象的某些屬性查出來(lái)返回給頁(yè)面。
類似在實(shí)體類上加上改注解:

二、@JsonFormat參數(shù)講解
1.locale代表中國(guó)的意思:中國(guó)時(shí)間段
2.pattern寫的就是對(duì)于數(shù)據(jù)庫(kù)的時(shí)間段
- yyyy代表-年
- MM代表-月
- dd代表-日
- HH代表-時(shí)
- mm代表分
- ss代表-秒
3.TimeZone
TimeZone 表示時(shí)區(qū)偏移量,也可以計(jì)算夏令時(shí)。
在操作 Date, Calendar等表示日期/時(shí)間的對(duì)象時(shí),經(jīng)常會(huì)用到TimeZone;因?yàn)椴煌臅r(shí)區(qū),時(shí)間不同。
那么timezone加上GMT+8什么意思呢?
GMT 就是格林威治標(biāo)準(zhǔn)時(shí)間的英文縮寫(Greenwich Mean Time 格林尼治標(biāo)準(zhǔn)時(shí)間),是世界標(biāo)準(zhǔn)時(shí)間,gmt+8 是格林威治時(shí)間+8小時(shí),中國(guó)所在時(shí)區(qū)就是gmt+8 。
附:@JsonFormat 將枚舉序列化為對(duì)象
Java Enum
@JsonFormat(shape = JsonFormat.Shape.OBJECT)
public enum EmployeeType {
FullTime("Full Time"), PartTime("Part Time");
private String displayName;
EmployeeType(String displayName) {
this.displayName = displayName;
}
public String getDisplayName() {
return displayName;
}
}
public class Employee {
private String name;
private EmployeeType employeeType;
...
}
Main Class
public class ExampleMain {
public static void main(String[] args) throws IOException {
Employee employee = new Employee();
employee.setName("Amy");
employee.setEmployeeType(EmployeeType.PartTime);
System.out.println("-- before serialization --");
System.out.println(employee);
System.out.println("-- after serialization --");
ObjectMapper om = new ObjectMapper();
String jsonString = om.writeValueAsString(employee);
System.out.println(jsonString);
System.out.println("-- after deserialization --");
System.out.println(om.readValue(jsonString, Employee.class));
}
}
枚舉被序列化為了 JSON 對(duì)象,但無(wú)法反序列化。
-- before serialization --
Employee{name='Amy', employeeType=PartTime}
-- after serialization --
{"name":"Amy","employeeType":{"displayName":"Part Time"}}
-- after deserialization --
Exception in thread "main" com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize value of type `org.example.c27.EmployeeType` from Object value (token `JsonToken.START_OBJECT`)
at [Source: (String)"{"name":"Amy","employeeType":{"displayName":"Part Time"}}"; line: 1, column: 30] (through reference chain: org.example.c27.Employee["employeeType"])
at com.fasterxml.jackson.databind.exc.MismatchedInputException.from(MismatchedInputException.java:59)
at com.fasterxml.jackson.databind.DeserializationContext.reportInputMismatch(DeserializationContext.java:1601)
at com.fasterxml.jackson.databind.DeserializationContext.handleUnexpectedToken(DeserializationContext.java:1375)
at com.fasterxml.jackson.databind.DeserializationContext.handleUnexpectedToken(DeserializationContext.java:1280)
at com.fasterxml.jackson.databind.DeserializationContext.extractScalarFromObject(DeserializationContext.java:872)
at com.fasterxml.jackson.databind.deser.std.EnumDeserializer.deserialize(EnumDeserializer.java:199)
at com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeAndSet(MethodProperty.java:129)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:324)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:187)
at com.fasterxml.jackson.databind.deser.DefaultDeserializationContext.readRootValue(DefaultDeserializationContext.java:322)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4593)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3548)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3516)
at org.example.c27.ExampleMain.main(ExampleMain.java:28)
Process finished with exit code 1不使用 @JsonFormat
-- before serialization --
Employee{name='Amy', employeeType=PartTime}
-- after serialization --
{"name":"Amy","employeeType":"PartTime"}
-- after deserialization --
Employee{name='Amy', employeeType=PartTime}總結(jié)
到此這篇關(guān)于Java中注解@JsonFormat用法的文章就介紹到這了,更多相關(guān)Java注解@JsonFormat內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring MVC請(qǐng)求參數(shù)接收的全面總結(jié)教程
這篇文章主要給大家總結(jié)介紹了關(guān)于Spring MVC請(qǐng)求參數(shù)接收的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-08-08
Java利用DelayQueue實(shí)現(xiàn)延遲任務(wù)代碼實(shí)例
這篇文章主要介紹了Java利用DelayQueue實(shí)現(xiàn)延遲任務(wù)代碼實(shí)例,DelayQueue?是一個(gè)支持延時(shí)獲取元素的阻塞隊(duì)列,?內(nèi)部采用優(yōu)先隊(duì)列?PriorityQueue?存儲(chǔ)元素,同時(shí)元素必須實(shí)現(xiàn)?Delayed?接口,需要的朋友可以參考下2023-12-12
SpringBoot整合ES多個(gè)精確值查詢 terms功能實(shí)現(xiàn)
這篇文章主要介紹了SpringBoot整合ES多個(gè)精確值查詢 terms功能實(shí)現(xiàn),本文給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧2024-06-06
SpringBoot常見(jiàn)問(wèn)題小結(jié)
這篇文章主要介紹了SpringBoot常見(jiàn)問(wèn)題小結(jié),需要的朋友可以參考下2017-07-07
Maven分步詳解多環(huán)境配置與應(yīng)用流程
這篇文章主要介紹了Maven進(jìn)階多環(huán)境配置與應(yīng)用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08
windows 部署JAVA環(huán)境安裝iDea的詳細(xì)步驟
這篇文章主要介紹了windows 部署JAVA環(huán)境安裝iDea的詳細(xì)步驟,本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-08-08

