LoginSignup
1
0

More than 3 years have passed since last update.

【Spock】LocalDate型をJSONに変換

Posted at

問題

controllerのテストコードを書いていたら、LocalDate型がJSONにうまく変換できなかった。

原因

変換後よくわからない複雑なJSONオブジェクトになっていた。これは、JacksonがLocalDate型のJavaインスタンスをそのままJSON形式に変換しているから。変な文字列じゃなくて、読みやすい"YYYY-MM-DD"で統一したい!

@JsonSerialize で解決!!

model のLocalDate型のオブジェクトに@JsonSerializeアノテーションをつければ良い!

@JsonSerialize(using = LocalDateSerializer.class)
@JsonProperty("created_at")
private LocalDate createdAt;

詳細は後日追記

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0