swagger提供开发者文档
========================================================
作用:想使用swagger的同学,一定是想用它来做前后台分离,后台开发为前台提供API,以供前台的同学调用和调试。
那么swagger的作用就是上面这句话。
具体swagger包含了哪些,swagger官网展示的很齐全
本篇只表达swagger2+spring boot怎么用,也是给想用swagger但是无从下手的同学们带带路!!!!
========================================================
本篇需要准备的东西:
InterllJ Idea/JDK1.8/spring boot 项目一个
========================================================
在这开始之前,你要自己去搭建一个spring boot项目,至于使用maven管理架包或者gradle管理架包,看你自己。
参考地址:
http://www.cnblogs.com/sxdcgaq8080/p/7712874.html. maven+spring boot
http://www.cnblogs.com/sxdcgaq8080/p/8717914.html gradle+spring boot
1.添加依赖
gradle添加依赖
// https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui compile group: 'io.springfox', name: 'springfox-swagger-ui', version: '2.8.0' // https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 compile group: 'io.springfox', name: 'springfox-swagger2', version: '2.8.0'
maven添加依赖
<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>
2.swagger配置类
其实这个配置类,只要了解具体能配置哪些东西就好了,毕竟这个东西配置一次之后就不用再动了。
【注意1】:特别要注意的是里面配置了api文件也就是controller包的路径,你要生成API接口文档的controller文件都是放在这个包路径下的,不然生成的文档扫描不到接口。
【注意2】:这个swagger配置类,是要放在你Application类同级,也就是Springboot项目的启动类的同级
package com.sxd.sweeping; 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.service.ApiInfo; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.swagger2.annotations.EnableSwagger2; @Configuration @EnableSwagger2 public class Swagger2 { @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.basePackage("com.sxd.sweeping.controller")) .paths(PathSelectors.any()) .build(); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("Spring Boot中使用Swagger2构建RESTful APIs") .description("更多精彩博客请关注:http://www.cnblogs.com/sxdcgaq8080/") .termsOfServiceUrl("http://www.cnblogs.com/sxdcgaq8080/") .contact("Angel挤一挤") .version("1.0") .build(); } }