swagger
api框架
RestFul Api文档在线自动生成工具=>Api文档与 Api定义同步更新
直接运行,可以在线测试Api接口
支持多种语言
官方网站:https://swagger.io/
在项目中的Swagger需要springbox
swagger2
ui
maven坐标:
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
配置spring boot的集成:
import org.springframework.context.annotation.Configuration;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
//配置swagger
@Configuration
@EnableSwagger2
public class Swagger {
}
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-77GiMih6-1607926775496)(image-20201214121116165.png)]
访问路径:http://localhost:8080/swagger-ui.html
//配置了swagge
@Bean
public Docket docket(){
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo());
}
//配置swagger信息
private ApiInfo apiInfo(){
Contact DEFAULT_CONTACT = new Contact("小庆", "baidu.com", "1578442339@qq.ccom");
return new ApiInfo("Swagger的api文档",
"xxxxx",
"1.0",
"baidu.com",
DEFAULT_CONTACT,
"Apache 2.0",
"http://www.apache.org/licenses/LICENSE-2.0",
new ArrayList());
}
swagger配置扫描接口
//配置了swagge
//enable是否启用swagger
@Bean
public Docket docket(){
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
//basePackage 扫描指定的包
//any; 扫描全部
//none; 不扫描
//withclassAnnotation; 扫描类上的主键
//withMetAnnotation
.apis(RequestHandlerSelectors.basePackage("com.swagger.controoler"))
//过滤路径
.paths(PathSelectors.ant("/com/***"))
.build();
}
//配置swagger信息
private ApiInfo apiInfo(){
Contact DEFAULT_CONTACT = new Contact("小庆", "baidu.com", "1578442339@qq.ccom");
return new ApiInfo("Swagger的api文档",
"xxxxx",
"1.0",
"baidu.com",
DEFAULT_CONTACT,
"Apache 2.0",
"http://www.apache.org/licenses/LICENSE-2.0",
new ArrayList());
}
让swagger只在生成环境使用?
swagger的配置:
package com.swagger.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.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;
//配置swagger
@Configuration
@EnableSwagger2
public class Swagger {
//配置了swagge
//enable是否启用swagger
@Bean
public Docket docket(Environment environment){
//设置显示的swagger环境
Profiles profiles=Profiles.of("dev","test");
//通过enviroment。acceptsProfiles 判断是否处在自己设定的环境当中
boolean flag=environment.acceptsProfiles(profiles);
//获取项目的环境;
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.swagger.controoler"))
//过滤路径
// .paths(PathSelectors.ant("/com/***"))
.build();
}
//配置swagger信息
private ApiInfo apiInfo(){
Contact DEFAULT_CONTACT = new Contact("小庆", "baidu.com", "1578442339@qq.ccom");
return new ApiInfo("Swagger的api文档",
"xxxxx",
"1.0",
"baidu.com",
DEFAULT_CONTACT,
"Apache 2.0",
"http://www.apache.org/licenses/LICENSE-2.0",
new ArrayList());
}
}
分组:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-mOpjZiga-1607926775498)(Untitled/image-20201214134538482.png)]
swagger的controller的使用:
@RestController
public class HelloController {
@GetMapping("/hello")
public String hello(){
return "hello";
}
@ApiOperation("User的控制")
// 返回值存在实体类 ta就会 扫描到swagger中去
@PostMapping("/user")
public String user(@ApiParam("用户名") String username){
return username;
}
}
实体类的注释:
@ApiModel("用户实体类") //加注释
public class User {
@ApiModelProperty("用户名")
private String username;
@ApiModelProperty("用户密码")
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
总结:
正式开发的时候不要关闭swagger的使用