在其它两篇文章中,已经解决的自定义枚举在MyBatis以及Rest接口的转换,但是在Springfox中还存在问题,不能使用code来作为api。本文通过扩展Springfox,实现了对自定义枚举的良好支持。

ps: 枚举的定义参见 自定义枚举 --- MyBatis字段映射

当前

 
Springfox默认枚举

存在2个问题

  1. 类型显示为string,需要修改为integer
  2. 枚举的类型显示为枚举值,需要修改为枚举的code值(CodedEnum的定义请参见其他文章)

扩展后

 
扩展Springfox后的枚举展示

实现方式

实现ModelPropertyBuilderPlugin接口,

  1. @Component
  2. public class CodedEnumPropertyPlugin implements ModelPropertyBuilderPlugin {
  3. @Override
  4. public void apply(ModelPropertyContext context) {
  5. Optional<ApiModelProperty> annotation = Optional.absent();
  6.  
  7. if (context.getAnnotatedElement().isPresent()) {
  8. annotation = annotation.or(ApiModelProperties.findApiModePropertyAnnotation(context.getAnnotatedElement().get()));
  9. }
  10. if (context.getBeanPropertyDefinition().isPresent()) {
  11. annotation = annotation.or(Annotations.findPropertyAnnotation(
  12. context.getBeanPropertyDefinition().get(),
  13. ApiModelProperty.class));
  14. }
  15. final Class<?> rawPrimaryType = context.getBeanPropertyDefinition().get().getRawPrimaryType();
  16. //过滤得到目标类型
  17. if (annotation.isPresent() && CodedEnum.class.isAssignableFrom(rawPrimaryType)) {
  18. //获取CodedEnum的code值
  19. CodedEnum[] values = (CodedEnum[]) rawPrimaryType.getEnumConstants();
  20. final List<String> displayValues = Arrays.stream(values).map(codedEnum -> Integer.toString(codedEnum.getCode())).collect(Collectors.toList());
  21. final AllowableListValues allowableListValues = new AllowableListValues(displayValues, rawPrimaryType.getTypeName());
  22. //固定设置为int类型
  23. final ResolvedType resolvedType = context.getResolver().resolve(int.class);
  24. context.getBuilder().allowableValues(allowableListValues).type(resolvedType);
  25. }
  26. }
  27.  
  28. @Override
  29. public boolean supports(DocumentationType documentationType) {
  30. return true;
  31. }
  32. }

作者:十毛tenmao
链接:https://www.jianshu.com/p/1ebe41c5f284
来源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

自定义枚举 --- Swagger文档展示的更多相关文章

  1. 使用 Swagger 文档化和定义 RESTful API

    大部分 Web 应用程序都支持 RESTful API,但不同于 SOAP API——REST API 依赖于 HTTP 方法,缺少与 Web 服务描述语言(Web Services Descript ...

  2. Swagger文档转Word 文档

    GitHub 地址:https://github.com/JMCuixy/SwaggerToWord/tree/developer 原创作品,转载请注明出处:http://www.cnblogs.co ...

  3. Swagger文档转Word

    Swagger文档转Word 文档   GitHub 地址:https://github.com/JMCuixy/SwaggerToWord/tree/developer 原创作品,转载请注明出处:h ...

  4. asp.net core web api 生成 swagger 文档

    asp.net core web api 生成 swagger 文档 Intro 在前后端分离的开发模式下,文档就显得比较重要,哪个接口要传哪些参数,如果一两个接口还好,口头上直接沟通好就可以了,如果 ...

  5. 懒得写文档,swagger文档导出来不香吗

    导航 前言 离线文档 1 保存为html 2 导出成pdf文档 3 导出成Word文档 参考 前言   早前笔者曾经写过一篇文章<研发团队,请管好你的API文档>.团队协作中,开发文档的重 ...

  6. SpringBoot系列:六、集成Swagger文档

    本篇开始介绍Api文档Swagger的集成 一.引入maven依赖 <dependency> <groupId>io.springfox</groupId> < ...

  7. revel + swagger 文档也能互动啦

    beego 从 1.3 后开始支持自动化API文档,不过,目测比较复杂,一直期望 revel 能有官方支持. revel 确实已经有了官方支持的计划,有可能将在 0.14 版本支持,现在才 0.11. ...

  8. 利用typescript生成Swagger文档

    项目地址:https://github.com/wz2cool/swagger-ts-doc demo代码地址:https://github.com/wz2cool/swagger-ts-doc-de ...

  9. springboot成神之——swagger文档自动生成工具

    本文讲解如何在spring-boot中使用swagger文档自动生成工具 目录结构 说明 依赖 SwaggerConfig 开启api界面 JSR 303注释信息 Swagger核心注释 User T ...

随机推荐

  1. [转帖]pidstat 命令详解

    pidstat 命令详解 https://www.jianshu.com/p/3991c0dba094 pidstat -r -u -d -p 各种参数非常好用. pidstat 概述 pidstat ...

  2. 来自后端的逆袭 blazor简介 全栈的福音

    背景 什么是SPA 什么是MPA MPA (Multi-page Application) 多页面应用指的就是最传统的 HTML 网页设计,早期的网站都是这样的设计,所之称为「网页设计」.使用 MPA ...

  3. mysql 5.7 修改root密码允许远程连接

    1.修改root密码(其他用户类似)  试过网上看的一些 在mysql数据库执行 update user set password='新密码'  where user='root' 执行说找不到字段, ...

  4. 在excel实现多级联动

    最近做了一个Excel的多级联动的功能,具体是将全国所有的气象局按一二三四级单位做成四列,实现各级的联动下拉选择,这和省市县乡的各级联动的功能基本一样,下面记录下具体的操作步骤. 1.首先需要从数据库 ...

  5. Java判断两个时间相差的天数

    1.实现目标 输入:两个日期 输出:两个日期相差的天数 2.代码实现 方法1: 通过Calendar类的日期比较.注意:这里需要考虑一下: 日期是跨年份的,如一个是2012年,一个是2015年的   ...

  6. 门控循环单元(GRU)与 LSTM 的区别

    29 November 2019 14:48 GRU is a popular variant of LSTM which replaces the forget gate and the input ...

  7. Integrated SOA Gateway 不是当前用户的有效责任。请联系您的系统管理员。

    问题:给用户新增职责集成SOA网关,点击路径进入时出现报错:"Integrated SOA Gateway 不是当前用户的有效责任.请联系您的系统管理员." 解决:功能管理员职责, ...

  8. sqlite3 FTS全文索引按照相关性排序

    PS.后来发现,以下代码并不准确.请大家不要试了. -------以下是原内容------------------------------------------------------------- ...

  9. 【前端_js】Bootstrap之表单验证

    Bootstrap表单验证插件bootstrapValidator使用方法整理 BootstrapValidator 表单验证超详细教程    

  10. rest framework 之解析器

    一.示例 1.api/urls.py from django.urls import path, re_path from api.views import UserView, ParserView ...