Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent Swagger from automatically adding some models

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.

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"));
}
like image 310
Triet Doan Avatar asked Oct 28 '22 17:10

Triet Doan


1 Answers

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.

like image 125
Alisha Raju Avatar answered Oct 31 '22 08:10

Alisha Raju