SpringBoot的Swagger
前后端分离式时代:后端:后端控制层,服务层,数据访问层前端:前端控制层,视图层伪造后端数据,json已经存在了,不需要后端也能,前端工程也能跑起来前后端如何交互?–》API前后端相对独立,松耦合前后端甚至可以部署在不同服务器上这就产生一个问题:前后端集合联调,前端和后端人员已依旧无法做到"即使协商,尽早解决",最终导致问题爆发解决方案:首先指定schema【计划的提纲】,实时更新最新API,降低集
·
前后端分离式时代:
- 后端:后端控制层,服务层,数据访问层
- 前端:前端控制层,视图层
- 伪造后端数据,json已经存在了,不需要后端也能,前端工程也能跑起来
- 前后端如何交互?–》API
- 前后端相对独立,松耦合
- 前后端甚至可以部署在不同服务器上
这就产生一个问题: - 前后端集合联调,前端和后端人员已依旧无法做到"即使协商,尽早解决",最终导致问题爆发
解决方案: - 首先指定schema【计划的提纲】,实时更新最新API,降低集成的风险
- 早些年指定word计划文档
- 前后端分离
- 前端测试后端接口:postman
- 后端提供接口,需要实时更新最新的消息以及改动
Swagger
- 号称世界上最流行的Api框架
- RestFul Api文档在线自动生成工具=》Api文档与API同步更新
- 直接运行,可以在线测试API接口
- 官网:
https://swagger.io/
SprinBoot集合swagger
新创建个集合web的springboot工程使用方式:
- 导入knife4j的maven坐标
- 导入knife4j的相关配置类
- 设置静态资源,否则接口文档页面无法访问
- 在LoginCheckFilter中设置不需要处理的路径
<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>
编写一个hello工程
package com.example.springboot_swagger.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class Hello {
@RequestMapping("/hello")
public String hello(){
return "hello";
}
}
配置swagger==》config
(SpringBoot版本不应太高,不然容易报错,我改为2.5.2就好了)
package com.example.springboot_swagger.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
@Configuration
@EnableWebMvc //开启swagger2
public class SwaggerConfig {
}
输入网址
http://localhost:8080/swagger-ui.html
配置Swagger信息
package com.example.springboot_swagger.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
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;
@Configuration
@EnableSwagger2 //开启swagger2
public class SwaggerConfig {
//配置了swagger的Docket的bean实例
@Bean
public Docket docket(){
return new Docket(DocumentationType.SWAGGER_2)
// 指定构建api文档的详细信息的方法:apiInfo()
.apiInfo(apiInfo());
}
//配置swagger信息=apiInfo
//构建api文档的详细信息
private ApiInfo apiInfo(){
//作者信息
Contact contact=new Contact("yingcai","https://blog.csdn.net/qq_56299755?type=bbs","1297805120@qq.com");
return new ApiInfoBuilder()
// 设置页面标题
.title("Swagger接口总览")
//设置作者信息
.contact(contact)
// 设置接口描述
.description("你我皆黑马")
// 设置版本
.version("1.0")
// 构建
.build();
}
}
配置扫描接口
//配置了swagger的Docket的bean实例
@Bean
public Docket docket(){
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
//RequestHandlerSelectors:配置要扫描的接口方式
//basePackage:指定要扫描的包
//any:扫描全部
//none:不扫描
//withClassAnnotation:扫描类上的注解,参数是一个反射对象
//withMethodAnnotation:扫描方法上的注解
.apis(RequestHandlerSelectors.basePackage("com.example.springboot_swagger.controller"))
//paths:过滤什么路径
//.paths(PathSelectors.ant("/example/**"))
.build();
}
Docker.select()
配置API文档的分组
分组
.groupName("英才")
如何配置多个小组
在创建多个Docket实例即可
@Bean
public Docket docket1(){
return new Docket(DocumentationType.SWAGGER_2)
.groupName("Yingcai");
}
实体类配置
创建实体类
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
@ApiModel("用户实体类")
public class User {
@ApiModelProperty("用户名")
public String username;
@ApiModelProperty("密码")
public String password;
}
修改controller文件
@Api("Hello控制类")
@RestController
public class Hello {
@GetMapping("/hello")
public String hello(){
return "hello";
}
//只要我们接口中,返回值存在实体类,就会被扫描到swagger中
@PostMapping("/user")
public User user(){
return new User();
}
//operation接口,不是放在类上,是方法
@ApiOperation("Hello控制类")
@GetMapping("/hello2")
public String hello2(@ApiParam("用户名") String username){
return "hello"+username;
}
}
小结
1、我们可以通过Swagger给一些比较难以理解的属性或者接口,增加注释信息
2、接口文档实时更新
3、可以在线测试
PS:再正式发布时候,关闭swagger!!处于安全考虑。而且节约内存
更多推荐
所有评论(0)