1.引入swagger依赖
<!-- swagger -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
2.可以引入第三方管理界面
由于swagger原生API管理界面不是很好操作,所以可以借助第三方API接口管理工具
bootstrap-ui:
<!-- bootstrap-ui -->
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>swagger-bootstrap-ui</artifactId>
<version>1.9.3</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-bean-validators</artifactId>
<version>2.9.2</version>
</dependency>
knife4j:
<!-- knife4j -->
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring-boot-starter</artifactId>
<version>2.0.2</version>
</dependency>
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
</dependency>
以上两个都是管理API接口比较好的工具,二者选其一就好
3.编写 Swagger2Config 配置类
@Configuration
@EnableSwagger2
public class Swagger2Config implements WebMvcConfigurer {
/**
* 展示该项目模块下所有api接口
* @return
*/
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo()).groupName("全部")
.select()
// 为当前包路径
.apis(RequestHandlerSelectors.basePackage("com.example.nosql")).paths(PathSelectors.any())
.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
.build();
}
/**
* 展示该项目下指定模块的api接口,项目中的业务模块可直接添加配置并调用此方法
* @param Apiname
* @param ScanUrl
* @return
*/
public Docket scanningApi(String Apiname, String ScanUrl) {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo()).groupName(Apiname)
.select()
//此包路径下的类,才生成接口文档
.apis(RequestHandlerSelectors.basePackage(ScanUrl))
//加了ApiOperation注解的类,才生成接口文档
.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
// 页面标题
.title("SpringBoot使用Swagger2构建RESTful API")
// 创建人信息
.contact(new Contact("MrWang", "https://www.cnblogs.com/zs-notes/category/1258467.html", "1633639075@qq.com"))
// 版本号
.version("1.0")
// 描述
.description("后台API接口")
.build();
}
@Bean
public Docket createWgApi() {
return this.scanningApi("用户操作", "com.example.nosql.mongo.controller");
}
@Bean
public Docket createCrawlerApi() {
return this.scanningApi("爬取数据", "com.example.nosql.elasticsearch.controller");
}
}
如果使用 bootstrap-ui 管理界面,则在配置类上添加 @EnableSwaggerBootstrapUI
如果使用 knife4j 管理界面,则在配置类上添加 @EnableKnife4j 和 @Import(BeanValidatorPluginsConfiguration.class)
4.修改启动类
为了快速打开API接口管理界面,可以对启动类做一下修改
- 在启动类上添加 @Slf4j 注解
- 修改main方法
public static void main(String[] args) throws UnknownHostException {
ConfigurableApplicationContext applicationContext = SpringApplication.run(NosqlApplication.class, args);
Environment environment = applicationContext.getEnvironment();
String ip = InetAddress.getLocalHost().getHostAddress();
String port = environment.getProperty("server.port");
log.info("\n----------------------------------------------------------\n\t" +
"Doc-ui: \t\thttp://" + ip + ":" + port + "/doc.html\n\t" +
"Swagger-ui: \thttp://" + ip + ":" + port + "/swagger-ui.html\n" +
"----------------------------------------------------------");
}