User.java
package org.example.entity; import com.fasterxml.jackson.annotation.JsonFormat; import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.databind.annotation.JsonDeserialize; import com.fasterxml.jackson.databind.annotation.JsonSerialize; import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer; import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer; import lombok.Data; import java.time.LocalDateTime; @Data public class User { @JsonProperty(value = "姓名") private String name; @JsonProperty(value = "年龄") private Integer age; @JsonProperty(value = "城市") private String city; @JsonProperty(value = "地址") private String address; @JsonProperty(value = "生日") @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") @JsonSerialize(using = LocalDateTimeSerializer.class) @JsonDeserialize(using = LocalDateTimeDeserializer.class) private LocalDateTime birthday; @JsonProperty(value = "级别") @JsonInclude(value = JsonInclude.Include.NON_NULL) private Integer level; @JsonIgnore private String password; }
@JsonProperty
属性注解,表示序列化时 key 的名字
@JsonIgnore
属性注解,表示序列化时排除的属性
@JsonInclude(value = JsonInclude.Include.NON_NULL)
属性注解,表示序列化时仅包含非空字段
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")标签:JsonProperty,常用,jackson,value,import,注解,com,fasterxml From: https://www.cnblogs.com/nihaorz/p/17865462.html
@JsonSerialize(using = LocalDateTimeSerializer.class)
@JsonDeserialize(using = LocalDateTimeDeserializer.class)
属性注解,组合使用,用于对 LocalDateTime 类型字段的序列化及反序列化时的字符串格式