swagger
什么时swagger
号称:世界上最流行的API文档
restful API文档在线自动生成工具,API文档和API定义同时更新
swageer的作用
了解前后端分离
Vue+SpringBoot 实现前后端分离
前端:前端控制层,试图层
用json伪造数据,不需要后端传入数据也可以跑起来
后端:控制层,服务层,数据访问层
前后端如何交互==>API
前后端分离会出现一个问题:就是前端和后端的工作人员联调不一致问题。
如何使用swagger
在项目中使用需要导入jar包
swagger
ui
springBoot集成swagger
1新建项目
2导入依赖
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
3编写一个helloControlelr
4对swagger进行配置
package com.ming.swagger_test.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
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 java.util.ArrayList;
@Configuration
@EnableSwagger2//开启swagger2
public class SwaggerConfig {
//配置swagger的bean实例
@Bean
public Docket docket(){
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo());
}
private ApiInfo apiInfo(){
Contact contact = new Contact("wang","http://baidu.com","11731028010@qq.com");
return new ApiInfo("mingStudy",
"这个人有点帅",
"1.0",
"urn:tos",
contact,
"Apache 2.0",
"http://www.apache.org/licenses/LICENSE-2.0",
new ArrayList<>());
}
}
5测试运行
访问这个地址出现页面
http://localhost:8080/swagger-ui.html
//进行RequestHandlerSelector的配置要扫描接口
进行拦截
如何指定生产,和发布环境进行使用
package com.ming.swagger_test.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import springfox.documentation.RequestHandler;
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;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import java.util.ArrayList;
@Configuration
@EnableSwagger2//开启swagger2
public class SwaggerConfig {
//配置swagger的bean实例
@Bean
public Docket docket(Environment environment){
//设置项目什么环境时候显示swagger
Profiles profiles = Profiles.of("dev","test");
boolean b=environment.acceptsProfiles(profiles);
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
//进行RequestHandleerSelector的配置要扫描接口
//通过basePackage指定扫描的包
.enable(b)//这个配置是否打开swagger页面
.select()
.apis(RequestHandlerSelectors.basePackage("com.ming.swagger_test.controller"))
//配置过滤路径
//.paths(PathSelectors.ant("com.ming/**"))
.build();
}
private ApiInfo apiInfo(){
Contact contact = new Contact("wang","http://baidu.com","11731028010@qq.com");
return new ApiInfo("mingStudy",
"这个人有点帅",
"1.0",
"urn:tos",
contact,
"Apache 2.0",
"http://www.apache.org/licenses/LICENSE-2.0",
new ArrayList<>());
}
}
设置分组
可以进行多个配置分组
自动扫描控制器返回的实体类
最主要的功能进行在线测试
、