java - 如何使用 Quarkus 在 Swagger 中生成请求示例

标签 java swagger openapi quarkus

我想使用 Quarkus 和 Swagger 在请求中生成示例值。

Quarkus中有类似@ApiModelProperty的注解吗?或者是否有任何标签可以在请求中设置示例?

enter image description here

谢谢

最佳答案

对于 Quarkus,您需要使用 Microprofile Openapi 注释:

https://download.eclipse.org/microprofile/microprofile-open-api-1.0/microprofile-openapi-spec.html

具体来说:@ExampleObject 其中

Illustrates an example of a particular content.

https://github.com/eclipse/microprofile-open-api/blob/master/api/src/main/java/org/eclipse/microprofile/openapi/annotations/media/ExampleObject.java

示例:

 public interface ExamplePayloads {
      String NEW_PLAYER = "{\"age\":12, \"username\":\"username\"}";
      String SAVED_PLAYER = "{\"id\":1234, \"age\":12, \"username\":\"username\"}";
      
}

ExampleObjectvalue 属性采用字符串:

   /**
     * A string representation of the example.
     * <p>
     * This is mutually exclusive with the externalValue property, and ignored if the externalValue property is
     * specified.
     * </p>
     * If the media type associated with the example allows parsing into an object, it may be converted from a string.
     * 
     * @return the value of the example
     **/
    String value() default "";
    
  

import static yourpackage.ExamplePayloads.*;

@Path("/players")
@Consumes(APPLICATION_JSON)
@Produces(APPLICATION_JSON)
public class PlayersResource {


      @POST
      @Operation(summary = "save new player", description = "Creates new player with given age and username.")
      @RequestBody(
        required = true,
        content = @Content(
          schema = @Schema(implementation = Player.class, required = true, requiredProperties = {"username", "age"}),
          examples = @ExampleObject(
            name = "new player",
            description = "saves new player with parameters username and age",
            value = NEW_PLAYER
          )
        ))
      @APIResponses(
        value = {
        @APIResponse(
          name = "success",
          responseCode = "200",
          content = @Content(
            schema = @Schema(implementation = Player.class, required = true, requiredProperties = {"id", "username", "age"}),
            examples = @ExampleObject(
              name = "saved player",
              description = "new player with id assigned.",
              value = SAVED_PLAYER
            )
          ))
        ,
        @APIResponse(
          name = "bad request, no username",
          responseCode = "400",
          description = "missing username or age"
        ),
        }
    )
      public Player savePlayer(Player player) {
       
        return playersService.savePlayer(player);
      }


}

关于java - 如何使用 Quarkus 在 Swagger 中生成请求示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71464876/

相关文章:

java - 我可以在 Java 代码中动态扩展 ListView 的大小吗

Swagger 2.0 - 如何使 "one or the other"参数成为必需的?

swagger - OpenAPI/JSON Schema 中的多重继承/组合

openapi - 如何在 OpenAPI YAML 中为 MP3 文件编写响应?

aws-cloudformation - 如果 sam 模板位于 API Gateway DefinitionBody 中作为响应示例,如何让 null 值传入 sam 模板

java - JPA Criteria Query API 和两列排序

java - Intellij 无法解析具有多个 SourceSet 的 Gradle 项目中的符号

java - 在线小程序给出 ClassNotFoundException

maven - Swagger |通过 maven 命令将 YAML 转换为 JSON

oauth-2.0 - Swagger 支持哪些 OAuth 2.0 流程?