1、首先打开项目的pom.xml文件添加相关依赖
<!--引入swagger2-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.8.0</version>
</dependency>
<!--引入swagger-ui-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.8.0</version>
</dependency>
2、新建一个包放置Swagger2配置类(例如:com.we.weshop.config.Swagger2Configuration.java)
package com.we.weshop.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.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
/**
* Swagger2 配置类
* @author E
* @date 2018/6/4 11:01
*/
@Configuration
public class Swagger2Configuration {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.we.weshop"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("WESHOP API文档")
.description("power by E,1565181015@qq.com")
.termsOfServiceUrl("https://blog.csdn.net/qq_40738872")
.version("1.0")
.build();
}
}
3、Reimport 一下pom.xml 文件下载相关jar包
4、在启动类上添加@EnableSwagger2 注解
5、controller在方法上添加相关注解(例如如下)
@GetMapping("/index")
@ApiOperation(value = "返回后台管理主页",notes = "返回后台管理页面")
public String index(){
return "index";
}
6、运行项目,访问http://localhost:8090/swagger-ui.html(注:端口号因人而异)