英文:
Set OpenApi examples for java objects
问题
我有一个Java类,我想要注释以获得一个良好文档化的API。该类如下:
```java
package test;
import io.swagger.v3.oas.annotations.media.Schema;
@Schema(description = "The Employee.", title = "Employee", type = "object")
public class Employee {
@Schema(description = "Firstname.", example = "Alex", type = "string")
private String firstname;
@Schema(description = "The person.", type = "object")
private Person person;
public String getFirstname() {
return firstname;
}
public Employee setFirstname(String firstname) {
this.firstname = firstname;
return this;
}
public Person getPerson() {
return person;
}
public Employee setPerson(Person person) {
this.person = person;
return this;
}
}
我在这里遇到的问题是,似乎找不到一种方法来为属性Person设置示例。我尝试放置一个JSON字符串,但那不起作用。是否有可能为对象设置示例?谢谢。
这是我尝试的内容:
...
@Schema(description = "The person.", type = "object", example = "{\"age\": 30}")
private Person person;
...
这会生成:
...
"example": "{\"age\": 30}"
...
但我正在寻找的是:
"example": {"age": 30}
英文:
I have a java class that I want to anotate in order to get a well documented API. The class is the following :
package test;
import io.swagger.v3.oas.annotations.media.Schema;
@Schema(description = "The Employee.", title = "Employee", type = "object")
public class Employee {
@Schema(description = "Firstname.", example = "Alex", type = "string")
private String firstname;
@Schema(description = "The person.", type = "object")
private Person person;
public String getFirstname() {
return firstname;
}
public Employee setFirstname(String firstname) {
this.firstname = firstname;
return this;
}
public Person getPerson() {
return person;
}
public Employee setPerson(Person person) {
this.person = person;
return this;
}
}
The problem I have here is that I can't seem to find a way to set an example when for the property Person. I tried putting a json string but that didn't work. Is it even possible to set an example for objects? Thanks.
Here's what I tried :
...
@Schema(description = "The person.", type = "object", example: "{'age': 30}")
private Person person;
...
this generates :
...
"example": "{'age': 30}"
...
but what I'm looking for is :
"example": {"age": 30}
答案1
得分: 2
为了为Employee类中的Person对象设置OpenAPI示例,您可以使用@Schema注解。
import io.swagger.v3.oas.annotations.media.Schema;
@Schema(description = "员工", title = "员工", type = "object")
public class Employee {
@Schema(description = "名字", example = "Alex", type = "string")
private String firstname;
@Schema(description = "人员信息", type = "object", example = "{ \"name\": \"John\", \"age\": 30 }")
private Person person;
public String getFirstname() {
return firstname;
}
public Employee setFirstname(String firstname) {
this.firstname = firstname;
return this;
}
public Person getPerson() {
return person;
}
public Employee setPerson(Person person) {
this.person = person;
return this;
}
}
英文:
To set OpenAPI examples for the Person object within the Employee class, you can use the @Schema annotation.
import io.swagger.v3.oas.annotations.media.Schema;
@Schema(description = "The Employee.", title = "Employee", type = "object")
public class Employee {
@Schema(description = "Firstname.", example = "Alex", type = "string")
private String firstname;
@Schema(description = "The person.", type = "object", example = "{ \"name\": \"John\", \"age\": 30 }")
private Person person;
public String getFirstname() {
return firstname;
}
public Employee setFirstname(String firstname) {
this.firstname = firstname;
return this;
}
public Person getPerson() {
return person;
}
public Employee setPerson(Person person) {
this.person = person;
return this;
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论