Introduction to swagger:
swagger is an open source API interface documentation generation tool.
Swagger's project home page: https://swagger.io/ A popular practice is to add swagger-related comments to the code, and then use a small tool to generate a swagger.json or swagger.yaml file.
springboot makes swagger even simpler:
springboot has its own auto-configuration feature, and swagger also publishes an auto-dependency configuration module applied to springboot.
That is to say, we only need to introduce the swagger module configuration information in the pom file, and then perform a simple configuration of the swagger framework in the application, and then we can easily access the web interface description document generated by swagger for us through the browser.
Specific steps:
1. First, we need to add the swagger module configuration information to pom.xml, and introduce the swagger module into the project:
<!-- https://mvnrepository.com/artifact/com.spring4all/spring-boot-starter-swagger --> <dependency> <groupId>com.spring4all</groupId> <artifactId>spring-boot-starter-swagger</artifactId> <version>1.5.1.RELEASE</version> </dependency>
2. Add annotations to the springboot startup class:
@EnableSwagger2Doc @SpringBootApplication public class Bootstrap { public static void main(String[] args) { SpringApplication.run(Bootstrap.class, args); } }
3. Add swagger configuration information:
I saw on the Internet that there are two configuration methods, one is to start another application.yaml, and then configure it through the yaml language, and the other is to add the configuration to the existing application.properties (the second method is described here) :
#swagger configuration information swagger.title=yyh project online API specification swagger.description=the web page which you opened is generated by swagger automatically swagger.version=1.5.0.RELEASE swagger.license=Apache License, Version 2.0 swagger.licenseUrl=https://www.apache.org/licenses/LICENSE-2.0.html swagger.termsOfServiceUrl=https://github.com/dyc87112/spring-boot-starter-swagger swagger.contact.name=mht swagger.contact.url=http://localhost:8084/swagger-ui.html [email protected] swagger.base-package=com.seco swagger.base-path=/** swagger.exclude-path=/error, /ops/**
#Configuration instructions: swagger.title=Title swagger.description=Description swagger.version=version swagger.license=License swagger.licenseUrl=License URL swagger.termsOfServiceUrl=Terms of Service URL swagger.contact.name=Maintainer swagger.contact.url=Maintainer URL swagger.contact.email=Maintainer email swagger.base-package=the base package of swagger scan, default: full scan swagger.base-path=base URL rule to be processed, default: /** swagger.exclude-path=URL rules to be excluded, default: empty
4. API document effect view:
Start the project, open the browser and enter the following address in the address bar to view the generated API documentation:
http://localhost:8080/swagger-ui.html
Reference article:
" Using spring-boot-starter-swagger to implement API documentation "
" 5 minutes to understand swagger "