1, the establishment of a spring MVC project;
2. Add Pom dependency:
1 <Properties>2 <springfoxversion>2.6.1</springfoxversion>3 </Properties>4 <Dependencies>5 <Dependency>6 <groupId>Io.springfox</groupId>7 <Artifactid>Springfox-swagger2</Artifactid>8 <version>${springfoxversion}</version>9 <Scope>Compile</Scope>Ten </Dependency> One </Dependencies> A - <Dependency> - <groupId>Com.fasterxml.jackson.core</groupId> the <Artifactid>Jackson-databind</Artifactid> - <version>2.6.6</version> - </Dependency>
3. config class
1@Configuration//must exist2@EnableSwagger2//must exist3@EnableWebMvc//must exist4@ComponentScan (basepackages = {"Org.blog.controller"})//Scan API Controller package name must be present can also scan class directly (basepackageclasses)5 Public classwebappconfig{6 @Bean7 PublicDocket Customdocket () {8 //9 return NewDocket (documentationtype.swagger_2)Ten . Apiinfo (Apiinfo ()); One } A - Privateapiinfo Apiinfo () { -Contact Contact =NewContact ("Zhou", "https://cc520.me", "[email protected]"); the return NewApiinfo ("Blog Foreground API Interface",//Headline title -"Blog Foreground API Interface",//Small title -"0.0.1",//version -"Www.fangshuoit.com",//Termsofserviceurl +Contact//author -"Blog",//Link Display text +"Https://cc520.me"//site Links A ); at } -}
4. Write Controller
1 @Controller2@RequestMapping ("Home")3 Public classTest1 {4 5@ApiOperation (value= "a test API", notes = "First Test API")6@RequestMapping ("/index")7 @ResponseBody8 PublicString Index () {9 return"Index";Ten } One A}
6. Web. Xml adds code to access HTML files
1 <servlet-mapping>2 <Servlet-name>Default</Servlet-name>3 <Url-pattern>*.css</Url-pattern>4 </servlet-mapping>5 <servlet-mapping>6 <Servlet-name>Default</Servlet-name>7 <Url-pattern>*.js</Url-pattern>8 </servlet-mapping>9 <servlet-mapping>Ten <Servlet-name>Default</Servlet-name> One <Url-pattern>*.jpg</Url-pattern> A </servlet-mapping> - <servlet-mapping> - <Servlet-name>Default</Servlet-name> the <Url-pattern>*.png</Url-pattern> - </servlet-mapping> - <servlet-mapping> - <Servlet-name>Default</Servlet-name> + <Url-pattern>*.html</Url-pattern> - </servlet-mapping> + <servlet-mapping> A <Servlet-name>Default</Servlet-name> at <Url-pattern>*.htm</Url-pattern> - </servlet-mapping>
7, run, open "Http://127.0.0.1:8080/test/v2/api-docs" or "http://127.0.0.1:8080/test/swagger-ui.html" view in the browser
Springfox+swagger2 Generating API documentation