简体   繁体   English

如何在openApi / springfox-swagger2中为不同的状态代码定义不同的响应模型

[英]How to define different response models for different status codes in openApi / springfox-swagger2

Given the following REST method with springfox-swagger2 annotations: 给定以下带有springfox-swagger2注释的REST方法:

@GetMapping(value = "/access", produces = MediaType.APPLICATION_JSON_VALUE)
@ApiOperation(value = "check access allowed")
@ApiResponses({
        @ApiResponse(code = 200, message = "okay, there you go", response = AccessResponse.class),
        @ApiResponse(code = 204, message = "I got nothing for you", response = Void.class)
})
public ResponseEntity<AccessResponse> access() {

    if (!isAccessEnabled()) {
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }
    AccessResponse response = new AccessResponse("some data");
    return ResponseEntity.ok(response);
}

Notice that there are two states that this method can return: 请注意,此方法可以返回两种状态:

  1. a response of type AccessResponse AccessResponse类型的响应
  2. a http 204 - no content response http 204 - 没有内容回复

I want to generate a swagger api documentation that reflects the different response models (AccessResponse vs. Void). 我想生成一个反映不同响应模型(AccessResponse vs. Void)的swagger api文档。 Inside the @ApiResponse Annotation I explicitly tell springfox-swagger2 to use different models for each state. 在@ApiResponse注释中,我明确告诉springfox-swagger2为每个状态使用不同的模型。 Unfortunately the generated swagger api doc json refers only to the AccessResponse model for both http 200 and 204: 不幸的是,生成的swagger api doc json仅指http 200和204的AccessResponse模型:

 "responses":{ "200":{ "description":"okay, there you go", "schema":{"$ref":"#/definitions/AccessResponse"} }, "204":{ "description":"I got nothing for you", "schema":{"$ref":"#/definitions/AccessResponse"} } } 

Am I missing something? 我错过了什么吗? Is there a way to tell swagger to render two different models for each HTTP/ok status code? 有没有办法告诉swagger为每个HTTP / ok状态代码渲染两个不同的模型?

I've changed the return type of the method - removing the generic type: 我已经更改了方法的返回类型 - 删除泛型类型:

public ResponseEntity access()

which results in a better (but not perfect) model description: 这导致更好(但不完美)的模型描述:

 "204":{ "description": "I got nothing for you", "schema":{"type":"object"} } 

Swagger UI renders this to Swagger UI呈现此内容

在此输入图像描述

I like that it displays the empty body now. 我喜欢它现在显示空体。 However, the statusCode is a bit irritating. 但是,statusCode有点恼人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Swagger 和 Springfox 更改不同状态代码的示例响应 - Swagger and Springfox Changing the example response for different status codes Springfox-Swagger2 406 不可接受的错误 - Spring 4 - Springfox-Swagger2 406 not acceptable error -Spring 4 财产“年份”的冲突设置者定义 (Springfox-Swagger2) - Conflict Setter Definition For Property "Year" (Springfox-Swagger2) 如何在Springfox Swagger中删除操作的响应体? - How to remove the response body of an operation in Springfox Swagger? 如何在Springfox Swagger中将响应模型设置为类? - How to set response model as a class in Springfox Swagger? Springfox Swagger将响应状态200添加到POST和PUT - Springfox Swagger adding response status 200 to POST and PUT 使用springfox-swagger2版本3.0.0-SNAPSHOT观察到错误“无法推断基本URL” - Observing error “Unable to infer base url” with springfox-swagger2 version 3.0.0-SNAPSHOT 春季不同的响应模型 - Different response models in spring 如何使用Openapi Generator从Swagger yaml生成SpringBoot模型 - How to generate SpringBoot models from Swagger yaml with Openapi Generator ResponseEntity - 如何处理不同于 200 ok 的状态码? - ResponseEntity - How to handle status codes that are different from 200 ok?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM