微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Open API 3.0.1 SpringBoot RestAPI 中的示例定义之一

如何解决Open API 3.0.1 SpringBoot RestAPI 中的示例定义之一

我正在为现有 API 编写 OpenAPI 规范。此 API 针对两种不同的失败返回状态 400,但具有不同的响应结构。我尝试了以下结构。

如何在 OpenAPI 3.0.1 中使用 oneOf 示例和 Springboot Rest API?

    get:
      tags:
        - pet
      summary: Find pet by ID
      description: Returns a single pet
      operationId: getPetById
      parameters:
        - name: petId
          in: path
          description: ID of pet to return
          required: true
          schema:
            type: integer
            format: int64
      responses:
        '400':
          description: Invalid ID supplied
          content:
            application/json:
               schema:
                 oneOf:
                  - $ref: '#/components/schemas/ApiResultOk'
                  - $ref: '#/components/schemas/ApiResultError'
               examples:
                  success:
                    summary: Example of a Test response 1
                    value:
                      errorCode: "1001"
                      errorMsg: "Error Message 1"
                  error:
                    summary: Example of an error response 2
                    value:
                      errorCode: "1002"
                      errorMsg: "Error Message 2"
components:
  schemas:
    ApiResultOk:
      type: object
      properties:
        errorCode:
          type: string
        errorMsg:
          type: string
    ApiResultError:
      type: object
      properties:
        errorCode:
          type: string
        errorMsg:
          type: string

编辑:生成maven插件删除oneOf(单一架构)后

    @ApiOperation(value = "Find pet by ID",nickname = "getPetById",notes = "Returns a single pet",authorizations = {
        
        @Authorization(value = "api_key")
         },tags={ "pet",})
    @ApiResponses(value = { 
        @ApiResponse(code = 400,message = "Invalid ID supplied",response = ApiResult.class),@ApiResponse(code = 404,message = "Pet not found") })
@RequestMapping(value = "/ticketReOpen/{ticketUUID}",method = RequestMethod.POST,produces = MediaType.APPLICATION_JSON_VALUE)

公共类 ApiResult {

  @JsonProperty("errorCode")
  private String errorCode;

  @JsonProperty("errorMsg")
  private String errorMsg;

  public ApiResult errorCode(String errorCode) {
    this.errorCode = errorCode;
    return this;
  }

  @ApiModelProperty(value = "")


  public String getErrorCode() {
    return errorCode;
  }

  public void setErrorCode(String errorCode) {
    this.errorCode = errorCode;
  }

  public ApiResult errorMsg(String errorMsg) {
    this.errorMsg = errorMsg;
    return this;
  }

  @ApiModelProperty(value = "")


  public String getErrorMsg() {
    return errorMsg;
  }

  public void setErrorMsg(String errorMsg) {
    this.errorMsg = errorMsg;
  }

} Swagger UI 不显示 400 代码响应和示例

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。