集成 Swagger
1. 在 idea 中先创建一个Spring Boot 项目
2. 修改 pom 文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.3</version>
<relativePath/>
</parent>
<groupId>top.shijialeya.swagger</groupId>
<artifactId>swagger</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<!-- 让 Maven 打包的时候忽略单元测试 -->
<skipTests>true</skipTests>
</properties>
<dependencies>
<!-- spring mvc 的集成包 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 导入 Swagger -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<!-- Swagger 前端页面包 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
</dependencies>
</project>
Swagger 现在版本出到了 3.0.0,但在目前为止 2.9.2 版本最受欢迎使用最多,这里使用 2.9.2 版本的。
3. 编写 Swagger 配置类
package top.shijialeya.swagger.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.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
/**
* @CreateTime: 2021/2/28 9:03
* @Description: Swagger 配置类
*/
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket createRestApi() {
Docket docket = new Docket(DocumentationType.SWAGGER_2)
.apiInfo(new ApiInfoBuilder().build())
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build();
return docket;
}
}
4. 编写启动器类
package top.shijialeya.swagger;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SwaggerApplication {
public static void main(String[] args) {
SpringApplication.run(SwaggerApplication.class, args);
}
}
- 修改 application.yml 文件
server:
port: 8080
-
执行启动器类
-
在浏览器中数输入地址:http://localhost:8080/swagger-ui.html,即可打开 Swagger 页面,如下:
到目前为止就已经集成好了!
接下来编一个控制器类来测试看看效果。
映射路径为 hello,并且该路径返回 hello world字符串。
package top.shijialeya.swagger.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class HelloController {
@RequestMapping("hello")
@ResponseBody
public Object hello(){
return "hello World";
}
}
再来查看 swagger 页面:http://localhost:8080/swagger-ui.html,此时就多了一个 Hello Controller 栏目,打开该栏目: