3

I'm using Swagger annotations and SpringFox to produce a Swagger spec for my REST API (being built with Sprint Boot). I'm annotating each method with the @ApiResponse codes that will be returned. For example:

@DeleteMapping("/{pipelineName}")
@ApiOperation(value = "Delete a specific pipeline")
@ApiResponses({
    @ApiResponse(code = 204, message = "Pipeline has been deleted"),
    @ApiResponse(code = 404, message = "Pipeline does not exist")
})
public void deletePipeline(@PathVariable("pipelineName") @ApiParam(value = "Name of pipeline", required = true) String name){
    ...
}

However, something (I assume SpringFox) is adding a 200 response code to every API call regardless of whether it's defined or not. I'm aware that I can remove this by adding a @ResponseStatus annotation to each method, but a) that seems like unneccessary duplication of the @ApiResponse definitions and b) some methods could return one of multiple 2xx codes.

Is there a way or removing the 200 code that is added by default?

James Baker
  • 1,143
  • 17
  • 39

4 Answers4

8

I believe you need to define the default response status for your method as you mentioned in your question.@ApiResponse is from Springfox, @ResponseStatus is from Spring framework. It is not exactly a duplication. An HTTP 200 response is the default behavior, and Springfox do not do anything to change that, so it just adds the additional codes on top of the automatically detected one.

Example for HTTP 204:

@ResponseStatus(value = HttpStatus.NO_CONTENT)

In your code:

DeleteMapping("/{pipelineName}")
@ApiOperation(value = "Delete a specific pipeline")
@ApiResponses({
    @ApiResponse(code = 204, message = "Pipeline has been deleted"),
    @ApiResponse(code = 404, message = "Pipeline does not exist")
})
@ResponseStatus(value = HttpStatus.NO_CONTENT)
public void deletePipeline(@PathVariable("pipelineName") @ApiParam(value = "Name of pipeline", required = true) String name){
    ...
}

For others that may be checking this topic: if Springfox is generating other HTTP codes, you may also need this.

saulotoledo
  • 1,737
  • 3
  • 19
  • 36
0

What I would do is:

    @DeleteMapping("/{pipelineName}")
    @ApiOperation(value = "Delete a specific pipeline")
    @ApiResponses({
        @ApiResponse(code = 204, message = "Pipeline has been deleted"),
        @ApiResponse(code = 404, message = "Pipeline does not exist")
    })
    public @ResponseBody ResponseEntity<Object> deletePipeline(@PathVariable("pipelineName") @ApiParam(value = "Name of pipeline", required = true) String name){
        if (deleted) {
       return new ResponseEntity<>("Pipeline has been deleted", HttpStatus.CREATED); //code 204
} else {
return new ResponseEntity<>("Pipeline does not exist", HttpStatus.NOT_FOUND); //code 404
}
    }

Now, I wouldn't use 204 as a code to indicate that something was deleted, I would keep it at 200, but that's up to you.

Hope this helps!

Syrup72
  • 116
  • 1
  • 12
0

Currently You can't remove standard 200 code. I found the same question on the github and developers said that is bug in springfox:

You can read about this in this link

I tried to remove the 200 code in 2.9.2 version of SpringFox and this bug is still exists

lentek91
  • 1
  • 1
  • From Review: Hi, while links are great way of sharing knowledge, they won't really answer the question if they get broken in the future. Add to your answer the essential content of the link which answers the question. In case the content is too complex or too big to fit here, describe the general idea of the proposed solution. Remember to always keep a link reference to the original solution's website. See: [How do I write a good answer?](https://stackoverflow.com/help/how-to-answer) – sɐunıɔןɐqɐp Nov 04 '19 at 10:48
0

You need to mention useDefaultResponseMessages to false while creating the Docket object. Now it will document only that you have added in @ApiResponses.

new Docket(DocumentationType.SWAGGER_2)
            .select()
            .apis(RequestHandlerSelectors.basePackage("com.ty"))
            .build()
            .apiInfo(apiInfo)
            .useDefaultResponseMessages(false);
Vikas s kumar
  • 287
  • 3
  • 3