本文共 1651 字,大约阅读时间需要 5 分钟。
swagger的诞生的一个背景是,前后端开发人员的关于API文档交流的一个产物。前端人员要集成相关功能,则必须要有相关API文档。
当然API文档可以自己手写维护,但是存在以下几个问题所以,swagger就很有必要在项目中使用。我们可以通过注解的形式,编写API文档。
io.springfox springfox-swagger2 2.9.2 io.springfox springfox-swagger-ui 2.9.2
Docket
对象)(1)Docket
对象的创建
new Docket(DocumentationType.SWAGGER_2) .host(swaggerProperties.getHost()) .apiInfo(apiInfo(swaggerProperties)) .select() .apis(RequestHandlerSelectors.basePackage(swaggerProperties.getBasePackage())) .paths(Predicates.and(urls)) .build() .securitySchemes(Collections.singletonList(securitySchemes(swaggerProperties))) .securityContexts(Collections.singletonList(securityContexts(swaggerProperties)));
DocumentationType.SWAGGER_2
设置文档类型,源自springfox.documentation.spi.DocumentationType
host
设置暴露端口 select
初始化api选择器ApiSelectorBuilder
apiInfo
设置文档信息 new ApiInfoBuilder() .title(swaggerProperties.getTitle()) // 设置文档标题 .description(swaggerProperties.getDescription()) // 设置文档描述 .version(swaggerProperties.getVersion()) // 设置版本 .build();
paths
设置扫描路径
securitySchemes
配置鉴权参数 securityContexts
设置鉴权的接口 (2) swagger常用注解 (3)Swagger集成思维导图 转载地址:http://hfdlf.baihongyu.com/