**Swagger:**最流行的Api框架
RestFul Api文档在线自动生成工具——>Api文档与API定义同步更新
直接运行,可以在线测试API接口,支持多种语言,
Swagger用处:自动生成接口文档,接口文档实时更新,可以给一些比较难理解的类或视图注释信息。
使用Swagger需要springfox;
Swagger2
Swagger ui
SpringBoot集成Swagger:
1、导入springfox两个包:springfox-swagger2、springfox-swagger-ui
io.springfox
springfox-swagger2
2.9.2
io.springfox
springfox-swagger-ui
2.9.2
2、写配置类:SwaggerConfig :使用@Configuration修饰为配置类,
@EnableSwagger2注解修饰,开启Swagger2服务
@Configuration
@EnableSwagger2 //开启Swagger2服务
public class SwaggerConfig {
// @Bean
// public Docket docket1(){
// return new Docket(DocumentationType.SWAGGER_2).groupName("管理员权限管理");
// }
// @Bean
// public Docket docket2(){
// return new Docket(DocumentationType.SWAGGER_2).groupName("客户管理");
// }
//一个Docket bean代表一个分组
//配置了Swagger的Bean实例
@Bean
//需要获取当前环境变量参数时,才需要Environment参数注入
public Docket docket(Environment environment){
//返回一个带环境变量的Profiles,即设置要显示的Swagger环境
// Profiles profiles = Profiles.of("dev","test");
//判断是否处在自己设定的环境变量(dev或test)
// boolean flag = environment.acceptsProfiles(profiles);
// System.out.println(flag);
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
//.enable(flag)//配置是否启用swagger服务,false不能启用,默认就是true
.groupName("员工管理")//配置API文档分组名称
.select()
//RequestHandlerSelectors配置要扫描接口的方式
// .basePackage:指定要扫描的包
// .any扫描所有的包
//.withClassAnnotation:扫描类上的注解
//.withMethodAnnotation:扫描方法上的注解
.apis(RequestHandlerSelectors.basePackage("com.fkjava"))
//.paths:过滤路径,只扫描gaoshan包下的路径
.paths(PathSelectors.any())
.build();
}
//配置Swagger信息 ApiInfo
private ApiInfo apiInfo(){
Contact contact = new Contact("高山", "http://www.gaoshan.com", "416951514@qq.com");
//参数:标题、描述、版本、URL、作者信息 后三个默认配置
return new ApiInfo("万能 Api Documentation",
"全智能Api Documentation",
"1.0",
"http://www.gaoshan.com",
contact,
"Apache 2.0",
"http://www.apache.org/licenses/LICENSE-2.0",
new ArrayList());
}
}
3、使用Swagger的注解修饰类或方法,描述类或方法的一些信息:
@Api(value=“用户controller”,tags={“用户操作接口”})
Api 用在类上,说明该类的作用。可以标记一个Controller类做为swagger 文档资源
@ApiOperation(value=“获取用户信息”,notes=“注意问题点”,httpMethod=“GET”)
用在方法上,说明方法的作用,每一个url资源的定义,使用方式
@ApiImplicitParams({@ApiImplicitParam(name=“id”,value=“用户id”,dataType=“Long”, paramType = “path”)})
参数说明
@ApiIgnore():忽略方法
@ApiModel(“用户实体”)修饰pojplist,//给生成的Model加注释说明
@ApiModelProperty(“用户名”)修饰pojo类的属性。