3
我正在使用springfox-swagger2版本2.6.1,它自動爲PUT和POST操作插入HTTP 200響應消息,儘管我嘗試將它配置爲不這樣做(我沒有對POST或PUT使用響應狀態200,而是分別使用201和204);見下圖:Springfox Swagger將響應狀態200添加到POST和PUT
我已經看到了答案,這裏筆者建議增加一個@ResponseStatus
註釋到控制器「修理」它類似的問題,但是這成爲僵化和違背Spring自己的文檔關於使用的ResponseEntity
和@ResponseStatus
其餘的API。例子:
How to change the response status code for successful operation in Swagger?
和
https://github.com/springfox/springfox/issues/908
是否有任何其他的方式來迫使Springfox揚鞭不添加此200 OK狀態代碼?
我的文案配置:
@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();
}