Swagger

Springboot 集成Swagger

Swagger 就是将项目中所有(想要暴露的)接口展现在页面上,并且可以进行接口调用和测试的服务。

集成Swagger

1.创建项目

2.导入相关依赖

<!-- 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>

3.编写hellowContoller

4.编写Swagger配置文件

@Configuration //配置文件
@EnableSwagger //开启Swagger
public Class SwaggerConfig{

}

5.运行测试

http://locahost:8080/swagger-url.html

配置Swagger扫描接口
@Configuration //配置文件
@EnableSwagger //开启Swagger
public Class SwaggerConfig{

//配置多个分组;多个Docket实例即可
@Bean
public Docket docket1(){
	return nnew Docket(DocumentationType.SWAGGET_2).groupName("A");
}
@Bean
public Docket docket2(){
	return nnew Docket(DocumentationType.SWAGGET_2).groupName("B");
}
@Bean
public Docket docket3(){
	return nnew Docket(DocumentationType.SWAGGET_2).groupName("C");
}
//1.配置Swagger的Docket的bean实例
@Bean
public Docket docket(Environment environment){

		//获取项目的环境  用于正式发布时防止接口暴露
		//设置要显示的Swagger环境
		Profiles profiles=Profiles.of("dev","test");
		
		//通过enviroment.acceptsProfiles判断是否在自己设定的环境中
		boolean flag=environment.accptsProfiles(profiles);

    return new Docket(DocumentationType.SAGGER_2)
    .apiInfo(apiInfo())
    //配置API文档的分组
    .groupName("我的")
    //是否启动Swagger ,true启动 ,false未启动 ,flag根据环境判断是否设置启动Swagger
    .enable(flag)
//RequestHandlerSelectors,配置要扫描接口的方式
//basePackge:指定要扫描的包
//any():扫描全部
//none():什么都不扫描
//withClassAnnotation:扫描类上的注解,参数是一个注解的反射对象RestContorller.class
//withMethodAnnotation :扫描方法上得到注解GetContorller.class
 		//RequesHandlerSelectors.withClassAnntation(RestContorller.class) 
 	.select()
    .api(RequesHandlerSelectors.basepackage("com.xie.swagger.controller"))
    //paths():过滤什么路径
   // .paths(PathSelectors.any("/xie/**"))
    .build();
}




//配置Swagger信息=apiInfo
private ApiInfo apiInfo(){
//作者信息
Contact contact =new Contact("名字","地址url","邮箱");
return new ApiInfo("标题","描述","版本","url",
                   Contact,"许可证","许可地址",new ArrayList());
}
}

实体类注释
@ApiOperation
使用于在方法上,表示一个http请求的操作
源码中属性太多,记几个比较常用
value用于方法描述
notes用于提示内容
tags可以重新分组(视情况而用)

@ApiParam
使用在方法上或者参数上,字段说明;表示对参数的添加元数据(说明或是否必填等)
name–参数名
value–参数说明
required–是否必填

@ApiModel()
使用在类上,表示对类进行说明,用于参数用实体类接收
value–表示对象名
description–描述

@ApiModelProperty()
使用在方法,字段上,表示对model属性的说明或者数据操作更改
value–字段说明
name–重写属性名字
dataType–重写属性类型
required–是否必填
example–举例说明
hidden–隐藏

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值