0
Help us understand the problem. What are the problem?

posted at

springでjsonを簡易的に整形

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 ]
}

参考

Register as a new user and use Qiita more conveniently

  1. You can follow users and tags
  2. you can stock useful information
  3. You can make editorial suggestions for articles
What you can do with signing up
0
Help us understand the problem. What are the problem?