java - Springfox Swagger 将响应状态 200 添加到 POST 和 PUT

标签 java spring swagger springfox

我正在使用 springfox-swagger2 2.6.1 版,它会自动为 PUT 和 POST 操作插入 HTTP 200 响应消息,尽管我尝试将其配置为不这样做(我不使用响应状态 200 用于 POST 或PUT,但分别为 201 和 204);看下面的截图:

enter image description here

我看到了类似问题的答案,其中作者建议向您的 Controller 添加一个 @ResponseStatus 注释以“修复”它,但这变得不灵活并且违背了 Spring 关于使用 ResponseEntity@ResponseStatus 用于 rest API。示例:

How to change the response status code for successful operation in Swagger?

https://github.com/springfox/springfox/issues/908

有没有其他方法可以强制Springfox Swagger不添加这个200 OK状态码?

我的 Docket 配置:

@Bean
public Docket api() {
    return new Docket(DocumentationType.SWAGGER_2)
            .useDefaultResponseMessages(false)
            .select().
                    apis(RequestHandlerSelectors.any()).
                    paths(paths()).
                    build()
            .pathMapping("/")
            .apiInfo(apiInfo())
            .genericModelSubstitutes(ResponseEntity.class)
            .alternateTypeRules(newRule(
                    typeResolver.resolve(DeferredResult.class, typeResolver.resolve(ResponseEntity.class, WildcardType.class)),
                    typeResolver.resolve(WildcardType.class)
            ));

...以及实际的 API 端点声明:

@RequestMapping(method = RequestMethod.POST, produces = "application/json")
@ApiOperation(value = "Create a new enrolment", code = 201)
@ApiResponses(value = {
        @ApiResponse(code = 201, message = "New enrolment created",
                responseHeaders = @ResponseHeader(name = "Location", description = "The resulting URI of the newly-created enrolment", response = String.class))})
@ResponseStatus(HttpStatus.CREATED)
public ResponseEntity<Void> saveNewEnrolment(@ApiParam(value = "Enrolment to save", required = true) @RequestBody final Enrolment enrolment) {
    // implementation code removed; "location" header is created and returned
    return ResponseEntity.created(location).build();
}

最佳答案

尝试添加 @ResponseStatus(HttpStatus.CREATED)@ResponseStatus(HttpStatus.NO_CONTENT) 注释。取自here

关于java - Springfox Swagger 将响应状态 200 添加到 POST 和 PUT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40702699/

相关文章:

apigee - Swagger:路径参数问题

java - android 通过Jsoup迭代HTML页面中的数据

java - 如何使用 Spring Framework 在 RabbitMQ Java 配置类中设置多个主题?

java - 关于Struts2中的Action映射 - 没有映射Action

java - NoClassDefFound错误: org/springframework/jdbc/core/RowMapper

java - Spring 应用程序运行失败

javascript - 如何在 swagger UI 中添加自定义授权?

java - 如何知道字符串路径是 Web URL 还是基于文件

JavaFX + Spring Boot + JPA-NullPointerException

spring - 更正应用程序的类路径,使其包含一个兼容的 org.springframework.plugin.core.PluginRegistry 版本