最近新入职了一家公司,项目开发完毕发现没有写接口的API文档工具,问了一下组长,组长告诉我说日常都是口头交流调试,用不着接口文档......想着能少费点口水,因而简单就整合一个swagger2,下面贴上代码。html
咱们用的是Maven项目,因此直接在pom.xml中进行依赖,由于咱们项目使用的Springboot-1.3.5,因此依赖比较低,若是是高版本的小伙伴,能够自行下载对应的版本依赖。java
附上maven仓库地址:https://mvnrepository.com/search?q=swagger2web
<!-- swagger2--> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.4.0</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.4.0</version> </dependency>
而后咱们须要新建一个swagger配置类spring
package net.XXXX.xx.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import springfox.documentation.builders.ApiInfoBuilder; import springfox.documentation.builders.PathSelectors; import springfox.documentation.builders.RequestHandlerSelectors; import springfox.documentation.schema.ModelRef; import springfox.documentation.service.ApiInfo; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.swagger2.annotations.EnableSwagger2; import springfox.documentation.service.Parameter; import springfox.documentation.builders.ParameterBuilder; import java.util.ArrayList; import java.util.List; @Configuration @EnableSwagger2 public class SwaggerConfig { @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.basePackage("net.XXX.xx"))//这里是本身的包结构 .paths(PathSelectors.any()) .build() .globalOperationParameters(globalOperation()); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("接口示例文档") .description("接口文档Demo") .version("1.0") .build(); } //下面代码能够酌情处理,由于咱们接口须要有token验证,因此须要配置一下token输入框,下面代码就是作这个功能的 private List<Parameter> globalOperation(){ //添加head参数配置start ParameterBuilder tokenPar = new ParameterBuilder(); List<Parameter> pars = new ArrayList<>(); //第一个token为传参的key,第二个token为swagger页面显示的值 tokenPar.name("token").description("token").modelRef(new ModelRef("string")).parameterType("header").required(false).build(); pars.add(tokenPar.build()); return pars; } }
若是你们项目中有过滤器,须要将swagger的访问地址释放掉,将如下地址进行释放。api
swagger-resources/* swagger-ui.html/* v2/* webjars/*
这里咱们用一个GET请求作一下示例,标注一下Controller以及参数,请忽略代码内容~app
@Api(value = "RevenueBudgetReisterController", description = "收入预算登记API") @RestController @RequestMapping("/revenueBudgetRegister") public class RevenueBudgetReisterController extends BaseController { private RevenueBudgetRegisterService registerService; @Autowired public RevenueBudgetReisterController(RevenueBudgetRegisterService registerService) { this.registerService = registerService; } @ApiOperation(value = "获取一条记录", notes = "获取预算的明细记录") @ApiImplicitParams({ @ApiImplicitParam(name = "year", value = "年份", required = true, dataType = "String", paramType = "query"), @ApiImplicitParam(name = "dictionaryName", value = "科目名称", required = false, dataType = "String", paramType = "query"), @ApiImplicitParam(name = "departmentId", value = "部门ID", required = true, dataType = "String", paramType = "query") }) @RequestMapping(value = "/v1.0/revenueBudgetTetails", method = RequestMethod.GET) public ResultDto revenueBudgetTetails(String year, String dictionaryName, String departmentId) { if (null == year || year.equals("") || null == departmentId || "".equals(departmentId)) { return new ResultDto(ResultDto.CODE_FAIL, RevenueBudgetDetailsController.ERROR_DATA, null); } List<RevenueBudgetDetailsVo> details = registerService.details(year, dictionaryName, departmentId); return new ResultDto(ResultDto.CODE_SUCCESS, RevenueBudgetDetailsController.SUCCEEDED, details); } }
访问地址:http://localhost:8006/swagger-ui.html#/ 地址端口号你们请更改成与本身相对应的,我这里设置的是8006maven
bingo!~ 出现这个界面就表示成功了!( 老汉激动摸了摸本身的秃头,从轮椅上站了起来,流下了开心的泪水.....)工具
而后咱们随便点进去一个ui
这里会显示咱们接口的入参,以及请求方式等信息,若是咱们配置了token输入框,这里则会显示出来。this
可能集成swagger会出现下面这个异常。
集成swagger2报错:java.lang.NoSuchMethodError: com.google.common.XXX
出现这个异常是由于swagger中内置了一个谷歌的guava包,而项目中也有guava的依赖,版本不一致致使的冲突,咱们须要将版本进行匹配,或者将低版本的依赖给排除掉,我这里下降了版本,原本项目中依赖的是swagger-2.9.2的版本,因而下降到了swagger-2.4.0版本,完美解决~
-
swagger经过注解代表该接口会生成文档,包括接口名、请求方法、参数、返回信息的等等。
本次教程就到这里了,谢谢你们阅读, 写的有不对的地方还请多多包涵以及提出!