防止 Swagger 自动添加某些模型 [英] Prevent Swagger from automatically adding some models

查看:43
本文介绍了防止 Swagger 自动添加某些模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 Spring Boot 框架构建了一个 REST 接口.然后,我使用 Swagger 2.9.2 版来生成文档.从下图可以看出,Swagger 自动检测了很多模型.

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.

然而,它们中的大多数都是多余的.其中,只有ResponseMessage是必须的,其余都是标准Java类.

However, most of them are redundant. Among them, only the ResponseMessage is necessary, the rest are just standard Java class.

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

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

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);
}

控制器:

@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"));
}

推荐答案

您可以使用:

@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);
}

这对我有用.在 ignoredParameterTypes 中指定类名后,它不再出现在 swagger ui 中.

This worked for me. After specifying the class name in ignoredParameterTypes, it was no longer present in swagger ui.

这篇关于防止 Swagger 自动添加某些模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆