2016-11-20 226 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

相关问题