Swagger2文档API
为了减少程序员撰写文档时间,提高生产力,Swagger2应运而生,使用Swagger2可以减少编写过多的文档,只需要通过代码就能生成文档API,提供给前端人员对接,非常方便。
引入依赖
<!-- 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>
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>swagger-bootstrap-ui</artifactId>
<version>1.6</version>
</dependency>
设置跨域访问
@Configuration
@EnableSwagger2
public class Swigger2 {
//配置swagger2核心配置
@Bean
public Docket creakRestApi() {
return new Docket(DocumentationType.SWAGGER_2) //指定api类型为swigger2
.apiInfo(this.apiInfo())//指定api类型为swigger2
.select()
.apis(RequestHandlerSelectors
.basePackage("com.dada.controller")) //指定controller
.paths(PathSelectors.any()) //地址下的所有(any)
.build();
}
private ApiInfo apiInfo(){
return new ApiInfoBuilder()
.title("哒哒网电商平台接口api")//标题
.contact(new Contact("小马哒哒哒","http://dadawang.xyz","984501240@qq.com"))//联系人信息
.description("专为哒哒网提供的api文档") //详细信息
.version("1.0.1")
.termsOfServiceUrl("http://www.dadawang.xyz")
.build();
}
}