Swagger - Springfox always generates some response messages (401,403...) by default. How can I remove them?
Asked Answered
S

3

35

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
Strother answered 5/5, 2015 at 7:52 Comment(0)
E
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
  ...;
Edmead answered 6/5, 2015 at 0:37 Comment(3)
This does remove the undocumented response codes, But doesn't remove 200 codeArgueta
@Argueta : 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)"Brockman
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?Walk
A
6

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

springdoc:
  override-with-generic-response: false
Aron answered 27/1, 2022 at 18:26 Comment(2)
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?Wiegand
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.Bromide
G
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.

Gillett answered 14/8, 2020 at 9:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.