亲宝软件园·资讯

展开

Java中注解@JsonFormat的用法详解

喝汽水的猫^ 人气:0

一、@JsonFormat是什么?

通常日期格式都是以时间戳的形式存放在数据库里,当前端页面通过接口查询时,我们会将一个对象的某些属性查出来返回给页面。

类似在实体类上加上改注解:

二、@JsonFormat参数讲解

1.locale代表中国的意思:中国时间段

2.pattern写的就是对于数据库的时间段

3.TimeZone

TimeZone 表示时区偏移量,也可以计算夏令时。

在操作 Date, Calendar等表示日期/时间的对象时,经常会用到TimeZone;因为不同的时区,时间不同。

那么timezone加上GMT+8什么意思呢?

GMT 就是格林威治标准时间的英文缩写(Greenwich Mean Time 格林尼治标准时间),是世界标准时间,gmt+8 是格林威治时间+8小时,中国所在时区就是gmt+8 。

附:@JsonFormat 将枚举序列化为对象

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 对象,但无法反序列化。

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

总结 

加载全部内容

相关教程
猜你喜欢
用户评论