1.pom.xml添加依赖
<!-- swagger2 配置 前两个为官方,第三个是另一种ui -->
<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>
2.写Swagger2的配置类
package com.zkx.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 Swagger2 {
// http://localhost:8080/swagger-ui.html 原路径
// http://localhost:8080/doc.html 原路径
// 配置swagger2核心配置
@Bean
public Docket createRestApi(){
return new Docket(DocumentationType.SWAGGER_2) //指定api类型为swagger2
.apiInfo(apiInfo()) 用于定义api文档汇总信息
.select()
.apis(RequestHandlerSelectors
.basePackage("com.zkx.controller")) //指定controller包
.paths(PathSelectors.any()) //所有controller
.build();
}
private ApiInfo apiInfo(){
return new ApiInfoBuilder()
.title("标题") //文档页标题
.contact(new Contact("name","url","email")) //联系人信息
.description("") //详细信息
.version("1.0") //版本信息
.termsOfServiceUrl("") //网站地址
.build();
}
}
3.访问http://localhost:8080/swagger-ui.html或http://localhost:8080/doc.html (两种ui)
# 如果有静态页面,在配置文件里配置了静态页面的访问路径,swagger文档会出现404,需要在写个WebMvcConfig的配置类配置一下