目录
1. pom.xml中引用依赖
2. 引入相关的依赖
3. 编写配置类
4. application.yml 中添加配置
5. API 常用注解
6. 访问 API 列表
7. API 导入 Postman
统⼀管理版本,在 properties 标签中加入版本号:
3.0.0
3.0.0 版本对应 Spring Boot 2.6 之前的版本,但是随着 Spring Boot 的更新 Springfox 并没有进行同步的更新,所以存在一些兼容性问题,因此我们选择使用 SpringBoot 2.7.6 版本。
io.springfox
springfox-boot-starter
${springfox-boot-starter.version}
org.springframework.boot
spring-boot-starter-actuator
将以下代码复制到 SwaggerConfig 类中:
package com.example.demo.controller;
import org.springframework.boot.actuate.autoconfigure.endpoint.web.CorsEndpointProperties;
import org.springframework.boot.actuate.autoconfigure.endpoint.web.WebEndpointProperties;
import org.springframework.boot.actuate.autoconfigure.web.server.ManagementPortType;
import org.springframework.boot.actuate.endpoint.ExposableEndpoint;
import org.springframework.boot.actuate.endpoint.web.*;
import org.springframework.boot.actuate.endpoint.web.annotation.ControllerEndpointsSupplier;
import org.springframework.boot.actuate.endpoint.web.annotation.ServletEndpointsSupplier;
import org.springframework.boot.actuate.endpoint.web.servlet.WebMvcEndpointHandlerMapping;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.util.StringUtils;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
/**
* Swagger配置类
*/
// 配置类
@Configuration
// 开启Springfox-Swagger
@EnableOpenApi
public class SwaggerConfig {
/**
* Springfox-Swagger基本配置
* @return
*/
@Bean
public Docket createApi() {
Docket docket = new Docket(DocumentationType.OAS_30)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.example.demo.controller"))
.paths(PathSelectors.any())
.build();
return docket;
}
// 配置API基本信息
private ApiInfo apiInfo() {
ApiInfo apiInfo = new ApiInfoBuilder()
.title("论坛系统API")
.description("论坛系统前后端分离API测试")
.contact(new Contact("Test",
"https://hello.fprum.com", "[email protected]"))
.version("1.0")
.build();
return apiInfo;
}
/**
* 解决SpringBoot 6.0以上与Swagger 3.0.0 不兼容的问题
* 复制即可
**/
@Bean
public WebMvcEndpointHandlerMapping
webEndpointServletHandlerMapping(WebEndpointsSupplier webEndpointsSupplier,
ServletEndpointsSupplier servletEndpointsSupplier,
ControllerEndpointsSupplier controllerEndpointsSupplier,
EndpointMediaTypes endpointMediaTypes, CorsEndpointProperties corsProperties,
WebEndpointProperties webEndpointProperties, Environment environment) {
List> allEndpoints = new ArrayList();
Collection webEndpoints =
webEndpointsSupplier.getEndpoints();
allEndpoints.addAll(webEndpoints);
allEndpoints.addAll(servletEndpointsSupplier.getEndpoints());
allEndpoints.addAll(controllerEndpointsSupplier.getEndpoints());
String basePath = webEndpointProperties.getBasePath();
EndpointMapping endpointMapping = new EndpointMapping(basePath);
boolean shouldRegisterLinksMapping =
this.shouldRegisterLinksMapping(webEndpointProperties, environment,
basePath);
return new WebMvcEndpointHandlerMapping(endpointMapping, webEndpoints,
endpointMediaTypes,
corsProperties.toCorsConfiguration(), new
EndpointLinksResolver(allEndpoints, basePath),
shouldRegisterLinksMapping, null);
}
private boolean shouldRegisterLinksMapping(WebEndpointProperties
webEndpointProperties, Environment environment,
String basePath) {
return webEndpointProperties.getDiscovery().isEnabled() &&
(StringUtils.hasText(basePath)
||
ManagementPortType.get(environment).equals(ManagementPortType.DIFFERENT));
}
}
- 在 spring 节点下添加 mvc 配置项
由于 SpringBoot 2.6 之后版本把 SpringMVC 路径匹配策略修改为 MatchingStrategy. PATH_PATTERN_PARSER;而 Springfox-Swagger 还没有更新版本,我们暂时先把 路径匹配策略回退到之前的MatchingStrategy. ANT_PATH_MATCHER
mvc:
path match:
matching-strategy: ANT_PATH_MATCHER #Springfox-Swagger兼容性配置
启动程序,在浏览器中输入网址:http://127.0.0.1:58080/swagger-ui/index.html
点击“测试接口”出现如下图所示:
选择对应的 API 列表,点击“Try it out”:
点击 Execute:
带有参数的同样可以进行测试:
在 Postman 中导入 url:
接下来输入参数: