英文:
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
类,有两个属性,name
和 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;
}
}
假设您有一个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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论