当前位置:网站首页>Swagger study notes
Swagger study notes
2022-06-11 08:19:00 【Scarecrow 0.0】
Swagger
After the front and rear ends are separated , The front and rear end integrated joint commissioning will result in the failure of timely negotiation , Solve interface interaction problems in time .
The front end often complains that the interface document given by the back end is inconsistent with the actual situation . The back end also thinks that writing and maintaining interface documents will cost a lot of energy , Often too late to update .
Over time , Version of the iteration , Interface documents can easily fail to keep up with the code .
As a programmer , Two things I hate most :1、 Others don't write notes .2、 Write your own notes .
1、 What is? Swagger?
Swagger Is a normative and complete framework , Used to generate 、 describe 、 Invocation and visualization RESTful Style Web service .
Swagger The goal is to RESTful API Define a standard, language independent interface , People and computers can access the source code without 、 Document or discover and understand the functions of the service through network traffic monitoring .
Swagger After correct definition , The user can understand the remote service and interact with the remote service with minimal implementation logic .
Swagger Support API Automatic generation of synchronized online documents also supports online testing .
Similar to that : Lkadoc Interface documentation tools
2、SpringBoot Integrate Swagger
Create a new one SpringBootWeb project .
Import correlation dependency
3.0 Dependencies imported from previous versions
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>3.0.0</version> </dependency> <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>3.0.0</version> </dependency>3.0 After version Swagger Use the starter
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-boot-starter --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-boot-starter</artifactId> <version>3.0.0</version> </dependency>To write Helloworld
To configure Swagger (Config)
@Configuration // @EnableSwagger2 // Turn on Swagger2 3.0 Previous version @EnableOpenApi // 3.0 edition public class SwaggerConfig { }test run
Access address :http://localhost:8080/swagger-ui/index.html You can see it swagger Backend interface .
3、 To configure Swagger Information
4、 Configure scanning interface and document
@Configuration
// @EnableSwagger2 // Turn on Swagger2 3.0 Previous version
@EnableOpenApi // 3.0 edition
public class SwaggerConfig {
// Configured with swagger Of Docket Of bean example
@Bean
public Docket docket(){
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
// Configure whether to enable swagger
.enable(true)
.select()
// RequestHandlerSelectors: Configure the interface mode to be scanned
// basePackage() Specify packages to scan
// any() Scan all
// none() No scanning
// withClassAnnotation() Scan annotations on classes , The parameter is the return object of an annotation
// withMethodAnnotation() Notes on scanning methods
.apis(RequestHandlerSelectors.basePackage("com.example.swagger.controller"))
// paths(): Filter path
.paths(PathSelectors.ant("/example/**"))
.build();
}
// To configure Swagger Information apiInfo
private ApiInfo apiInfo(){
// The author information
Contact contact = new Contact(" bread ", "http://localhost", "[email protected]");
return new ApiInfo(
" bread API file ",
" This is a description of ",
"1.0",
"http://localhost",
contact,
"Apache 2.0",
"http://www.apache.org/licenses/LICENSE-2.0",
new ArrayList<>()
);
}
}
Swagger Use in a specific environment :
Judge the environment used (application-dev.properties、application-pro.properties、application-test.properties)
Inject enable( false )
# application.properties spring.profiles.active=dev// You can make the annotation fetch environment @Value("${spring.profiles.active}") private String env; ----------------------------------------------------------------- // Set what to display Swagger Environmental Science Profiles profiles = Profiles.of("dev", "test"); // Get the environment of the project boolean flag = environment.acceptsProfiles(profiles);
5、 To configure API Grouping documents
.groupName(" bread ")
One Docket Corresponding to a group :
@Bean
public Docket docket1(){
return new Docket(DocumentationType.SWAGGER_2).groupName(" grouping 1");
}
@Bean
public Docket docket2(){
return new Docket(DocumentationType.SWAGGER_2).groupName(" grouping 2");
}
@Bean
public Docket docket3(){
return new Docket(DocumentationType.SWAGGER_2).groupName(" grouping 3");
}
Entity class configuration :
1、 As long as the return value of our interface , There are returns of entity classes , He'll be scanned to swagger in
2、 Used on user entity classes @ApiModel("xx Entity class ") Use annotations on attributes @ApiModelProperty("xxx")
【 Be careful 】: It should be closed when it is officially released swagger !! Safe on , It also saves memory consumption !!
边栏推荐
- C. Managing history (greedy / hashing / thinking / good questions)
- qiao-lerna:lerna辅助工具
- Use guidelines in constraintlayout to limit the maximum width of controls
- JS basic learning script
- How to find the complementary sequence of digraph
- Login and parameterization of interface test
- Receive ontrimmemory and other callbacks through componentcallbacks2 and mock the corresponding scenario
- uniapp 插件开发
- Typescript interface and type alias similarities and differences
- These gadgets are also very easy to use
猜你喜欢

Difference between threadpooltaskexecutor and ThreadPoolExecutor

torch. Var (), sample variance, parent variance

Printing diamond of beginner C

Figure seamless database integration tushare interface

Empty difference between postgrepsql and Oracle

Node error report sorting

Solve notimplementederror: layer XX has arguments in`__ init__` and therefore must override `get_ config`

(resolved) the tqdm progress bar in the Jupiter notebook does not update and display in one line, but scrolls down to output

Solve valueerror: no model found in config file

What does it mean to buy a single-mode, dual-mode and Rechargeable Wireless Mouse
随机推荐
Printing diamond of beginner C
SylixOS SD设备驱动开发
2022.6.7 special student simulation
Logical implication of functional dependence
Deep understanding of add in argparse module_ Argument parameters (such as action)
Uniapp turn off / on / adjust system sound
Jupyter notebook code completion plug-in + Solution
torch. nn. functional. pad
TypeScript-枚举
Label the mask image not obtained through labelme
Training yolov4 CSP model using coco dataset
Record a murder case caused by ignoring the @suppresslint ("newapi") prompt
Js学习基础document.write在页面中写一行文字
How CSDN reports plagiarized articles
Timestamp of PostgreSQL and Oracle
进程控制:进程等待(回收子进程)
Reference implementation scheme of database database and table division strategy
TypeScript-头文件使用细节
centos随笔03:centos8.2安装mysql
Bubble sorting with C language