Swagger study notes
Look, the crazy God said video
Introduction to swagger
Front and rear end separation
vue+springboot
Back end: back end control layer, service layer and data access layer
Front end: front end control layer, view layer
- Fake backend data json already exists. Without a backend, front-end engineering can still run
- How the front and back end interact = = > API
- The front and rear ends are relatively independent and loosely coupled
- The front and back end can even be deployed on different servers;
The problems are:
- The front-end and back-end integration and joint commissioning, the front-end personnel and back-end personnel can not achieve "even if negotiated, solve it as soon as possible", which eventually leads to the centralized outbreak of problems
Solution:
-
First, specify the schema. Update the latest API in real time to reduce the risk of integration;
-
Previous years: specify the word plan document
-
Front and rear end separation:
Front end test back end interface: postman
The back-end provides an interface, which needs to update the latest messages and changes in real time
**Swagger * * features
-
Known as the world's most popular API framework;
-
RestFul Api document online automatic generation tool = > API document and API definition are updated synchronously
-
Run directly, you can test the API interface online
-
Support multiple languages
Official website: https://swagger.io/
Swagger is used in projects
springbox required
-
swagger2
-
ui
springboot integration swagger
1. Create a new springboot -web project
2. Import dependency
<dependencies> <!-- 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> <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.9.2</version> </dependency> </dependencies>
3. Write a hello project
package com.example.swaggerdemo.controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class SwaggerController { @RequestMapping(value = "/hello") public String hello(){ return "Hello Swagger"; } }
4. Configure swagger = = > config
package com.example.swaggerdemo.config; import org.springframework.context.annotation.Configuration; import springfox.documentation.swagger2.annotations.EnableSwagger2; @Configuration//Configure configuration to springboot @EnableSwagger2//Open swagger2 public class SwaggerConfig { }
5. Test run
http://localhost:8080/swagger-ui.html
[the external chain image transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-0an6grrk-163289185619) (C: \ users \ Yingju \ appdata \ roaming \ typora \ typora user images \ image-20210929104302629. PNG)]
6. Configure swagger
bean instance of swagger
package com.example.swaggerdemo.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; @Configuration//Configure configuration to springboot @EnableSwagger2//Open swagger2 public class SwaggerConfig { // bean instance of dock configured with Swagger @Bean public Docket docket(){ return new Docket ( DocumentationType.SWAGGER_2 ) .apiInfo ( apiInfo () ); } //Configure apiInfo information for swagger information private ApiInfo apiInfo(){ //Author information Contact contact=new Contact ( "Ying Jing","https://blog.csdn.net/qq_44647596?spm=1001.2101.3001.5343","1324232975@qq.com"); return new ApiInfo( "Should be alone swaggerAPI file", //document title "Api Documentation", //Document description "1.0", //Version number "urn:tos", contact, "Apache 2.0", "http://www.apache.org/licenses/LICENSE-2.0", new ArrayList ()); } }
7.swagger configuration scanning interface
.select () //RequestHandlerSelectors to configure the method of interface to be scanned //basePackage: Specifies the package to scan //any(): scan all //none(): do not scan //withClassAnnotation: scan annotations on classes //withMethodAnnotation: scan annotations on Methods //General basePackage .apis ( RequestHandlerSelectors.basePackage ( "com.example.swaggerdemo.controller" ) ) //What paths are filtered? The paths under swaggerdemo are filtered out, and they are not displayed on swagger .paths ( PathSelectors.ant ( "/swaggerdemo/**") ) .build ();//
close
//enable whether swagger is enabled. if false, swagger cannot be accessed in the browser .enable ( false )
[the external chain image transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-Xey0Uspi-1632899185625)(C:\Users \ Ying Ju \ appdata \ roaming \ typora \ typora user images \ image-20210929112007897. PNG)]
Example questions:
I hope my swagger is only used in the production environment, but not in the release. How to solve it
Idea:
- Get the environment,
- Determine whether it is a production environment
- Injection enable
Setting the configuration environment
[the external chain image transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-YbujjVuP-1632899185630)(C:\Users \ Ying Ju \ appdata \ roaming \ typora \ typora user images \ image-20210929140559489. PNG)]
[the external chain image transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-WWU6KQei-1632899185640)(C:\Users \ Ying Ju \ appdata \ roaming \ typora \ user images \ image-20210929140621501. PNG)]
[the external chain image transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-dMnElIrD-1632899185644)(C:\Users \ Ying Ju \ appdata \ roaming \ typora \ typora user images \ image-20210929140644735. PNG)]
[the external chain image transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-csfLtpuz-1632899185651)(C:\Users \ Ying Ju \ appdata \ roaming \ typora \ typora user images \ image-20210929140659668. PNG)]
//Set the swagger environment to display Profiles profiles= Profiles.of ("dev");//Multiple can be configured, only dev is displayed, and other environments are filtered out //Use environment.acceptsProfiles to determine whether you are in the environment you set to obtain the environment of the project boolean flag=environment.acceptsProfiles ( profiles); return new Docket ( DocumentationType.SWAGGER_2 ) .apiInfo ( apiInfo () ) //enable whether swagger is enabled. if false, swagger cannot be accessed in the browser .enable ( flag )
8. Configure the grouping of API documents
return new Docket ( DocumentationType.SWAGGER_2 ) .apiInfo ( apiInfo () ) //Configure grouping .groupName ( "zhongyan" )
Example: how to configure multiple groups to realize multi person cooperation and manage different interfaces
@Bean public Docket docket1(){ return new Docket ( DocumentationType.SWAGGER_2 ).groupName ( "A" ); } @Bean public Docket docket2(){ return new Docket ( DocumentationType.SWAGGER_2 ).groupName ( "B" ); } @Bean public Docket docket3(){ return new Docket ( DocumentationType.SWAGGER_2 ).groupName ( "C" ); }
[the external chain image transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-ko5X73dK-1632899185656)(C:\Users \ Ying Ju \ appdata \ roaming \ typora \ typora user images \ image-20210929142306171. PNG)]
//Controller.java
//As long as there is an entity class in the return value in our interface, it will be scanned into swagger @ApiOperation ( "Method notes" ) @PostMapping(value = "/user") public User user(){ return new User (); } @ApiOperation ( "Method notes" ) @PostMapping(value = "/user") public String hello2(@ApiParam("Parameter notes")String username){ return username; }
Entity class User.java
package com.example.swaggerdemo.pojo; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; //ApiModel is used to annotate entity classes in swagger @ApiModel("User entity class") public class User { //ApiModelProperty is used to annotate fields @ApiModelProperty("user name") public String userName; @ApiModelProperty("password") public String password; }
[the external chain image transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-EFCRrqG5-1632899185658)(C:\Users \ Ying Ju \ appdata \ roaming \ typora \ typora user images \ image-20210929143407000. PNG)]
Summary:
- You can add comments to some properties or interfaces that are difficult to understand through swagger
- Real time update of interface documents
- It can be tested online
- During the official release, close Swagger to prevent users from getting interface information and save running memory for security reasons