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 are the new features of Swagger3.0

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly explains "what are the new features of Swagger3.0". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what are the new features of Swagger3.0"?

Support for OpenAPI

What is OpenAPI?

OpenAPI specification is actually the previous Swagger specification, it is a REST API description format, through the established specification to describe the document interface, it is the real API document standard in the industry, which can be described by YAML or JSON. It includes the following:

Interface (/ users) and operation of each interface (GET / users,POST / users)

Input parameters and response content

Authentication method

Some necessary contact information, license, etc.

For more information about OpenAPI, interested partners can see it on GitHub: https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md

Dependence

In the past, when using version 2.9.2, generally speaking, we might need to add the following two dependencies:

Io.springfox springfox-swagger2 2.9.2 io.springfox springfox-swagger-ui 2.9.2

These two, one is used to generate interface documents (JSON data), and the other is used to show the visualization of JSON.

In version 3.0, we don't need to go to so much trouble, we can do it with a starter:

Io.springfox springfox-boot-starter 3.0.0

Like other starter in Spring Boot, springfox-boot-starter dependency can achieve zero configuration and automatic configuration support. In other words, if you have no other special requirements, just add this dependency, and the interface document will be generated automatically.

Interface address

The interface address in 3.0 is also different from that before. In the past, we mainly accessed two addresses in 2.9.2:

Document interface address: http://localhost:8080/v2/api-docs

Document page address: http://localhost:8080/swagger-ui.html

Now in 3.0, these two addresses have also changed:

Document interface address: http://localhost:8080/v3/api-docs

Document page address: http://localhost:8080/swagger-ui/index.html

In particular, the document page address, if you use 3.0, and visit the previous page, will report 404.

The old annotations can continue to be used, but some other annotations are provided in 3.0.

For example, we can use @ EnableOpenApi instead of @ EnableSwagger2 in the previous version.

That's true, but Brother Song feels that the function of @ EnableOpenApi annotation is not obvious in the actual experience, so it doesn't matter if you add it. After looking through the source code, the @ EnableOpenApi annotation is mainly used to import OpenApiDocumentationConfiguration configuration classes, as follows:

@ Retention (value = java.lang.annotation.RetentionPolicy.RUNTIME) @ Target (value = {java.lang.annotation.ElementType.TYPE}) @ Documented @ Import (OpenApiDocumentationConfiguration.class) public @ interface EnableOpenApi {}

Then I took a look at the automatic configuration class OpenApiAutoConfiguration, as follows:

Configuration @ EnableConfigurationProperties (SpringfoxConfigurationProperties.class) @ ConditionalOnProperty (value = "springfox.documentation.enabled", havingValue = "true", matchIfMissing = true) @ Import ({OpenApiDocumentationConfiguration.class, SpringDataRestConfiguration.class, BeanValidatorPluginsConfiguration.class, Swagger2DocumentationConfiguration.class, SwaggerUiWebFluxConfiguration.class, SwaggerUiWebMvcConfiguration.class}) @ AutoConfigureAfter ({WebMvcAutoConfiguration.class, JacksonAutoConfiguration.class, HttpMessageConvertersAutoConfiguration.class, RepositoryRestMvcAutoConfiguration.class}) public class OpenApiAutoConfiguration {}

As you can see, OpenApiDocumentationConfiguration is also imported into the automation configuration class.

So under normal circumstances, you don't really need to add the @ EnableOpenApi annotation.

According to the definition in the @ ConditionalOnProperty condition annotation on OpenApiAutoConfiguration, we find that if you set springfox.documentation.enabled=false in application.properties, that is, the swagger function is turned off, the automatic configuration class is not executed, and the OpenApiDocumentationConfiguration configuration class can be imported through the @ EnableOpenApi annotation. Technically, this is the logic, but no such requirement has been found in the application (that is, close swagger in application.properties and open it with the @ EnableOpenApi annotation).

Thank you for your reading, the above is the content of "what are the new features of Swagger3.0". After the study of this article, I believe you have a deeper understanding of the new features of Swagger3.0, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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