swagger interface documentation
1. Introduction to swagger
Front and rear separation
Vue + springboot a set of solutions
Back-end era: the front-end only manages static pages; html ===> back-end. Template engine jsp ejb backend is the main force
The era of separation of front and back ends:
- Back-end: back-end control layer, service layer, data access layer [back-end team]
- Front end: front-end control layer (two-way binding), view layer [front-end team]
- Forge back-end data, json. It already exists, no backend is needed, and the whole project can still run.
- How do the front and back ends interact? ===> API json
- Relatively independent front and rear ends, loosely coupled
- The front and back ends can be deployed on different servers
The separation of front and back ends, a problem
- Front-end and back-end integration and joint debugging, front-end and back-end personnel cannot do it, "negotiate in real time, solve as soon as possible", and eventually lead to concentrated outbreak
Solution:
-
Specify the schema [Outline of the plan]. Update the latest API in real time to reduce the risk of integration;
-
Early years: Develop word plan documents;
-
Front and rear separation:
- Front-end test back-end interface: postman
- The back-end provides an interface and needs to update the latest news and changes in real time!
swagger is born
- Known as the most popular api framework in the world
- Restful api document online automatic generation tool => api document and api definition are synchronized and updated
- Run directly, test the api interface online
- Support multiple languages java, php, py...
Official website: https://swagger.io
Use swagger springbox in the project
- swagger2
- ui
Second, springboot integrates swagger
rely
-
Import related dependencies
- springfox-swagger2
- springfox-swagger-ui
-
pom
<!-- 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>
Write the helloworld interface
package cn.bitqian.swagger.controller;
import org.springframework.web.bind.annotation.*;
/**
* hello world
* @author echo lovely
* @date 2020/10/28 19:23
*/
@RestController
public class HelloController {
@RequestMapping(value = "/hello")
public String hello() {
return "hello";
}
}
Configure swagger ==> config configuration class
Open swagger, Enablexxx
@Configuration
@EnableSwagger2 // 开启swagger2
public class SwaggerConfig {
}
Test run
http://localhost:8080/swagger-ui.html
Three, configure swagger
Tell the transfer personnel what the interface does and describe the interface of the controller. You can also set interface access permissions.
package cn.bitqian.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.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import java.util.ArrayList;
/**
* swagger配置类
* @author echo lovely
* @date 2020/10/28 19:35
*/
@Configuration
@EnableSwagger2 // 开启swagger2
public class SwaggerConfig {
// 文档的标题,和描述!作者的信息deng..
@Bean
public Docket docket() {
return new Docket(DocumentationType.SWAGGER_2).apiInfo(appInfo());
}
// api 信息 接口文档的头部信息描述
public ApiInfo appInfo() {
Contact contact = new Contact("bitqian", "http://example.com", "[email protected]");
return new ApiInfo("bitqian的swagger文档",
"用来描述此接口的功能,内容。",
"v1.0",
"http://example.cn", contact,
"Apache 2.0",
"http://www.apache.org/licenses/LICENSE-2.0",
new ArrayList());
}
}
swagger configuration scan interface
@Bean
public Docket docket() {
return new Docket(DocumentationType.SWAGGER_2).apiInfo(appInfo()).
select().
// RequestHandlerSelectors 配置要扫描的接口的方式
// basePackage 指定要扫描的包
// any() 扫描全部
// none() 不扫描
// withClassAnnotation(Controller.class) 扫描类上的注解, 生效
// withMethodAnnotation(RequestMapping.class) 扫描方法上的注解, 生效
apis(RequestHandlerSelectors.basePackage("cn.bitqian.swagger.controller")).
// paths(PathSelectors.ant("/bitqian/**")) 扫描指定的接口
// PathSelectors.regex("")
paths(PathSelectors.ant("/hello/**"))
.build();
}
How to start swagger only in the production environment?
- Determine whether it is a production environment
- Inject enable
@Bean
public Docket docket(Environment environment) {
// 获取当前环境 是生产环境 启动swagger
boolean isProFlag = environment.acceptsProfiles(Profiles.of("pro"));
System.out.println("is dev environment....===========>" + isProFlag);
return new Docket(DocumentationType.SWAGGER_2).apiInfo(appInfo()).groupName("bitqian").
enable(isProFlag). // 是否启动swagger 如果为false,则不能在浏览器中使用swagger
select().
apis(RequestHandlerSelectors.basePackage("cn.bitqian.swagger.controller")).
// paths(PathSelectors.ant("/hello/**")).
build();
}
Configure grouping of api documents
- Make multiple dockets, and multiple people write different interfaces
@Bean
public Docket docket1() {
return new Docket(DocumentationType.SWAGGER_2).groupName("bitqian666 group1");
}
@Bean
public Docket docket12() {
return new Docket(DocumentationType.SWAGGER_2).groupName("bitqian666 group2");
}
Fourth, swagger annotations are used to describe entity classes and interfaces
sing
package cn.bitqian.swagger.entity;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
/**
* 给实体类生成文档
* @author echo lovely
* @date 2020/10/29 21:09
*/
@ApiModel("user实体类")
public class User {
@ApiModelProperty("用户名")
public String username;
@ApiModelProperty("密码")
public String password;
public User() {
}
public User(String username, String password) {
this.username = username;
this.password = password;
}
}
controller
package cn.bitqian.swagger.controller;
import cn.bitqian.swagger.entity.User;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;
/**
* hello world
* @author echo lovely
* @date 2020/10/28 19:23
*/
@RestController
public class HelloController {
@ApiOperation("get/post都可的hello接口")
@RequestMapping(value = "/hello")
public String hello() {
return "hello";
}
@ApiOperation("get的hello接口, 返回一个空 user")
@GetMapping(value = "/hello1")
public User hello1() {
return new User();
}
@ApiOperation("hello1 接口post way~")
@PostMapping(value = "/hello1")
public User hello1(@ApiParam("传递用户") User user) {
return user;
}
@ApiOperation("登录接口 post way~")
@PostMapping(value = "/login")
public String login(@ApiParam("登录用户名") @RequestParam("username") String username,
@ApiParam("登录密码") @RequestParam("password") String password) {
return "ok" + "{" + username + ", " + password + "}";
}
}
Five, what do you think is swagger can only provide interface description information? Huh~
swagger can test it
try it out
Six, finally
- We can add annotation information to some more difficult to understand attributes or interfaces through swagger
- Real-time update of interface documentation
- Can be tested online
Swagger is an excellent tool, which is used by almost all large companies, which is more in line with the needs of iterative development.
[Note] When it is officially released, close swagger! ! ! For safety reasons. And save running memory.