2016-11-20 67 views
3

我正在使用springfox-swagger2版本2.6.1,它自動爲PUT和POST操作插入HTTP 200響應消息,儘管我嘗試將它配置爲不這樣做(我沒有對POST或PUT使用響應狀態200,而是分別使用201和204);見下圖:Springfox Swagger將響應狀態200添加到POST和PUT

enter image description here

我已經看到了答案,這裏筆者建議增加一個@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(); 
} 

回答

0

@RequestMapping註釋去掉

produces = "application/json" 

一部分,因爲你的反應是Void.class

相關問題