1. What is Swagger?
? Swagger is a very useful framework for writing API documents. Other self-built Baidu
Ii. SSM integration with Swagger?
1: Introduce dependency in the maven pom file: (Note the version. Otherwise, Tomcat cannot be started normally.)
<! -- Introduce swagger --> <! -- Springfox's core jar package --> <dependency> <groupid> Io. springfox </groupid> <artifactid> springfox-swagger2 </artifactid> <version> 2.7.0 </version> </dependency> <! -- Springfox-UI jar package (which contains static swagger interface files) --> <dependency> <groupid> Io. springfox </groupid> <artifactid> springfox-swagger-UI </artifactid> <version> 2.7.0 </version> </dependency> <! -- Springfox dependent jar package; if your project has already been integrated, you do not need to repeat it --> <dependency> <groupid> COM. fasterxml. jackson. core </groupid> <artifactid> Jackson-databind </artifactid> <version> 2.9.0 </version> </dependency>
2: Create the swaggerconfig. Java file, that is, the swagger configuration file. It is best to put it in a separate folder.
@ Webappconfiguration @ enableswagger2 @ enablewebmvc @ componentscan (basepackages = "com.zgz.cn. controller ") public class swaggerconfig {@ bean public docket API () {return New docket (documentationtype. swagger_2 ). select (). APIS (requesthandlerselectors. any ()). build (). apiinfo ();} private apiinfo () {return New apiinfobuilder (). title ("XXX project interface document "). description ("XXX project interface test "). version ("1.0.0 "). termsofserviceurl (""). license (""). licenseurl (""). build ();}}
3: Configure in the springmvc configuration file: (configure the packages of All controllers)
<! -- Configure the swagger Bean --> <! -- Handle static resources by default servlet --> <MVC: default-servlet-handler/> <! -- Automatically inject configuration to the container --> <context: annotation-config/> <! -- Inject the swaggerconfig configuration class --> <Bean class = "com.zgz.cn. Swagger. swaggerconfig"/> <! -- Configure swagger resources not to be intercepted --> <! -- <Bean id = "swagger2config" class = "springfox.doc umentation. swagger2.configuration. swagger2documentationconfiguration "/> --> <MVC: Resources location =" classpath:/META-INF/resources/"Mapping =" swagger-ui.html "/> <MVC: Resources location =" classpath: /META-INF/resources/webjars/"Mapping ="/webjars/** "/>
4: All requests configured in Web. XML are processed by dispatcherservlet.
<servlet-mapping> <servlet-name>springMVC</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>
5: Use annotations in controllers: (Common swagger annotations)
6: Test (access address: Project name/swagger-ui.html)
Step by step teach you how to integrate SSM with swagger