为Java对象设置OpenApi示例。

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

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

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:

确定