翻译:大摇大摆;神气十足
读音: [ˈswæɡər] 人称:(丝袜哥)
版本:swagger3比swagger2相比,配置更少,使用更加方便
官网:https://swagger.io/ (API Development for Eveyone)
在线编辑器:http://editor.swagger.io/
作用:简单但功能强大的API表达工具
配置或修改源码,达到对应功能:
- Swagger Codegen
- Swagger UI
- Swagger Editor
- Swagger Inspector
- Swagger Hub
目前 版本使用 2.5.5
2.2 引入依赖org.springframework.boot spring-boot-starter-parent 2.5.5
io.springfox springfox-boot-starter 3.0.0
整体个pom.xml如下
2.3 开启Swagger4.0.0 org.example swagger 1.0-SNAPSHOT 1.8 1.8 org.springframework.boot spring-boot-starter-parent 2.5.5 io.springfox springfox-boot-starter 3.0.0 org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test test org.springframework.boot spring-boot-maven-plugin 2.5.5 true
在SpringBoot的启动类 添加**@EnableOpenApi**注解,开启Swagger支持
@EnableOpenApi
@SpringBootApplication
public class Starter extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(Starter.class);
}
}
2.4 新建 Controller.java控制器类
@RestController
public class HelloWorldController {
@RequestMapping("/helloWorld")
public String helloWorld(){
return "helloWorld";
}
}
2.5 启动测试 1 hello返回结果@RestController注解相当于@ResponseBody + @Controller合在一起的作用
localhost:8080/hello 没有问题可进行下一步
http://localhost:8080/swagger-ui/
注意,这个网址一定路径要写齐全 http://localhost:8080/swagger-ui/
结构如下
- 分组定义信息 区块
- API文档上信息区块
- 接口定义信息区块
@Api(tags="hello类测试")
@RestController
public class HelloController {
@ApiOperation("测试方法")
@RequestMapping("hello")
public String hello(){
return "helloWorld";
}
}
效果
用于请求的类上,表示对类的说明
- tags=“说明该类的作用,可以在ui界面上看到的注解”
- value=“该参数没什么意义,在UI界面上也看到,所以不需要配置”
用在请求的方法上,说明方法的用途、作用
- value=“说明方法的用途、作用”
- notes=“方法的备注说明”
用在请求的方法上,表示一组参数说明
@ApiImplicitParam:用在@ApiImplicitParams注解中,指定一个请求参数的各个方面
name:参数名
value:参数的汉字说明、解释
required:参数是否必须传
paramType:参数放在哪个地方
header --> 请求参数的获取:@RequestHeader
query --> 请求参数的获取:@RequestParam
path(用于restful接口)–> 请求参数的获取:@PathVariable
div(不常用)
form(不常用)
dataType:参数类型,默认String,其它值dataType=“Integer”
defaultValue:参数的默认值
3.4 @ApiResponses:用在请求的方法上,表示一组响应
@ApiResponse:用在@ApiResponses中,一般用于表达一个错误的响应信息
- code:数字,例如400
- message:信息,例如"请求参数没填好"
- response:抛出异常的类
用于响应类上,表示一个返回响应数据的信息
(这种一般用在post创建的时候,使用@RequestBody这样的场景,
请求参数无法使用@ApiImplicitParam注解进行描述的时候)
@ApiModelProperty:用在属性上,描述响应类的属性
3.6 实例一 参数注释说明- @ApiImplicitParams
- @ApiImplicitParam
@GetMapping("query")
@ApiImplicitParams({
@ApiImplicitParam(name = "name",value = "姓名",required = true,paramType = "query"),
@ApiImplicitParam(name = "age",value = "年龄",required = true,paramType = "query",dataType = "Integer")
})
@ApiOperation("测试查询")
public String search(String name,Integer age){
return name+":"+age;
}
效果
实例二 实体注释说明- @ApiModel
- @ApiModelProperty
新建 User类
@ApiModel("用户信息实体")
public class User {
@ApiModelProperty("编码")
private Integer id;
@ApiModelProperty("姓名")
private String name;
@ApiModelProperty("年龄")
private Integer age;
public User() {
}
public User(Integer id,String name, Integer age) {
this.id=id;
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + ''' +
", age=" + age +
'}';
}
}
controller类
@PostMapping("add")
@ApiOperation("测试添加")
public String add(User user){
return user.getName()+":"+user.getAge();
}
效果
实例三 响应码- @ApiResponses
- @ApiResponse
@GetMapping("/user/{id}")
@ApiOperation("根据ID获取用户信息")
@ApiImplicitParams({@ApiImplicitParam(name="id",value = "用户编号",required = true,paramType ="path")})
@ApiResponses({
@ApiResponse(code=408,message = "错误1" ),
@ApiResponse(code=400,message = "错误2" ),
@ApiResponse(code=404,message = "错误3" )
})
public User load(@PathVariable("id") Integer id){
return new User(id,"jack",32);
}
效果
Swagger-ui图形客户端提供了接口测试功能;
- 点击-Try it out
- 点击-Execute
源码
springfox.documentation.service.ApiInfo.java
static {
DEFAULT = new ApiInfo(
"Api documentation",
"Api documentation",
"1.0",
"urn:tos",
DEFAULT_CONTACT,
"Apache 2.0",
"http://www.apache.org/licenses/LICENSE-2.0", new ArrayList());
}
springfox.documentation.spring.web.plugins.Docket.java
@Configuration
public class Swagger3Config {
@Bean
public Docket createRestApi() {
return new Docket(documentationType.OAS_30) // 指定swagger3.0版本
.groupName("开发组001")
.select()
.apis(RequestHandlerSelectors.basePackage("org.example.controller")) // 指定扫描的包 常用方式
.build()
.apiInfo(createApiInfo());
}
@Bean
public ApiInfo createApiInfo(){
return new ApiInfo(
"Api documentation", //标题
"XX项目Api文档",//描述
"3.0", //版本
"https://www.kszs.xyz",//团队链接
new Contact("小明", "https://www.kszs.xyz", "123456789@qq.com"),
"Apache 2.0",
"https://www.apache.org/licenses/LICENSE-2.0",
new ArrayList());
}
这些API信息主要作用是让前端开发人员看的,谁开发的接口,或者那个小组负责,有问题方便联系沟通。
Docket开关|过滤|分组 配置详解 开关设置 enable开发环境才会用到swagger;正式环境需要关闭swagger;
原因:一个是安全问题,另外一个它会影响系统运行速度
enable
@Bean
public Docket createRestApi() {
return new Docket(documentationType.OAS_30) // 指定swagger3.0版本
.enable(false) //关闭
.apiInfo(createApiInfo());
}
设置过滤
apis (必选有调用select) 参数:
-
any 所有都有效
-
none都无效
-
withClassAnnotation 根据类注解
-
withMethodAnnotation 根据方法注解
-
basePackage根据包路径(主要)
@Bean
public Docket createRestApi() {
return new Docket(documentationType.OAS_30) // 指定swagger3.0版本
.select()
.apis(RequestHandlerSelectors.basePackage("org.example.controller"))
.apiInfo(createApiInfo());
}
最后都要加build方法
还有一个 paths方法 参数有
- any 匹配任意路径
- none都不匹配
- regex正则匹配
.apis(RequestHandlerSelectors.basePackage("/org/example/**"))
设置分组
groupName
.groupName("开发组001")
结合过滤,通过apis的basePackage方法,弄2个组,分别扫描不同包路径,模拟分组开发
@GetMapping是一个组合注解,是@RequestMapping(method = RequestMethod.GET)的缩写
@PostMapping是一个组合注解,是@RequestMapping(method = RequestMethod.POST)的缩写
1、get方式的安全性较Post方式要差些,包含机密信息的话,建议用Post数据提交方式;
2、在做数据查询时,建议用Get方式;而在做数据添加、修改或删除时,建议用Post方式;
源码下载:https://download.csdn.net/download/Linlietao0587/70087229



