如何通过@RestController中的注解使JSON输出具有缩进?

huangapple go评论137阅读模式
英文:

How to make Indent output JSONs by annotations in RestController?

问题

Is there an annotation for RestController that will help to do this? This is a spring boot application that produces = MediaType.APPLICATION_JSON_VALUE in @RequestMapping.

我需要美化输出JSON。是否有一个适用于RestController的注解可以帮助实现这一目标?这是一个Spring Boot应用程序,它在@RequestMapping中生成MediaType.APPLICATION_JSON_VALUE。

I found one way with the configuration in application.yml
jackson: serialization: indent-output: true
However, this solution imposes this rule on the entire project, and I want to do this for only one class.

我在application.yml配置中找到了一种方法:
jackson: serialization: indent-output: true
然而,这种解决方案会将此规则应用于整个项目,而我只想针对一个类执行此操作。

Also, I think @JsonSerialize can help me, but I couldn't set up an annotation for pretty print.

此外,我认为@JsonSerialize可以帮助我,但我无法设置一个适用于漂亮打印的注解。

英文:

I need to pretty output JSONs. Is there an annotation for RestController that will help to do this? This is a spring boot application that produces = MediaType.APPLICATION_JSON_VALUE in @RequestMapping.

I found one way with the configuration in application.yml

jackson:
  serialization:
    indent-output: true

However, this solution imposes this rule on the entire project, and I want to do this for only one class.
Also, I think @JsonSerialize can help me, but I couldn't set up an annotation for pretty print

答案1

得分: 1

让我们假设您有一个简单的 Person 类,有两个属性,nameage

public class Person {

   private String name;
   private int age;

   public Person(String name, int age) {
       this.name = name;
       this.age = age;
   }

   public String getName() {
       return name;
   }

   public int getAge() {
       return age;
   }
}

假设您有一个Spring的RestController,它将一个 Person 对象以JSON格式返回:

@RestController
public class MyRestController {

   @GetMapping("/person")
   public Person getPerson() {
      return new Person("John Doe", 30);
   }
}

默认情况下,JSON输出将是紧凑的,没有缩进:

{"name":"John Doe","age":30}

要使JSON输出格式化漂亮,您可以在 Person 类上使用 @JsonFormat 注解:

@JsonFormat(shape = JsonFormat.Shape.OBJECT)
public class Person {
    //...
}

该注解的 shape 属性指定了序列化的JSON形状。在这种情况下,我们将其设置为 JsonFormat.Shape.OBJECT,以指示输出应该格式化为JSON对象。

现在,当您调用 /person 端点时,JSON输出将被格式化漂亮:

{
  "name" : "John Doe",
  "age" : 30
}

正如您所见,JSON输出现在具有缩进,更容易阅读。

英文:

Let's say you have a simple Person class with 2 properties, name and age:

public class Person {

   private String name;
   private int age;

   public Person(String name, int age) {
       this.name = name;
       this.age = age;
   }

   public String getName() {
       return name;
   }

   public int getAge() {
       return age;
   }
}

And let's say you have a spring RestController that returns a Person object as JSON:

@RestController
public class MyRestController {

   @GetMapping("/person")
   public Person getPerson() {
      return new Person("John Doe", 30);
   }
}

By default, the JSON output will be compact and not indented:

{"name":"John Doe","age":30}

To pretty-print the JSON output, you can use the @JsonFormat annotation on the Person class:

@JsonFormat(shape = JsonFormat.Shape.OBJECT)
public class Person {
    //...
}

The shape property of the annotation specifics the shape of the serialized JSON. In this case, we set it to JsonFormat.Shape.OBJECT to indicate that the output should be formatted as a JSON object.

Now, when you call the /Person endpoint, the JSON output will be pretty-printed

{
  "name" : "John Doe",
  "age" : 30
}

As you can see, the JSON output is now indented and easier to read.

huangapple
  • 本文由 发表于 2023年3月8日 16:03:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/75670577.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定