Swagger实现动态条件注入与全局拦截功能详细流程
作者:毕小宝 时间:2023-11-23 13:41:05
背景
Swagger 可以提供 API 操作的测试文档,本文记录 Swagger 使用过程中遇到的两个小问题:
全局响应结果进行包装后导致 Swagger 请求响应结果无法解析,如果项目中配置了
ResponseBodyAdvice
实现类,重写了beforeBodyWrite
方法对响应结果进行统一时,应该排除 Swagger2 的两个 URLv2/api-docs
和swagger-resources
。Swagger 注入条件区分测试和生产环境的配置的 ETL 不同语法对默认无配置的处理结果。
Swagger2 使用流程
Swagger 用法很简单,加入引用,一个配置就可以为应用提供接口测试界面。
第一步,引入依赖。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<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.0</version>
</dependency>
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>swagger-bootstrap-ui</artifactId>
<version>1.8.5</version>
</dependency>
第二步,添加 Swagger 配置类。
@Configuration
@EnableSwagger2
@EnableSwaggerBootstrapUI
@ConditionalOnExpression("'${swagger.enable}'.equalsIgnoreCase('true')")
public class Swagger2Config {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("XXX 文档")
.version("1.0")
.build();
}
}
第三步,为 Web 请求设置 Api 配置。
@GetMapping("/set")
@ApiOperation(value = "设置 Session")
public Object Index(HttpServletRequest request){
request.getSession().setAttribute("userUid", "111111");
return request.getSession().getAttribute("userUid");
}
条件配置分析
@ConditionalOnExpression("'${swagger.enable}'.equalsIgnoreCase('true')")
这种方式,表达式成立的条件有两项 swagger.enable 必须配置,且值为 true 时,才会成立。
另外一种写法:
@ConditionalOnExpression("${swagger.enable:true}")
如果未配置属性 swagger.enable
,那么根据 ConditionalOnExpression 注解的默认值是 true ,所以就会执行注解配置。
启示录
为了方便开发测试,默认无配置时视为满足条件,生产环境下配置值为 false 是可以的。
来源:https://blog.csdn.net/wojiushiwo945you/article/details/128779434
标签:Swagger,动态,条件注入,全局拦截
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Mybatis映射文件规则实例详解
2022-04-22 03:08:14
![](https://img.aspxhome.com/file/2023/3/125313_0s.jpg)
Javaweb动态开发最重要的Servlet详解
2023-04-09 20:11:17
![](https://img.aspxhome.com/file/2023/4/84274_0s.png)
Android性能之冷启动优化详析
2022-03-06 13:42:51
![](https://img.aspxhome.com/file/2023/4/106864_0s.jpg)
Admin - SpringBoot + Maven 多启动环境配置实例详解
2022-03-08 09:48:16
Android自定义EditText右侧带图片控件
2023-08-30 03:09:47
![](https://img.aspxhome.com/file/2023/6/126796_0s.jpg)
Java中实现简单的Excel导出
2021-10-21 07:23:25
![](https://img.aspxhome.com/file/2023/0/68730_0s.png)
spring装配bean的3种方式总结
2023-01-05 07:01:47
再议C#中的装箱与拆箱的问题详解
2022-09-30 03:45:40
Service Activity的三种交互方式(详解)
2022-03-16 20:55:08
Java泛型中<?>和<T>的区别浅析
2023-11-25 05:20:09
IDEA 设置显示内存的使用情况和内存回收的方法
2023-10-08 15:51:25
![](https://img.aspxhome.com/file/2023/7/81367_0s.jpg)
Java线程状态变换过程代码解析
2023-08-06 18:05:29
![](https://img.aspxhome.com/file/2023/1/57951_0s.png)
GC调优实战之高分配速率High Allocation Rate
2021-09-30 22:03:21
Java Spring AOP之PointCut案例详解
2023-05-24 16:46:15
关于c#中单例模式的一些问题
2022-01-21 08:56:18
![](https://img.aspxhome.com/file/2023/2/102212_0s.png)
Android实现多级树形菜单并支持多选功能
2023-08-29 20:01:04
![](https://img.aspxhome.com/file/2023/7/103907_0s.jpg)
Java实现把两个数组合并为一个的方法总结
2022-08-04 16:30:03
Android异步方法以同步方式实现
2022-11-05 15:13:50
Android TextView显示Html类解析的网页和图片及自定义标签用法示例
2023-06-12 04:39:41
Java三大特性之多态详解
2022-11-07 14:24:40