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

Configuration and usage of Swagger API description document

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article mainly explains the "Swagger interface description document configuration and use", the article explains the content is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "Swagger interface description document configuration and use" bar!

Swagger is an open source interface configuration document, which is generally used for front-end separation, instead of the back-end staff to write tedious interface documents for the front-end personnel, so that the back-end personnel are freed from the tedious interface documents. How do you use Swagger? First, we need to introduce dependent packages into the pom file in springboot

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

Second, add a configuration file for swagger, which defines the path for web pages to access swagger2, as well as title, description and other information.

Package com.xash.quartzDemo.config;import org.springframework.beans.factory.annotation.Configurable;import org.springframework.context.annotation.Bean;import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;import springfox.documentation.builders.ApiInfoBuilder;import springfox.documentation.builders.PathSelectors;import springfox.documentation.builders.RequestHandlerSelectors;import springfox.documentation.service.ApiInfo;import springfox.documentation.spi.DocumentationType Import springfox.documentation.spring.web.plugins.Docket;import springfox.documentation.swagger2.annotations.EnableSwagger2 @ Configurablepublic class SwaggerConfig {@ Bean public Docket api () {return new Docket (DocumentationType.SWAGGER_2) .groupName ("test module") .apiInfo (getApiInfo ()) .select () .apis (RequestHandlerSelectors. BasePackage ("com.xash.quartzDemo.controller")) .build (PathSelectors.regex (". * /. *")) .build () } private ApiInfo getApiInfo () {return new ApiInfoBuilder () .title ("test module") .description ("subtitle") .version ("1.0") .build () } @ Bean public Docket api1 () {return new Docket (DocumentationType.SWAGGER_2) .groupName ("Test Module 1") .apiInfo (getApiInfo1 ()) .select () .apis (RequestHandlerSelectors.basePackage ("com.xash.quartzDemo.controller")) .build (PathSelectors.regex (". * /. *")) .build () } @ Bean private ApiInfo getApiInfo1 () {return new ApiInfoBuilder () .title ("Test Module 1") .description ("subtitle") .version ("1.0") .build ();}}

In this way, we can use annotations to add the corresponding interface document information where we want to add the interface document.

For example:

@ Api (tags= "this is a test Controller")

Add comments to the public class PermissionController {} class to illustrate the functions of this class

@ ApiOperation (value = "query permissions based on user id")

Public String selectPermissionById (ModelMap map,@ApiParam ("id") @ RequestParam ("id") int id) {

System.out.println ("start query")

Map.put ("name", "drink using thymeleaf template engine")

Map.put ("sysPermission", permissionService.selectPermissionById (id))

Return "index"

} add notes on the method, express the function of the method, and add notes on the formal parameters, specify the formal parameters, and describe the formal parameters.

The access address defaults to 192.168.2.199VOV 8080 / Project Application / swagger2-ui.html

Thank you for your reading, the above is the content of "the configuration and use of the Swagger interface description document". After the study of this article, I believe you have a deeper understanding of the configuration and use of the Swagger interface description document, 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

Internet Technology

Wechat

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

12
Report