【问题标题】:Can ApiResponse of swagger use HttpStatus in spring bootswagger的ApiResponse能否在spring boot中使用HttpStatus
【发布时间】:2019-01-30 18:30:17
【问题描述】:

在使用带有swagger的spring boot时,我发现@GetMapping之前的数字和消息是硬编码的

@ApiResponse(code = 200, message = "Successfully retrieved list"),
            @ApiResponse(code = 400, message = "Bad request"),
            @ApiResponse(code = 403, message = "Accessing the resource you were trying to reach is forbidden"),
            @ApiResponse(code = 404, message = "The resource you were trying to reach is not found")  

我可以使用类似的东西


@ApiResponse(response = HttpStatus.OK)

codemessage 采取HttpStatus.OK 吗? 这样我就不用写消息和代码了?

我发现我可以使用

@ResponseStatus(value=HttpStatus.OK)

它出现在 swagger doc 中,但我不能像下面这样使用几个

@ResponseStatus(value=HttpStatus.OK)
@ResponseStatus(value=HttpStatus.BAD_REQUEST)

编辑: 我的招摇版本是2.9.2, 我使用

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>${swagger.version}</version>
    <scope>compile</scope>
</dependency>

【问题讨论】:

    标签: spring-boot


    【解决方案1】:

    您使用的是哪个版本的 Swagger 注释?

    基本上,参考文档,@ApiResponse (Swagger-Core v1.5.0) 中可以使用 6 个可能的元素:code、message、reference、response、responseContainer、responseHeaders。 Swagger-Core v.2.0.0-RC3 中的 5 个可能元素:内容、描述、标题、链接、响应代码

    总而言之,不可能只设置响应值,而是同时生成响应代码和消息。

    另外,在 1.5.0 版中,code 和 message 元素是必需的。所以你不能只使用其中一个。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-06-04
      • 2023-01-19
      • 2020-06-27
      • 1970-01-01
      • 2015-12-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多