防止Swagger自动添加某些模型

Tri*_*oan 5 java swagger swagger-ui spring-boot

我使用 Spring Boot 框架构建 REST 接口。然后,我使用 Swagger 版本 2.9.2 生成文档。从下面的照片中可以看到,Swagger 自动检测了很多模型。

楷模

然而,其中大多数都是多余的。其中,只有 是ResponseMessage必需的,其余的只是标准的 Java 类。

所以,我的问题是:我如何告诉 Swagger 要公开哪些模型

这是我的控制器的 Swagger 配置和代码片段。

@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);
}
Run Code Online (Sandbox Code Playgroud)

控制器:

@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"));
}
Run Code Online (Sandbox Code Playgroud)

Ami*_*mar 0

您可以使用隐藏属性@ApiModelProperty来隐藏模型的任何特定属性。没有针对它的全局设置。

一旦声明了用于 swagger 扫描的基本包,swagger 将为您生成包中所有组件的定义。但是,通过正确使用 swagger 集annotations,您可以覆盖/自定义您的 swagger 文档。

请遵循这些精彩教程 ( 1 , 2 ) 来熟悉最有用的注释和用法。

@Api、@ApiOperation、@ApiResponses、@ApiParam、@ApiIgnore、@ApiModel、@ApiModelProperty 等