- 傑克遜註解教程
- 傑克遜 - 主頁
- 序列化註解
- 傑克遜 - @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"
}
廣告