gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-30 23:02:05 25 4
gpt4 key购买 nike

我正在使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com