为Java对象设置OpenApi示例。

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

Set OpenApi examples for java objects

问题

  1. 我有一个Java我想要注释以获得一个良好文档化的API该类如下
  2. ```java
  3. package test;
  4. import io.swagger.v3.oas.annotations.media.Schema;
  5. @Schema(description = "The Employee.", title = "Employee", type = "object")
  6. public class Employee {
  7. @Schema(description = "Firstname.", example = "Alex", type = "string")
  8. private String firstname;
  9. @Schema(description = "The person.", type = "object")
  10. private Person person;
  11. public String getFirstname() {
  12. return firstname;
  13. }
  14. public Employee setFirstname(String firstname) {
  15. this.firstname = firstname;
  16. return this;
  17. }
  18. public Person getPerson() {
  19. return person;
  20. }
  21. public Employee setPerson(Person person) {
  22. this.person = person;
  23. return this;
  24. }
  25. }

我在这里遇到的问题是,似乎找不到一种方法来为属性Person设置示例。我尝试放置一个JSON字符串,但那不起作用。是否有可能为对象设置示例?谢谢。

这是我尝试的内容:

  1. ...
  2. @Schema(description = "The person.", type = "object", example = "{\"age\": 30}")
  3. private Person person;
  4. ...

这会生成:

  1. ...
  2. "example": "{\"age\": 30}"
  3. ...

但我正在寻找的是:

  1. "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 :

  1. package test;
  2. import io.swagger.v3.oas.annotations.media.Schema;
  3. @Schema(description = "The Employee.", title = "Employee", type = "object")
  4. public class Employee {
  5. @Schema(description = "Firstname.", example = "Alex", type = "string")
  6. private String firstname;
  7. @Schema(description = "The person.", type = "object")
  8. private Person person;
  9. public String getFirstname() {
  10. return firstname;
  11. }
  12. public Employee setFirstname(String firstname) {
  13. this.firstname = firstname;
  14. return this;
  15. }
  16. public Person getPerson() {
  17. return person;
  18. }
  19. public Employee setPerson(Person person) {
  20. this.person = person;
  21. return this;
  22. }
  23. }

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 :

  1. ...
  2. @Schema(description = "The person.", type = "object", example: "{'age': 30}")
  3. private Person person;
  4. ...

this generates :

  1. ...
  2. "example": "{'age': 30}"
  3. ...

but what I'm looking for is :

  1. "example": {"age": 30}

答案1

得分: 2

为了为Employee类中的Person对象设置OpenAPI示例,您可以使用@Schema注解。

  1. import io.swagger.v3.oas.annotations.media.Schema;
  2. @Schema(description = "员工", title = "员工", type = "object")
  3. public class Employee {
  4. @Schema(description = "名字", example = "Alex", type = "string")
  5. private String firstname;
  6. @Schema(description = "人员信息", type = "object", example = "{ \"name\": \"John\", \"age\": 30 }")
  7. private Person person;
  8. public String getFirstname() {
  9. return firstname;
  10. }
  11. public Employee setFirstname(String firstname) {
  12. this.firstname = firstname;
  13. return this;
  14. }
  15. public Person getPerson() {
  16. return person;
  17. }
  18. public Employee setPerson(Person person) {
  19. this.person = person;
  20. return this;
  21. }
  22. }
英文:

To set OpenAPI examples for the Person object within the Employee class, you can use the @Schema annotation.

  1. import io.swagger.v3.oas.annotations.media.Schema;
  2. @Schema(description = "The Employee.", title = "Employee", type = "object")
  3. public class Employee {
  4. @Schema(description = "Firstname.", example = "Alex", type = "string")
  5. private String firstname;
  6. @Schema(description = "The person.", type = "object", example = "{ \"name\": \"John\", \"age\": 30 }")
  7. private Person person;
  8. public String getFirstname() {
  9. return firstname;
  10. }
  11. public Employee setFirstname(String firstname) {
  12. this.firstname = firstname;
  13. return this;
  14. }
  15. public Person getPerson() {
  16. return person;
  17. }
  18. public Employee setPerson(Person person) {
  19. this.person = person;
  20. return this;
  21. }
  22. }

huangapple
  • 本文由 发表于 2023年7月27日 22:11:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/76780603.html
匿名

发表评论

匿名网友

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

确定