springfox+swagger2生成API文档

1、建立一个spring mvc工程;

2、添加POM依赖:

 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>
10     </dependency>
11 </dependencies>
12 
13 <dependency>
14     <groupId>com.fasterxml.jackson.core</groupId>
15     <artifactId>jackson-databind</artifactId>
16     <version>2.6.6</version>
17 </dependency>

3、config类

 1 @Configuration //必须存在
 2 @EnableSwagger2 //必须存在
 3 @EnableWebMvc //必须存在
 4 @ComponentScan(basePackages = {"org.blog.controller"}) //必须存在 扫描的API Controller package name 也可以直接扫描class (basePackageClasses)
 5 public class WebAppConfig{
 6     @Bean
 7     public Docket customDocket() {
 8         //
 9         return new Docket(DocumentationType.SWAGGER_2)
10                 .apiInfo(apiInfo());
11     }
12 
13     private ApiInfo apiInfo() {
14         Contact contact = new Contact("周发扬", "https://cc520.me", "yangyang_666@icloud.com");
15         return new ApiInfo("Blog前台API接口",//大标题 title
16                 "Blog前台API接口",//小标题
17                 "0.0.1",//版本
18                 "www.fangshuoit.com",//termsOfServiceUrl
19                 contact,//作者
20                 "Blog",//链接显示文字
21                 "https://cc520.me"//网站链接
22         );
23     }
24 }

4、编写Controller

 1 @Controller
 2 @RequestMapping("/home")
 3 public class test1 {
 4 
 5     @ApiOperation(value="一个测试API",notes = "第一个测试api")
 6     @RequestMapping("/index")
 7     @ResponseBody
 8     public String index() {
 9         return "index";
10     }
11 
12 }

6、web.xml增加能访问html文件的代码

 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>
10         <servlet-name>default</servlet-name>
11         <url-pattern>*.jpg</url-pattern>
12     </servlet-mapping>
13     <servlet-mapping>
14         <servlet-name>default</servlet-name>
15         <url-pattern>*.png</url-pattern>
16     </servlet-mapping>
17     <servlet-mapping>
18         <servlet-name>default</servlet-name>
19         <url-pattern>*.html</url-pattern>
20     </servlet-mapping>
21     <servlet-mapping>
22         <servlet-name>default</servlet-name>
23         <url-pattern>*.htm</url-pattern>
24     </servlet-mapping>

7、运行,浏览器中打开“http://127.0.0.1:8080/test/v2/api-docs”或者“http://127.0.0.1:8080/test/swagger-ui.html”查看

posted on 2018-07-15 01:18  小夏coding  阅读(256)  评论(0编辑  收藏  举报

导航