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

时间:2017-07-31 20:42:59

标签: 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的模型和示例值......

问题(S):

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

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

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

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

1 个答案:

答案 0 :(得分:3)

  

如何在Swagger中描述/记录CustomRepsonse的各个属性?意思是,是否有一个Swagger注释来描述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

}
  

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

您肯定可以为ApiParam指定默认值。我认为没有办法通过CustomResponse字段的swagger注释指定默认数据。部分地,您可以使用注释@io.swagger.annotations.ApiModelProperty来满足您的需求。

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

正如我在您的招摇文档中看到的那样,您已覆盖code - 默认message映射。为了使其正常运行,to change some configuration会很有用{关注.useDefaultResponseMessages(false),然后按行负责这一点。)