SpringBoot集成Swagger


前言

前后端分离时代前端后端的问题: ·前后端集成联调,前端和后端人员无法做到“即时协商,尽早解决”,最终导致问题集中爆发 解决方案: ·首先指定schema【计划的提纲】,实时更新API,降低集成风险 ·前后端分离: ·前端测试后端接口 ·后端提供接口,需要实时更新最新的消息及改动 Swagger ·号称世界上最流行的API框架 ·RestFul Api文档在线自动生成工具=>Api文档与Api定义同步更新 ·直接运行,可以在线测试API接口 ·支持多语言 官网:https://swagger.io/tools/swagger-ui/

一、SpringBoot集成Swagger

·新建一个SpringBoot项目,导入依赖
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>3.0.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>3.0.0</version>
</dependency>

·写一个hello工程
·配置swagger

@Configuration
@EnableSwagger2
public class SwaggerConfig{

}

·测试运行:http://localhost:8080/swagger-ui.html

在这里插入图片描述

二、配置swagger

###swagger的bean实例Docket

@Configuration
@EnableSwagger2
public class SwaggerConfig{

	@Bean
	public Docket docket(){
		return new Docket(Documentation.SWAGGER_2).apiInfo();
	}
	//配置swagger信息=apiInfo
	private ApiInfo apiInfo(){
		//作者信息
		Contact contact = new Contact("秦疆","url","email")
		return new ApiInfo(
				"title",
				"description",
				"version",
				"termsOfServiceUrl",
				contact,
				"license",
				"licenseUrl"
				new ArrayList()
		);
	}
}

Swagger配置扫描接口

Docket.select()

@Configuration
@EnableSwagger2
public class SwaggerConfig{

	@Bean
	public Docket docket(){
		return new Docket(Documentation.SWAGGER_2)
		.apiInfo()
		.enable(true)//是否启动swagger
		.select()
		//RequestHandlerSelectors,配置要扫描接口的方式
		//basePackage,指定要扫描的包,any()扫描全部,none不扫描,withClassAnnotion:扫描类上的注解,withMethodAnnotion:扫描方法上的注解
		.apis(RequestHandlerSelectors.basePackage("包名"))
		//path过滤
		.path(PathSelectors.ant("路径"))
		.build();
	}
	//配置swagger信息=apiInfo
	private ApiInfo apiInfo(){
		//作者信息
		Contact contact = new Contact("秦疆","url","email")
		return new ApiInfo(
				"title",
				"description",
				"version",
				"termsOfServiceUrl",
				contact,
				"license",
				"licenseUrl"
				new ArrayList()
		);
	}
}

提问:我只希望我的swagger在生产环境使用,在发布的时候不使用?
·判断是不是生产环境
·注入enable(flag)


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值