java - 用于自定义响应的 Swagger 注释?

标签 java swagger swagger-ui springfox

像这样设置自定义响应:

public class CustomResponse {
    private int id;
    private String productName;
    private int quantity;
    private double price;

    // Constructor & along with Getters & Setters

}

我的 ProductController 中的 Swagger:

@RestController
@RequestMapping("/api/v1")
public class ProductController {

    @ApiOperation(httpMethod = "GET", 
                  value = "Retrieves reesults based on specific values set in the request parameters.", 
                  notes = "Sends back query results in JSON format after being processed.", 
                  produces = "application/json")
    @ApiResponses(value = { 
                        @ApiResponse(code = 200, message = "Successful GET command", response = CustomResponse.class),
                        @ApiResponse(code = 400, message = "Bad Request"),
                        @ApiResponse(code = 404, message = "Entity Not Found"), 
                        @ApiResponse(code = 500, message = "Internal Server Error") 
                        })
    @RequestMapping(value = "/products", method = RequestMethod.GET, produces="application/json" )
    public ResponseEntity<Object> getQueryResults(@ApiParam(value = "productName", required = true) @RequestParam(value = "productName") String productName) throws IOException {
        // Implementation details
    }
}

我的 pom.xml:

    <!-- Swagger -->
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger2</artifactId>
        <version>2.6.1</version>
        <scope>compile</scope>
    </dependency>

    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger-ui</artifactId>
        <version>2.6.1</version>
        <scope>compile</scope>
    </dependency>

当我启动我的网络服务并打开 Swagger 的 UI 时,我看到了 CustomResponse 的模型和示例值...

问题:

  1. 如何描述/记录 Swagger 中 CustomRepsonse 的各个属性?意思是,是否有 Swagger 注释来描述 CustomResponse 的每个字段?

  2. 在示例值中,有没有一种方法可以用实际的硬编码数据记录属性?

现在,它在示例值中显示如下:

{ "id" : 0, "productName" : "string","quantity" : 0, "price" : 0.00 }

最佳答案

How can I describe / document the individual attributes of the CustomRepsonse inside the Swagger? Meaning, is there a Swagger annotation to describe each field of the CustomResponse?

请注意注释 @io.swagger.annotations.ApiModelProperty - 它应该可以帮助您为 customResponse 添加文档

public class CustomResponse {
    private int id;
    @io.swagger.annotations.ApiModelProperty(value = "my super product name")
    private String productName;
    @io.swagger.annotations.ApiModelProperty(allowableValues = "1,5,10,25,50")
    private int quantity;
    private double price;

    // Constructor & along with Getters & Setters

}

In the Example Value, is there a way I can also document the attributes with actual hardcoded data?

您当然可以为 ApiParam 指定默认值。我认为没有办法通过 CustomResponse 字段的 swagger 注释指定默认数据。在某种程度上,您可以使用注释 @io.swagger.annotations.ApiModelProperty 来满足您的需求。


使用 swagger,您可以为 CustomResponse 定义网络文档,但默认值将在类级别(构造函数或字段声明)中默认指定。为了使其更灵活,我更喜欢使用 @ControllerAdvice我在其中配置异常处理程序,在这里我有机会在相应的异常上映射 code 并为我的自定义响应指定值(也是默认值)。


正如我在您的 swagger 文档中看到的,您已经覆盖了 code - 默认 message 映射。为了让它工作,它会很有用 to change some configuration (请注意 .useDefaultResponseMessages(false) 以及负责此操作的后续行)。

关于java - 用于自定义响应的 Swagger 注释?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45424955/

相关文章:

javascript - CKeditor 如何与 Bower 一起使用

java - 了解 StringUtils.join 性能决策

asp.net - 斯瓦格停止工作

wso2 - WSO2 API 管理什么时候开始支持 OpenAPI3.0 (Swagger 3.0)?

java - 使用 StreamTokenizer 获取下一个标记和剩余的字符串

java - Jenkins 不会运行 PHPUunit - 权限被拒绝

asp.net-web-api - 无法运行 Swagger UI

swagger - openapi3/swagger 描述文本中的内嵌图像

swagger - 我如何在 swagger ui 中为嵌套数组输入数据

go - 如何在带有 http.ServeMux 的 GoLang lang 中使用 swaggo (swagger doc)?