34

I have controller as simple as this:

@RequestMapping(value="/async/data", method=RequestMethod.GET, produces="application/json")
@ApiOperation(value = "Gets data", notes="Gets data asynchronously")
@ApiResponses(value={@ApiResponse(code=200, message="OK")})
public Callable<List<Data>> getData(){
    return ( () -> {return dataService.loadData();} );
}

I was expecting to have only a response message for HTTP status 200. However springfox always generates the ones below (401, 403, 404). How can I disable (not show) them?

async-rest-controller Show/Hide List Operations Expand Operations
GET /async/data Gets data

Implementation Notes
Gets data asynchronously

Response Class (Status 200)
ModelModel Schema
{}

Response Content Type 

Response Messages
HTTP Status Code    Reason  Response Model  Headers
401 Unauthorized        
403 Forbidden       
404 Not Found
saulotoledo
  • 1,737
  • 3
  • 19
  • 36
codependent
  • 23,193
  • 31
  • 166
  • 308

3 Answers3

54

You should be able to set up the plugin to not use the default response messages. Follow below instructions for different versions.

For 1.0.2 or prior

  new SwaggerSpringMvcPlugin(...)
        //More config
        .useDefaultResponseMessages(false) //<-- this should be false
  ...;

For 2.x

  new Docket()
        //More config
        .useDefaultResponseMessages(false) //<-- this should be false
  ...;
Supun Wijerathne
  • 11,964
  • 10
  • 61
  • 87
Dilip Krishnan
  • 5,417
  • 3
  • 37
  • 53
  • 2
    This does remove the undocumented response codes, But doesn't remove 200 code – warrior107 Jun 11 '20 at 10:33
  • 3
    @warrior107 : if you use 2.x and a recent version of spring, add this annotation on your controller endpoint: @ResponseStatus(HttpStatus.CREATED) Or whatever response code it should return. In swagger 2.x, 200 should not be in the list at the bottom, only at the top of each endpoint's descritpion: "Response class (Status XXX)" – Pierre Sep 10 '20 at 17:54
  • This approach removes all the default responses for all the resources. Is there a way to remove a particular default response, 201, from a particular POST endpoint? – Treefish Zhang Oct 07 '22 at 00:00
4

In addition to using

new Docket().useDefaultResponseMessages(false)

you may also need to use this annotation depending on the status code you want to return:

@ResponseStatus(HttpStatus.CREATED)

⚠️ Don't use ResponseEntity with WebFlux as that will always add the 200 code. See this github issue.

Minas Mina
  • 2,058
  • 3
  • 21
  • 35
4

With OpenAPI 3, you just need to add this property to the applications.yml (or similar)

springdoc:
  override-with-generic-response: false
Hugo Dias
  • 341
  • 3
  • 13
  • I was looking for a solution in springdoc and found yours. But this doesn't work. Adding this in properties file brings no change. Can you refer me the doc where you find this? – Bablu Mar 03 '22 at 11:49
  • It works with default `400 Bad Request` and `200 OK` responses! I'm working with the latest OpenAPI version at this moment designed as a dependency for Java (2.1.0) + Spring Boot 3.x. With that property it hides/eliminates all not documented endpoints. – mindOf_L May 04 '23 at 12:04