第一:在pom.xml引入swagger的jar包如下:
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.6.1</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.6.1</version>
</dependency>
第二:在应用启动器类同级下创建一个SwaggerConfig类如下:
package com.boot.dp;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
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;
@Configuration
public class SwaggerConfig {
@Bean --如果有多个controller文件夹 可以编写多个该方法即可(注意:方法名不能相同)
public Docket SystemBaseApi() {
return new Docket(DocumentationType.SWAGGER_2)
.groupName("某某模块API接口文档")
.apiInfo(apiInfo())
.select()
//为当前包路径
.apis(RequestHandlerSelectors.basePackage("com.boot.dp.modules.base.controller"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
//页面标题
.title("项目标题")
//创建人
.contact(new Contact("创建人名称", "网址", "邮件"))
//描述
.description("模块controller类描述")
//版本号
.version("1.0")
.build();
}
}
第三:在某个controller文件包下某个controller方法编写相应的api接口描述说明和传参数个数和方法类型等如下:
/**
* 列表
* @param params
* @return
*/
@ApiOperation(value="某个模块下某个菜单-列表",notes="在swagger页面上某个controller方法描述")
@ApiImplicitParam(name="参数名称",value="参数所需要输入值",required=true,dataType="参数类型格式")
@RequestMapping(value="/方法名",method=接收方法类型(get或psot))
public Page<BsCustomerEntity> list(@RequestBody Map<String, Object> params) {
return bsCustomerService.listBsCustomer(params);
}
第四:最后在系统添加一个访问api接口连接如下: