- 杰克逊注解教程
- 杰克逊 - 主页
- 序列化注解
- 杰克逊 - @JsonAnyGetter
- 杰克逊 - @JsonGetter
- @JsonPropertyOrder
- 杰克逊 - @JsonRawValue
- 杰克逊 - @JsonValue
- 杰克逊 - @JsonRootName
- 杰克逊 - @JsonSerialize
- 反序列化注解
- 杰克逊 - @JsonCreator
- 杰克逊 - @JacksonInject
- 杰克逊 - @JsonAnySetter
- 杰克逊 - @JsonSetter
- 杰克逊 - @JsonDeserialize
- @JsonEnumDefaultValue
- 属性包含注解
- @JsonIgnoreProperties
- 杰克逊 - @JsonIgnore
- 杰克逊 - @JsonIgnoreType
- 杰克逊 - @JsonInclude
- 杰克逊 - @JsonAutoDetect
- 类型处理注解
- 杰克逊 - @JsonTypeInfo
- 杰克逊 - @JsonSubTypes
- 杰克逊 - @JsonTypeName
- 通用注解
- 杰克逊 - @JsonProperty
- 杰克逊 - @JsonFormat
- 杰克逊 - @JsonUnwrapped
- 杰克逊 - @JsonView
- @JsonManagedReference
- @JsonBackReference
- 杰克逊 - @JsonIdentityInfo
- 杰克逊 - @JsonFilter
- 其他
- 自定义注解
- MixIn 注解
- 禁用注解
- 杰克逊注解资源
- 杰克逊 - 快速指南
- 杰克逊 - 有用资源
- 杰克逊 - 讨论
杰克逊注解 - @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"
}
广告