0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

springでjsonを簡易的に整形

Posted at

springというかjacksonでjson文字列をとりあえず整形したい場合はObjectMapper#writerWithDefaultPrettyPrinter()を使用する。

以下はjackson-2.13.2で確認。

  public record SampleResponse(String first, String second, List<Integer> list) {
  }

  ...

  SampleResponse response = new SampleResponse("aa", "bb", List.of(1,2,3));  
  ObjectMapper mapper = new ObjectMapper();
  String json = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(response);

出力は下のような感じ。

{
  "first" : "aa",
  "second" : "bb",
  "list" : [ 1, 2, 3 ]
}

参考

0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?