Swagger简介
Vue+SpringBoot
后端时代:前端只用管理静态页面;html==>后端。模板引擎 JSP=>后端是主力
后端:后端控制层,服务层,数据访问层【后端团队】
前端:前端控制层,视图层【前端团队】
伪造后端数据,json,已经存在了,不需要后端,前端
- 前后端如何交互?===>API
- 前后端相对独立,松耦合;
- 前后端甚至可以部署在不同的服务器上;
产生一个问题:
- 前后端集成联调,前端人员和后端人啊元无法做到"及时协商,尽早解决",最终导致问题集中爆发;
- 解决方案:
- 首先制定一个schema,实时更新最新API,降低集成的风险;
- 早些年:制定word计划文档;
- 前后端分离:
- 前端测试后端接口:postman
- 后端提供接口,需要实时更新最新的消息及改动!
Swagger
- 号称世界上最流行的API框架
- RestFul Api文档在线自动生成工具=>Api的文档与Api定义同步更新
- 直接运行,可以在线测试API接口;
- 支持多种语言(Java,php);
官网:https://swagger.io/
在项目中使用Swagger需要springbox;
SpringBoot整合Swagger
1.创建1个SpringBoot项目
2.导入Swagger相关依赖 swagger2 ui
</dependency> <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.9.2</version> </dependency> <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.9.2</version> </dependency>
3.编写一个Hello工程
4.配置Swagger==>Config
@Configuration //开启Swagger2工具 @EnableSwagger2 public class SwaggerConfig { }
5.测试运行 http://localhost:8080/swagger-ui.html
6.
配置Swagger实例
配置Swagger的bean实例Docket
package com.wang.springboot07_swagger.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.service.VendorExtension; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.swagger2.annotations.EnableSwagger2; import java.util.ArrayList; @Configuration //开启Swagger2工具 @EnableSwagger2 public class SwaggerConfig { //配置了Swagger的Docket的bean实例 @Bean public Docket docker(){ Docket d=new Docket(DocumentationType.SWAGGER_2); d.apiInfo(apiInfo()); return d; } private ApiInfo apiInfo(){ Contact contact=new Contact("王广元", "https://www.cnblogs.com/WangGuangYuan/", "[email protected]"); return new ApiInfo("王广元的SwaggerApi文档", "尽人事听天命", "1.2", "https://www.cnblogs.com/WangGuangYuan/", contact , "Apache 2.0", "http://www.apache.org/licenses/LICENSE-2.0", new ArrayList<VendorExtension>()); } }
Swagger配置扫描接口
package com.wang.springboot07_swagger.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.bind.annotation.RequestMapping; 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.service.VendorExtension; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.swagger2.annotations.EnableSwagger2; import java.util.ArrayList; @Configuration //开启Swagger2工具 @EnableSwagger2 public class SwaggerConfig { //配置了Swagger的Docket的bean实例 @Bean public Docket docker(){ Docket d=new Docket(DocumentationType.SWAGGER_2); d.apiInfo(apiInfo()); return d .select() //配置要扫描接口的方式 //制定扫描的包 basePackage //any:扫描全部 //none:全部不扫描 //withClassAnnotation 扫描类上的注解 //withMethodAnnotation 扫描方法上的注解 //.apis(RequestHandlerSelectors.basePackage("com.wang.springboot07_swagger.controller")) .apis(RequestHandlerSelectors.withClassAnnotation(RequestMapping.class)) //配置要扫描路径的方法 .paths(PathSelectors.any()) .build(); } private ApiInfo apiInfo(){ Contact contact=new Contact("王广元", "https://www.cnblogs.com/WangGuangYuan/", "[email protected]"); return new ApiInfo("王广元的SwaggerApi文档", "尽人事听天命", "1.2", "https://www.cnblogs.com/WangGuangYuan/", contact , "Apache 2.0", "http://www.apache.org/licenses/LICENSE-2.0", new ArrayList<VendorExtension>()); } }
配置是否启动Swagger
package com.wang.springboot07_swagger.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.bind.annotation.RequestMapping; 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.service.VendorExtension; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.swagger2.annotations.EnableSwagger2; import java.util.ArrayList; @Configuration //开启Swagger2工具 @EnableSwagger2 public class SwaggerConfig { //配置了Swagger的Docket的bean实例 @Bean public Docket docker(){ Docket d=new Docket(DocumentationType.SWAGGER_2); d.apiInfo(apiInfo()) //enable(boolean) 是否启动swagger false:不在浏览器中启动 .enable(false); return d .select() //配置要扫描接口的方式 //制定扫描的包 basePackage //any:扫描全部 //none:全部不扫描 //withClassAnnotation 扫描类上的注解 //withMethodAnnotation 扫描方法上的注解 //.apis(RequestHandlerSelectors.basePackage("com.wang.springboot07_swagger.controller")) .apis(RequestHandlerSelectors.withClassAnnotation(RequestMapping.class)) //配置要扫描路径的方法 .paths(PathSelectors.any()) .build(); } private ApiInfo apiInfo(){ Contact contact=new Contact("王广元", "https://www.cnblogs.com/WangGuangYuan/", "[email protected]"); return new ApiInfo("王广元的SwaggerApi文档", "尽人事听天命", "1.2", "https://www.cnblogs.com/WangGuangYuan/", contact , "Apache 2.0", "http://www.apache.org/licenses/LICENSE-2.0", new ArrayList<VendorExtension>()); } }
Swagger配置绑定SpringBoot配置环境
package com.wang.springboot07_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 org.springframework.web.bind.annotation.RequestMapping; 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.service.VendorExtension; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.swagger2.annotations.EnableSwagger2; import java.util.ArrayList; @Configuration //开启Swagger2工具 @EnableSwagger2 public class SwaggerConfig { //配置了Swagger的Docket的bean实例 @Bean public Docket docker(Environment environment){ //获取项目的环境 //设置要显示的Swagger环境 Profiles profiles=Profiles.of("prod"); //environment.acceptsProfiles 监听是否处在设置的环境当中 boolean b = environment.acceptsProfiles(profiles); Docket d=new Docket(DocumentationType.SWAGGER_2); d.apiInfo(apiInfo()) //enable(boolean) 是否启动swagger false:不在浏览器中启动 .enable(b); return d .select() //配置要扫描接口的方式 //制定扫描的包 basePackage //any:扫描全部 //none:全部不扫描 //withClassAnnotation 扫描类上的注解 //withMethodAnnotation 扫描方法上的注解 //.apis(RequestHandlerSelectors.basePackage("com.wang.springboot07_swagger.controller")) .apis(RequestHandlerSelectors.withClassAnnotation(RequestMapping.class)) //配置要扫描路径的方法 .paths(PathSelectors.any()) .build(); } private ApiInfo apiInfo(){ Contact contact=new Contact("王广元", "https://www.cnblogs.com/WangGuangYuan/", "[email protected]"); return new ApiInfo("王广元的SwaggerApi文档", "尽人事听天命", "1.2", "https://www.cnblogs.com/WangGuangYuan/", contact , "Apache 2.0", "http://www.apache.org/licenses/LICENSE-2.0", new ArrayList<VendorExtension>()); } }
Swagger配置Api文档的分组(多人协同开发)
.groupName("")
测试代码
@Bean public Docket docket(Environment environment){ //获取项目的环境 //设置要显示的Swagger环境 Profiles profiles=Profiles.of("prod"); //environment.acceptsProfiles 监听是否处在设置的环境当中 boolean b = environment.acceptsProfiles(profiles); Docket d=new Docket(DocumentationType.SWAGGER_2); d.apiInfo(apiInfo()) //enable(boolean) 是否启动swagger false:不在浏览器中启动 .enable(b). groupName("王广元"); return d .select() //配置要扫描接口的方式 //制定扫描的包 basePackage //any:扫描全部 //none:全部不扫描 //withClassAnnotation 扫描类上的注解 //withMethodAnnotation 扫描方法上的注解 //.apis(RequestHandlerSelectors.basePackage("com.wang.springboot07_swagger.controller")) .apis(RequestHandlerSelectors.withClassAnnotation(RequestMapping.class)) //配置要扫描路径的方法 .paths(PathSelectors.any()) .build(); } @Bean public Docket docketA(){ return new Docket(DocumentationType.SWAGGER_2).groupName("A"); } @Bean public Docket docketB(){ return new Docket(DocumentationType.SWAGGER_2).groupName("B"); }
Swagger相关注解
实体类
package com.wang.springboot07_swagger.pojo; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; //@Api(注释) @ApiModel("用户实体类") @Data @NoArgsConstructor @AllArgsConstructor public class User { private int id; @ApiModelProperty("用户姓名") private String name; @ApiModelProperty("用户年龄") private int age; }
controller
package com.wang.springboot07_swagger.controller; import com.wang.springboot07_swagger.pojo.User; import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiParam; import org.springframework.web.bind.annotation.*; @RestController @RequestMapping("") public class HelloController { @RequestMapping(value = "/hello",method = RequestMethod.GET) public String Hello(){ return "Hello"; } //只要我们的接口返回值中存在实体类,它就会被扫描到Swagger @PostMapping("/user") public User getUser(){ return new User(); } //Operation不是放在类上的,是放在方法上的 @ApiOperation("Hellouser方法") @PostMapping("/hellouser") public String helloUser(@ApiParam("用户名")String username){ return "hello"+username; } }
总结
- 我们可以Swagger给一些比较难理解的属性或者接口,增加注释信息
- 接口文档实时更新
- 可以在线测试
Swagger是一个优秀的工具,几乎所有大公司都有使用它
【注意点】在正式发布的时候,关闭Swagger!!!出于安全考虑。
标签:Swagger,SpringBoot,com,documentation,整合,import,springfox,Docket From: https://www.cnblogs.com/WangGuangYuan/p/16870481.html