问题
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"));
}
回答1:
You can use hidden attribute of @ApiModelProperty to hide any specific property of the model. There is no global setting for it.
Once you have base-package for swagger scan declared, swagger will generate definitions for all components in the package out of box for you. However, with correct use of set of swagger annotations, you can override/customize your swagger documentation.
Please follow these great tutorials (1, 2) to familiarize yourself with most useful annotations & usage.
@Api, @ApiOperation, @ApiResponses, @ApiParam, @ApiIgnore, @ApiModel, @ApiModelProperty etc
回答2:
Springfox Swagger2 acquire UI data through GET /v2/api-docs, which will mapping to springfox.documentation.swagger2.web.Swagger2Controller.getDocumentation().So you can just create a bean to take place of '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)
}
}
来源:https://stackoverflow.com/questions/55167859/prevent-swagger-from-automatically-adding-some-models