Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 
like image 350
codependent Avatar asked May 05 '15 07:05

codependent


People also ask

What is Springfox Swagger2?

React + Spring Boot Microservices and Spring Swagger2 is an open source project used to generate the REST API documents for RESTful web services. It provides a user interface to access our RESTful web services via the web browser.

How do I disable Swagger in spring boot?

In Spring, we can use the @Profile annotation to enable or disable the injection of beans. This forces us to be explicit about environments where we want to activate Swagger. It also helps to prevent accidentally turning it on in production.

What is the annotation required to enable the Swagger?

Configuring Swagger 2 in the Application In this configuration class, the @EnableSwagger2 annotation enables Swagger support in the class.


2 Answers

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   ...; 
like image 112
Dilip Krishnan Avatar answered Sep 29 '22 17:09

Dilip Krishnan


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.

like image 25
Minas Mina Avatar answered Sep 29 '22 17:09

Minas Mina