Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What's the use of Swagger?

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces the relevant knowledge of "what is the use of Swagger". In the operation of actual cases, many people will encounter such a dilemma. Then let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

What is Swagger?

Swagger is a Web service for generating, describing, and invoking RESTful interfaces. Generally speaking, Swagger is a service that presents all the interfaces in the project on the page and can be called and tested.

PS:Swagger follows the OpenAPI specification, and OpenAPI is a project of the Linux Foundation that attempts to standardize the RESTful service development process by defining a language that describes the API format or API definition.

Swagger official website address: https://swagger.io/

What's the use of Swagger?

From the above definition of Swagger, we can easily see that Swagger has the following three important functions:

Show all the interfaces in the project on the page so that back-end programmers do not need to write special interface documents for front-end users

When the interface is updated, you only need to modify the Swagger description in the code to generate a new interface document in real time, thus avoiding the problem that the interface document is old and cannot be used.

Through the Swagger page, we can directly call the interface, reducing the debugging cost in the project development phase.

Use of older versions of Swagger

The old version of Swagger, which is currently the mainstream version of V2 on the market, is Swagger 2.9.2. before we talk about the new version, let's review how Swagger 2.9.2 is used.

The use of Swagger 2.9.2 is divided into the following four steps:

Hongmeng official Strategic Cooperation to build HarmonyOS Technology Community

Add dependency

Enable the Swagger function

Configure Swagger document summary information

Call API access

Let's look at it separately.

1. Add dependency

First, we will go to mvnrepository to query Swagger dependencies, search for the "springfox" keyword, and get the first two dependencies of the results, which are the results we want, as shown in the following figure:

Add these two dependencies to the belt project:

Why is io.springfox springfox-swagger2 2.9.2 io.springfox springfox-swagger-ui 2.9.2 "springfox"?

Q: we are using Swagger, so why search for "springfox"?

A: Swagger can be seen as a technology that follows the OpenAPI specification, and springfox is a concrete implementation of this technology. Just like AOP and DI in Spring, the former is an idea and the latter is an implementation.

two。 Turn on Swagger

Add @ EnableSwagger2 annotation to the startup class or configuration class of Spring Boot, and enable Swagger. Some of the core codes are as follows:

@ EnableSwagger2 @ SpringBootApplication public class Application {. 3. Configuration Summary Information import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import springfox.documentation.builders.RequestHandlerSelectors; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.swagger2.annotations.EnableSwagger2 @ Configuration public class SwaggerConfig {@ Bean public Docket createRestApi () {return new Docket (DocumentationType.SWAGGER_2) / / 1.SWAGGER_2 .select () .apis (RequestHandlerSelectors.basePackage ("com.example.swaggerv2.controller")) / / 2. Set the scan path. Build ();}} 4. Visit Swagger

After the project starts normally, use http://localhost:8080/swagger-ui.html" to access the Swagger page, as shown in the following figure:

The latest version of Swagger uses

The configuration steps for the latest version of Swagger are the same as those for the previous version, except that each specific configuration item is slightly different, as follows.

1. Add dependency io.springfox springfox-boot-starter 3.0.0

As you can see from the above configuration, the new version of Swagger has only one dependency, while the old version has two, which is much simpler.

two。 Turn on Swagger

Add @ EnableOpenApi annotation to the startup class or configuration class of Spring Boot, and enable Swagger. Some of the core codes are as follows:

@ EnableOpenApi @ SpringBootApplication public class Application {. 3. Configuration Summary Information import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import springfox.documentation.builders.RequestHandlerSelectors; import springfox.documentation.oas.annotations.EnableOpenApi; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket @ Configuration public class SwaggerConfig {@ Bean public Docket createRestApi () {return new Docket (DocumentationType.OAS_30) / / v2 is different. Select () .apis (RequestHandlerSelectors.basePackage ("com.example.swaggerv3.controller")) / / set the scan path .build ();}}

From the above code, you can see that in the configuration of Docket, only the type setting of the document is different between the new version and the old version, the new version is OAS_30 and the old version is SWAGGER_2.

PS:OAS is the abbreviation of OpenAPI Specification, and the translation into Chinese is OpenAPI instruction manual.

4. Visit Swagger

The Swagger access address of the new version is different from that of the old version, and the access address of the new version is "localhost:8080/swagger-ui/", as shown in the following figure:

New version VS old version

The difference between the new version and the old version is mainly reflected in the following four aspects:

Hongmeng official Strategic Cooperation to build HarmonyOS Technology Community

Dependencies are added differently: only one item needs to be added in the new version, while two items need to be added in the old version

The note to start Swagger is different: the new version uses @ EnableOpenApi, while the old version uses @ EnableSwagger2

The file type configuration of Docket (document Summary Information) is different: the new version is configured with OAS_3, while the old version is SWAGGER_2.

The Swagger UI access address is different: the new version is "http://localhost:8080/swagger-ui/", while the old version is" http://localhost:8080/swagger-ui.html". "

This is the end of the content of "what's the use of Swagger"? thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report