阻止Swagger自动添加某些模型

时间:2019-03-14 16:41:02

标签: java spring-boot swagger swagger-ui

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

Models

但是,大多数都是多余的。其中,仅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);
}

控制器:

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

4 个答案:

答案 0 :(得分:0)

您可以使用@ApiModelProperty的{​​{3}}属性来隐藏模型的任何特定属性。没有全局设置。

一旦您声明了swagger扫描的基本软件包,swagger就会为您立即提供该软件包中所有组件的定义。但是,通过正确使用一组摇号annotations,您可以覆盖/自定义摇号文档。

请遵循这些出色的教程(hidden1)来熟悉最有用的注释和用法。

  

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

答案 1 :(得分:0)

Springfox Swagger2通过GET / v2 / api-docs获取UI数据,该映射将映射到springfox.documentation.swagger2.web.Sw​​agger2Controller.getDocumentation()。因此,您可以创建一个bean来代替“ ServiceModelToSwagger2Mapper”:

@Primary
@Component
class CustomServiceModelToSwagger2Mapper : ServiceModelToSwagger2MapperImpl() {
    @Autowired
    private lateinit var modelMapper: ModelMapper
    @Autowired
    private lateinit var parameterMapper: ParameterMapper
    @Autowired
    private lateinit var securityMapper: SecurityMapper
    @Autowired
    private lateinit var licenseMapper: LicenseMapper
    @Autowired
    private lateinit var vendorExtensionsMapper: VendorExtensionsMapper

    override fun mapDocumentation(from: Documentation?): Swagger? {
        if (from == null) {
            return null
        }

        val swagger = Swagger()

        swagger.vendorExtensions = vendorExtensionsMapper.mapExtensions(from.vendorExtensions)
        swagger.schemes = mapSchemes(from.schemes)
        swagger.paths = mapApiListings(from.apiListings)
        swagger.host = from.host
// ➡➡➡➡➡➡➡➡➡➡➡➡➡➡➡➡ here
        swagger.definitions = this.modelsFromApiListings(from.apiListings)
        swagger.securityDefinitions = securityMapper.toSecuritySchemeDefinitions(from.resourceListing)
        val info = fromResourceListingInfo(from)
        if (info != null) {
            swagger.info = mapApiInfo(info)
        }
        swagger.basePath = from.basePath
        swagger.tags = tagSetToTagList(from.tags)
        val list2 = from.consumes
        if (list2 != null) {
            swagger.consumes = ArrayList(list2)
        } else {
            swagger.consumes = null
        }
        val list3 = from.produces
        if (list3 != null) {
            swagger.produces = ArrayList(list3)
        } else {
            swagger.produces = null
        }

        return swagger
    }

    private fun fromResourceListingInfo(documentation: Documentation?): ApiInfo? {
        if (documentation == null) {
            return null
        }
        val resourceListing = documentation.resourceListing ?: return null
        return resourceListing.info ?: return null
    }


    /**
     * @see ModelMapper
     */
    internal fun modelsFromApiListings(apiListings: Multimap<String, ApiListing>): Map<String, Model>? {
        val definitions = newTreeMap<String, springfox.documentation.schema.Model>()
        for (each in apiListings.values()) {
// ➡➡➡➡➡➡➡➡➡➡➡➡➡➡➡➡ here
            // definitions.putAll(each.models)
            definitions.putAll(each.models.filter {
                it.value.qualifiedType.startsWith("com.cpvsn")
                        && it.value.type.typeBindings.isEmpty    
            })
        }
        return modelMapper.mapModels(definitions)
    }
}

答案 2 :(得分:0)

我能够通过在 genericModuleSubstitutes() 中添加 Docket() 来实现这一点。

示例:

.genericModelSubstitutes(ResponseEntity.class) 会将 ResponseEntity <MyModel> 替换为 MyModel。

Reference

答案 3 :(得分:0)

您可以使用:

@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 中。