springboot 集成swagger2 自动生成api 文档
1 导包
<!-- 构建Restful API 我这版本是2.6.1 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.6.1</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.6.1</version>
</dependency>
2 配置文件
package com.liu.netty.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.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration
@EnableSwagger2
public class Swagger2Config {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2).forCodeGeneration(true)
.select()
.apis(RequestHandlerSelectors.any())
//过滤生成链接
.paths(PathSelectors.any())
.build()
.apiInfo(apiInfo());
}
//api接口作者相关信息
private ApiInfo apiInfo() {
Contact contact = new Contact("MR.T", "", "shi860715@126.com");
ApiInfo apiInfo = new ApiInfoBuilder()
.license("Apache License Version 2.0")
.title("生兴自动化虫情系统")
.description("接口文档")
.contact(contact)
.version("1.0")
.build();
return apiInfo;
}
}
3 测试
http://localhost:9001/swagger-ui.html