1.准备工作(搭建一个springboot项目,其中创建一个类中的方法使用@RequestMapping注解)
2、导入Swagger2需要的依赖(pom.xml)
<!--加入Swagger2的依赖-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.2.2</version>
</dependency>
3.构建Swagger2的配置类
@Configuration //声明是配置类
@EnableSwagger2 //开启Swagger2
public class Swagger2 {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.zzu.demo")) //自己项目中需要生成api文档的类的根目录
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("Spring Boot中使用Swagger2构建RESTful APIs")
.description("描述")
.termsOfServiceUrl("http://www.baidu.com/")
.contact("LKS")
.version("1.0")
.build();
}
}
4.编写文档描述
5.启动项目,访问localhost:8989/swagger-ui.html(端口号视个人项目而定)
6.查看自定义的接口文档