以下都是在SpringBoot项目的基础上进行,就选了SpringWeb
首先在pom.xml 中导入swagger依赖,具体可以去百度maven查找依赖信息
<!-- 导入swagger2和swagger ui依赖-->
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
导入swagger依赖后,在程序主入口加以下代码,意思是每次运行都会自动更新并生成SwaggerApi文档
package com.example.swagger;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SwaggerDemoApplication {
public static void main(String[] args) {
//每次运行都会自动生成api文档
//加此行代码
SpringApplication.run(SwaggerDemoApplication.class, args);
}
}
下一步,建一个swaggerconfig类,即swagger的配置类,在这里面配置小组信息,实际开发过程中,通常有多个小组分别负责多个功能的开发,那么每个小组负责开发的功能通过这个swaggerconfig类的设置就可以将各个小组的功能分开展示在API文档中
package com.example.swagger.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.RequestHandler;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import javax.print.Doc;
import java.util.ArrayList;
@Configuration
@EnableSwagger2 //开启Swagger2
public class swaggerconfig {
//配置了swagger的Docket的bean实例
@Bean
public Docket docket(){
return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo())
.select()
//RequestHandlerSelectors 配置要扫描接口的方式
//指定要扫描的包
//.any()扫描全部
//.none()都不扫描
.apis(RequestHandlerSelectors.basePackage("com.example.swagger.controller"))
.build();//build
}
//有几个小组协作就有几个Docket,并且要配到Bean中,托管给Spring,不然扫描不到,通过swaggerApi文档下拉选择各个小组的部分
@Bean
public Docket docket1(){
return new Docket(DocumentationType.SWAGGER_2).groupName("a");
}
//每个组通过以下方法配置小组的个人页面
//配置swagger信息=apiInfo
private ApiInfo apiInfo(){
//作者信息
Contact contact= new Contact("xxx", "", "18888888");
return new ApiInfo(
"我的Swagger-Api日记",
"我的Api Documentation",
"1.0",
"urn:tos",
contact,
"Apache 2.0",
"http://www.apache.org/licenses/LICENSE-2.0",
new ArrayList());
}
}
有几个小组就有几个Docket,每个Docket都需要配置到Bean中,托管给Spring,否则扫描不到,上面的代码块中,有两个Docket,一个docket1,一个docket,这就是两个组,那么怎么把每个组和每个组开发的包绑定起来,就是用 Dpcket的.apis(RequestHandlerSelectors.basePackage(“com.example.swagger.controller”))这个方法,先看一下我的项目图
通过.api的方法绑定当前这个docket(即当前小组)负责开发的包名
显示到Api文档是这个效果,可以看到有两个组名
private ApiInfo apiInfo()这个方法是用来定义当前这个小组的个人页面,相当于当前小组的具体信息用的,在new Docket的时候调用进去即可,如图可见,我在第一个docket中定义了小组信息和绑定所负责的包名,Api文档中就显示了绑定的com.example.swagger.controller这个类里的方法,在ApiInfo方法中定义的小组信息也显示在Api文档中,
下面是在小组所负责的类中如何给代码添加注释并显示在Api文档中
给方法加注释:
@ApiOperation("输入用户信息")//这行是给方法写注释,注释会显示在swaggerAPi文档中
在方法前面添加这个语句即可,具体可以看上面的代码块
给方法中的参数添加注释:
@ApiParam("用户名") String name
在所定义的参数前面直接添加这个语句即可,具体可以看上面的代码块
显示在Api文档中的效果如下图
以上是方法注释
以上是参数注释
这样就能将注释显示在Api文档中
PS:
Swagger还能在线测试方法,下面开始以我的User方法为例进行展示
这是方法
点击Try it out并输入参数
点击Execute
就接收到了伪数据
刚学的Swagger,防止自己忘了才写的本博客,swagger的最基本的用法,也可能有错的地方