早先在实际开发工作中我们可能都遇到过困惑,如果在整理好所有接口文档后再进行开发,那么肯定是要耽搁很多开发时间,浪费掉不必要的时间,如果不用接口文档,前后端对接又会出很多问题,那么如果等后端开发完成后再完成开发文档呢又会增加很多工作量,那么在这里我们介绍一款非常好用的在线文档生成框架,也可以称作是工具-----swagger-ui
关于简介就不多说,可以询问度娘,在这里我们主要说她的用法(基于springboot),对于任何工具和三方框架我们的使用大都可按照使用三部曲进行,那么直接上代码吧
- 添加依赖
<!-- swagger-ui 用于前后端对接的生成在线文档用 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.8.0</version>
</dependency>
添加以上关于swagger的依赖,当然,版本大家自己控制,我这里是我项目中用到的
- 增加配置
springboot我们一般采用java类来使用@Configuration
表示一个配置类,在这里要另外注意的是要在这个配置类上加上@EnableSwagger2
注解,表示启用swagger
package com.wwy.config;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RequestMethod;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.builders.ResponseMessageBuilder;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.ResponseMessage;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import springfox.documentation.service.ApiKey;
import springfox.documentation.service.AuthorizationScope;
import springfox.documentation