先看效果图
POM配置
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
<!-- swagger2 增强UI ,拥有好看的界面, 和接口分组,排序等功能,如不引用可自行删除-->
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>swagger-bootstrap-ui</artifactId>
<version>1.8.7</version>
</dependency>
SwaggerConfig
配置注解
@EnableSwagger2 @EnableSwaggerBootstrapUI
import com.github.xiaoymin.swaggerbootstrapui.annotations.EnableSwaggerBootstrapUI;
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;
/**
* @Title: demo
* @Description:
* @author: liaryank
* @Date: 2020/11/19 10:42 上午
* @Version: 1.0
*/
@Configuration
@EnableSwagger2
@EnableSwaggerBootstrapUI
public class SwaggerConfig {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.groupName("后台管理接口")
.apiInfo(apiInfo())
.host("localhost:8088")
.select()
.apis(RequestHandlerSelectors.basePackage("com.liuyan.demo.controller"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("demo项目API")
.description("demo系统平台,为您提供服务")
.termsOfServiceUrl("")
.version("1.0")
.build();
}
}
controller配置
启动项目即可 访问swagger文档路径为 http://localhost:8088/demo/doc.html
本机ip+端口+项目名称+doc.html
如果访问不到可以在启动类加上扫描注解。
@MapperScan("com.liuyan.demo.config")
到此swagger2 基本搭建就完成了,感谢各位看官老爷。