Skip to content

Commit 02f8bb8

Browse files
committed
feat: 整合 Swagger2、Swagger Bootstrap UI
1 parent f808a15 commit 02f8bb8

2 files changed

Lines changed: 97 additions & 17 deletions

File tree

pom.xml

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,16 @@
88
<version>2.5.2</version>
99
<relativePath />
1010
</parent>
11-
<groupId>info.cadecode</groupId>
11+
<groupId>top.cadecode</groupId>
1212
<artifactId>simple-spring-boot</artifactId>
1313
<version>0.0.1-SNAPSHOT</version>
1414
<packaging>war</packaging>
1515
<name>simple-spring-boot</name>
1616
<description>Demo project for Spring Boot</description>
1717
<properties>
1818
<java.version>1.8</java.version>
19+
<springfox-swgger2.version>2.9.2</springfox-swgger2.version>
20+
<swgger-bootstrap-ui.version>1.9.6</swgger-bootstrap-ui.version>
1921
</properties>
2022

2123
<repositories>
@@ -37,41 +39,55 @@
3739
<groupId>org.springframework.boot</groupId>
3840
<artifactId>spring-boot-starter-web</artifactId>
3941
</dependency>
40-
<!--<dependency>-->
41-
<!-- <groupId>org.mybatis.spring.boot</groupId>-->
42-
<!-- <artifactId>mybatis-spring-boot-starter</artifactId>-->
43-
<!-- <version>2.2.0</version>-->
44-
<!--</dependency>-->
42+
<dependency>
43+
<groupId>org.springframework.boot</groupId>
44+
<artifactId>spring-boot-configuration-processor</artifactId>
45+
<optional>true</optional>
46+
</dependency>
47+
<dependency>
48+
<groupId>org.springframework.boot</groupId>
49+
<artifactId>spring-boot-starter-tomcat</artifactId>
50+
<scope>provided</scope>
51+
</dependency>
52+
<dependency>
53+
<groupId>org.springframework.boot</groupId>
54+
<artifactId>spring-boot-starter-test</artifactId>
55+
<scope>test</scope>
56+
</dependency>
4557
<dependency>
4658
<groupId>org.springframework.boot</groupId>
4759
<artifactId>spring-boot-devtools</artifactId>
4860
<scope>runtime</scope>
4961
<optional>true</optional>
5062
</dependency>
63+
<!--mybatis-->
64+
<!--<dependency>-->
65+
<!-- <groupId>org.mybatis.spring.boot</groupId>-->
66+
<!-- <artifactId>mybatis-spring-boot-starter</artifactId>-->
67+
<!-- <version>2.2.0</version>-->
68+
<!--</dependency>-->
69+
<!--mysql 驱动-->
5170
<dependency>
5271
<groupId>mysql</groupId>
5372
<artifactId>mysql-connector-java</artifactId>
5473
<scope>runtime</scope>
5574
</dependency>
56-
<dependency>
57-
<groupId>org.springframework.boot</groupId>
58-
<artifactId>spring-boot-configuration-processor</artifactId>
59-
<optional>true</optional>
60-
</dependency>
75+
<!--lombok-->
6176
<dependency>
6277
<groupId>org.projectlombok</groupId>
6378
<artifactId>lombok</artifactId>
6479
<optional>true</optional>
6580
</dependency>
81+
<!--swagger-->
6682
<dependency>
67-
<groupId>org.springframework.boot</groupId>
68-
<artifactId>spring-boot-starter-tomcat</artifactId>
69-
<scope>provided</scope>
83+
<groupId>io.springfox</groupId>
84+
<artifactId>springfox-swagger2</artifactId>
85+
<version>${springfox-swgger2.version}</version>
7086
</dependency>
7187
<dependency>
72-
<groupId>org.springframework.boot</groupId>
73-
<artifactId>spring-boot-starter-test</artifactId>
74-
<scope>test</scope>
88+
<groupId>com.github.xiaoymin</groupId>
89+
<artifactId>swagger-bootstrap-ui</artifactId>
90+
<version>${swgger-bootstrap-ui.version}</version>
7591
</dependency>
7692
</dependencies>
7793

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package top.cadecode.simple.config;
2+
3+
import com.github.xiaoymin.swaggerbootstrapui.annotations.EnableSwaggerBootstrapUI;
4+
import org.springframework.context.annotation.Bean;
5+
import org.springframework.context.annotation.Configuration;
6+
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
7+
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
8+
import springfox.documentation.builders.ApiInfoBuilder;
9+
import springfox.documentation.builders.RequestHandlerSelectors;
10+
import springfox.documentation.service.ApiInfo;
11+
import springfox.documentation.service.Contact;
12+
import springfox.documentation.spi.DocumentationType;
13+
import springfox.documentation.spring.web.plugins.Docket;
14+
import springfox.documentation.swagger2.annotations.EnableSwagger2;
15+
16+
/**
17+
* @author Li Jun
18+
* @date 2021/8/23
19+
* @description Swagger2 配置类
20+
*/
21+
@Configuration
22+
@EnableSwagger2
23+
@EnableSwaggerBootstrapUI
24+
public class SwaggerConfig implements WebMvcConfigurer {
25+
26+
// 配置 Swagger Docket
27+
@Bean
28+
public Docket docket() {
29+
return new Docket(DocumentationType.SWAGGER_2)
30+
// 设置文档信息
31+
.apiInfo(apiInfo())
32+
.select()
33+
// 设置监听包
34+
.apis(RequestHandlerSelectors.basePackage("top.cadecode.simple.controller"))
35+
.build()
36+
// 设置分组名称
37+
.groupName("默认分组");
38+
}
39+
40+
// 配置文档信息
41+
private ApiInfo apiInfo() {
42+
return new ApiInfoBuilder()
43+
// 设置标题
44+
.title("API Online Document")
45+
// 设置描述
46+
.description("simple-spring-boot 在线文档")
47+
// 设置联系方法
48+
.contact(new Contact("Cade Li",
49+
"https://github.com/cadecode/simple-spring-boot",
50+
"cadecode@foxmail.com"))
51+
// 设置版本号
52+
.version("0.0.1")
53+
.build();
54+
}
55+
56+
// 添加静态资源,映射 Swagger 网页文件
57+
@Override
58+
public void addResourceHandlers(ResourceHandlerRegistry registry) {
59+
registry.addResourceHandler("doc.html")
60+
.addResourceLocations("classpath:/META-INF/resources/");
61+
registry.addResourceHandler("/webjars/**")
62+
.addResourceLocations("classpath:/META-INF/resources/webjars/");
63+
}
64+
}

0 commit comments

Comments
 (0)