I build a REST interface using Spring Boot framework. Then, I use Swagger version 2.9.2 to generate the documentation. As you can see from the photo below, Swagger automatically detects a lot of models.
However, most of them are redundant. Among them, only the ResponseMessage
is necessary, the rest are just standard Java class.
So, my question is: how can I tell Swagger which models to expose?
Here are my Swagger configuration and code snippet of my controller.
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.basePackage("my.package"))
.paths(PathSelectors.any())
.build()
.apiInfo(API_INFO)
.useDefaultResponseMessages(false);
}
Controller:
@PostMapping(value = "/import", produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public ResponseEntity<?> importData(HttpServletRequest request) {
// processing...
return ResponseEntity.created(uri)
.body(new ResponseMessage(HttpStatus.CREATED, "Your data is being processed"));
}
You can use :
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2).select()
.apis(RequestHandlerSelectors.basePackage("com.package"))
.paths(PathSelectors.regex("/api.*")).build().apiInfo(apiInfo())
.ignoredParameterTypes(Timestamp.class);
}
This worked for me. After specifying the class name in ignoredParameterTypes, it was no longer present in swagger ui.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With