- Jackson 注释教程
- Jackson - 主页
- 序列化注解
- Jackson - @JsonAnyGetter
- Jackson - @JsonGetter
- @JsonPropertyOrder
- Jackson - @JsonRawValue
- Jackson-@JsonValue
- Jackson - @JsonRootName
- Jackson - @JsonSerialize
- 反序列化注解
- Jackson - @JsonCreator
- Jackson - @JacksonInject
- Jackson - @JsonAnySetter
- Jackson-@JsonSetter
- Jackson - @JsonDeserialize
- @JsonEnumDefaultValue
- 属性包含注释
- @JsonIgnoreProperties
- Jackson - @JsonIgnore
- Jackson - @JsonIgnoreType
- Jackson-@JsonIninclude
- Jackson - @JsonAutoDetect
- 类型处理注解
- Jackson - @JsonTypeInfo
- Jackson - @JsonSubTypes
- Jackson - @JsonTypeName
- 一般注释
- Jackson - @JsonProperty
- Jackson-@JsonFormat
- Jackson - @JsonUnwrapped
- Jackson-@JsonView
- @JsonManagedReference
- @JsonBackReference
- Jackson - @JsonIdentityInfo
- Jackson-@JsonFilter
- 各种各样的
- 自定义注释
- 混合注释
- 禁用注释
- Jackson 注释资源
- Jackson - 快速指南
- Jackson - 有用的资源
- Jackson - 讨论
Jackson注释 - @JsonFormat
@JsonFormat用于在序列化或反序列化时指定格式。它主要与日期字段一起使用。
示例 - @JsonFormat
import java.io.IOException; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import com.fasterxml.jackson.annotation.JsonFormat; import com.fasterxml.jackson.databind.ObjectMapper; public class JacksonTester { public static void main(String args[]) throws IOException, ParseException { ObjectMapper mapper = new ObjectMapper(); SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd-MM-yyyy"); Date date = simpleDateFormat.parse("20-12-1984"); Student student = new Student(1, date); String jsonString = mapper .writerWithDefaultPrettyPrinter() .writeValueAsString(student); System.out.println(jsonString); } } class Student { public int id; @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd-MM-yyyy") public Date birthDate; Student(int id, Date birthDate){ this.id = id; this.birthDate = birthDate; } }
输出
{ "id" : 1, "birthDate" : "19-12-1984" }