Springfox+swagger2 Generating API documentation

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.