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

How to start swagger quickly in springboot

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article introduces you how to quickly start swagger in springboot, the content is very detailed, interested friends can refer to, hope to be helpful to you.

The swagger Quick start code for springboot is roughly as follows:

When integrating with spring boot, / * * Swagger2 configuration class * is placed in a directory at the same level as Application.java. * use the @ Configuration annotation to let Spring load the configuration of this class. * enable Swagger2 through the @ EnableSwagger2 annotation. * / @ Configuration@EnableSwagger2public class SwaggerConfig {/ * create API application * apiInfo () add API related information * return an ApiSelectorBuilder instance through the select () function to control which interfaces are exposed to Swagger. * this example uses the specified scanning packet path to define the specified directory to create the API. * * @ return * / @ Bean public Docket createRestApi () {return new Docket (DocumentationType.SWAGGER_2) .apiInfo (apiInfo ()) .select () .apis (RequestHandlerSelectors.basePackage ("com.swaggerTest.controller")) .build (PathSelectors.any ()) } / * basic information about creating the API (which will be displayed on the document page) * access address: actual address of the http:// project / swagger-ui.html * @ return * / private ApiInfo apiInfo () {return new ApiInfoBuilder () .title ("use Swagger2 in Spring Boot to build RESTful APIs") .description ("Please pay more attention to http://www.baidu.com") .termsOfServiceUrl (" http://www.baidu.com") .contact ("sunf") .version ("1.0") .build () }} Modular-Starter

Reason

Partners who have developed micro-services should have experienced it. When there are many microservice modules, each module needs to configure such a class to load swagger. Resulting in each module is roughly the same SwaggerConfig, in extreme cases, some friends copy the SwaggerConfig of other modules for transformation, found that still can not load the swagger situation, resulting in clearly copied, why not load out, troubleshooting this bug and its time-consuming.

On top of this, you can build a swagger-starter module, just reference a jar, load some special configuration, and you can quickly use some of the functions of swagger.

Design

Create the module swagger-spring-boot-starter. The functions are roughly as follows:

Load SwaggerConfig.

Configure swagger through configuration.

Enable loads annotations.

1. Create SwaggerConfig

SwaggerConfig is the same as before, except that the configuration needs to be externalized.

@ Configuration@PropertySource (value = "classpath:swagger.properties", ignoreResourceNotFound = true, encoding = "UTF-8") @ EnableConfigurationProperties (SwaggerProperties.class) public class SwaggerConfig {@ Resource private SwaggerProperties swaggerProperties; @ Bean public Docket buildDocket () {return new Docket (DocumentationType.SWAGGER_2) .apiInfo (buildApiInf ()) .select () .apis (RequestHandlerSelectors.basePackage (")) .steps (PathSelectors.any ()) .b uild () } private ApiInfo buildApiInf () {return new ApiInfoBuilder () .title (swaggerProperties.getTitle ()) .description (swaggerProperties.getDescription ()) .termsOfServiceUrl (swaggerProperties.getTermsOfServiceUrl ()) .contact (new Contact ("skyworth", title (), ")) .version (swaggerProperties.getVersion ()) .build ();}} 2. Related to creating SwaggerProperties configuration

Configure to load the swagger.properties in the resources directory through the @ PropertySource annotation.

Create a SwaggerProperties configuration class that contains some commonly used properties for general swagger initialization, such as scan package path, title, and so on.

@ Data@ToString@ConfigurationProperties (SwaggerProperties.PREFIX) public class SwaggerProperties {public static final String PREFIX = "swagger"; / * * document scan package path * / private String basePackage = ""; / * title: user module system interface details * / private String title = "Shen Lanyun platform system interface details"; / * * Service file introduction * / private String description = "online documentation" / * terms of Service URL * / private String termsOfServiceUrl = "https://www.deepblueai.com/"; / * version * / private String version =" V1.0 ";}

In order to better use the configuration, like the official starter package in idea, we also need to configure an additional-spring-configuration-metadata.json, so that our own configuration also has the function of prompts. For a specific introduction, please take the production examination: configuration tips.

3. Load properties such as SwaggerConfig

Because it is a starter module, the project directory of someone else may not be consistent with the directory of the starter module, so the SwaggerConfig class cannot be loaded. We need to use spring.factories to load the SwaggerConfig class into the spring container.

Resources/META-INF

Org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ io.purge.swagger.SwaggerConfig

Of course, this time it is based on Enable to load SwaggerConfig.

Create the @ EnableSwaggerPlugins annotation class and import the SwaggerConfig into Dagong using @ Import (SwaggerConfig.class).

@ Retention (RetentionPolicy.RUNTIME) @ Target (ElementType.TYPE) @ Import (SwaggerConfig.class) @ EnableSwagger2public @ interface EnableSwaggerPlugins {} use

Add dependency

Package your own written swagger through maven and refer to your own project.

Com.purgeteam swagger-spring-boot-starter 0.1.0.RELEASE

Configure the swagger.properties file

Create a swagger.properties configuration in the resources directory of your project module

The swagger.properties is roughly configured as follows

Swagger.basePackage= "swagger scan Project package path" swagger.title= "swagger Page display title" swagger.description= "swagger Page display introduction"

Add @ EnableSwaggerPlugins annotation to the startup class.

@ EnableSwaggerPlugins@SpringBootApplicationpublic class FrontDemoApplication {public static void main (String [] args) {SpringApplication.run (FrontDemoApplication.class, args);}}

Access the http://ip: port / swagger-ui.html to check that the swagger-ui is working.

About how to quickly start swagger in springboot to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

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

Internet Technology

Wechat

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

12
Report