Python 官方文档:入门教程 => 点击学习
目录一、@JSONFORMat是什么?二、@jsonFormat参数讲解附:@JsonFormat 将枚举序列化为对象Java EnumMain Class不使用 @Jso
通常日期格式都是以时间戳的形式存放在数据库里,当前端页面通过接口查询时,我们会将一个对象的某些属性查出来返回给页面。
类似在实体类上加上改注解:
1.locale代表中国的意思:中国时间段
2.pattern写的就是对于数据库的时间段
3.TimeZone
TimeZone 表示时区偏移量,也可以计算夏令时。
在操作 Date, Calendar等表示日期/时间的对象时,经常会用到TimeZone;因为不同的时区,时间不同。
那么timezone加上GMT+8什么意思呢?
GMT 就是格林威治标准时间的英文缩写(Greenwich Mean Time 格林尼治标准时间),是世界标准时间,gmt+8 是格林威治时间+8小时,中国所在时区就是gmt+8 。
@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;
...
}
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 对象,但无法反序列化。
-- 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
-- before serialization --
Employee{name='Amy', employeeType=PartTime}
-- after serialization --
{"name":"Amy","employeeType":"PartTime"}
-- after deserialization --
Employee{name='Amy', employeeType=PartTime}
到此这篇关于Java中注解@JsonFormat用法的文章就介绍到这了,更多相关Java注解@JsonFormat内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!
--结束END--
本文标题: Java中注解@JsonFormat的用法详解
本文链接: https://lsjlt.com/news/176998.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0