SpringBoot2 整合 Swagger2文档 使用BootstrapUI页面
SpringBoot2 整合 Swagger2
SpringBoot整合三板斧
第一步、引入pom
<dependency>
  <groupId>com.spring4all</groupId>
  <artifactId>swagger-spring-boot-starter</artifactId>
  <version>1.9.0.RELEASE</version>
</dependency>
<dependency>
  <groupId>com.github.xiaoymin</groupId>
  <artifactId>swagger-bootstrap-ui</artifactId>
  <version>1.9.6</version>
</dependency>
<dependency>
  <groupId>io.swagger</groupId>
  <artifactId>swagger-annotations</artifactId>
  <version>1.5.22</version>
</dependency>
<dependency>
  <groupId>io.swagger</groupId>
  <artifactId>swagger-models</artifactId>
  <version>1.5.22</version>
</dependency>
swagger-spring-boot-starter该项目主要利用Spring Boot的自动化配置特性来实现快速的将swagger2引入spring boot应用来生成API文档,简化原生使用swagger2的整合代码。
swagger-bootstrap-ui是springfox-swagger的增强UI实现,为Java开发者在使用Swagger的时候,能拥有一份简洁、强大的接口文档体验
swagger-annotations,swagger-models是因为springfox-swagger2包里有swagger-models-1.5.20.jar报错。所以替换成1.5.22版本
java.lang.NumberFormatException: For input string: ""
	at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
	at java.lang.Long.parseLong(Long.java:601)
	at java.lang.Long.valueOf(Long.java:803)
	at io.swagger.models.parameters.AbstractSerializableParameter.getExample(AbstractSerializableParameter.java:412)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at......
看下1.5.20版本里AbstractSerializableParameter.java源码:
public Object getExample() {
    if (this.example == null) {
        return null;
    } else {
        try {
            if ("integer".equals(this.type)) {
                return Long.valueOf(this.example);
            }
            if ("number".equals(this.type)) {
                return Double.valueOf(this.example);
            }
            if ("boolean".equals(this.type) && ("true".equalsIgnoreCase(this.example) || "false".equalsIgnoreCase(this.defaultValue))) {
                return Boolean.valueOf(this.example);
            }
        } catch (NumberFormatException var2) {
            LOGGER.warn(String.format("Illegal DefaultValue %s for parameter type %s", this.defaultValue, this.type), var2);
        }
        return this.example;
    }
}
这里只判断了this.example == null才返回null,其余会去进行转换,而空字符串也会进行转换,导致格式抛出格式化转换异常.再来看下1.5.22版本里AbstractSerializableParameter.java源码:
public Object getExample() {
    if (this.example != null && !this.example.isEmpty()) {
        try {
            if ("integer".equals(this.type)) {
                return Long.valueOf(this.example);
            }
            if ("number".equals(this.type)) {
                return Double.valueOf(this.example);
            }
            if ("boolean".equals(this.type) && ("true".equalsIgnoreCase(this.example) || "false".equalsIgnoreCase(this.defaultValue))) {
                return Boolean.valueOf(this.example);
            }
        } catch (NumberFormatException var2) {
            LOGGER.warn(String.format("Illegal DefaultValue %s for parameter type %s", this.defaultValue, this.type), var2);
        }
        return this.example;
    } else {
        return this.example;
    }
}
对example同时进行了null和空值的判断,官方也发现了自己的这个问题,我们进行相应的替换即可
第二部、配置
swagger-spring-boot-starter相关配置信息可参考如下地址:
- 源码地址
 - 使用样例:https://github.com/dyc87112/swagger-starter-demo
 - 博客:http://blog.didispace.com
 - 社区:http://www.spring4all.com
 
swagger-bootstrap-ui相关配置信息可参考如下地址:
官方地址:https://doc.xiaominfo.com/guide/
swagger-bootstrap-ui目前已改名了knife4j-spring-boot-starter项目正式更名为knife4j,取名knife4j是希望她能像一把匕首一样小巧,轻量,并且功能强悍,更名也是希望把她做成一个为Swagger接口文档服务的通用性解决方案,不仅仅只是专注于前端Ui前端.
swagger-bootstrap-ui的所有特性都会集中在
knife4j-spring-ui包中,并且后续也会满足开发者更多的个性化需求.
swagger:
  version: 1.0v # 版本号
  authorization: # 全局参数
    name: Authorization # 鉴权策略ID,对应 SecurityReferences ID
    type: ApiKey # 鉴权策略,可选 ApiKey | BasicAuth | None,默认ApiKey
    key-name: X-Token # 鉴权传递的Header参数
  #    auth-regex: ^.*$ # 需要开启鉴权URL的正则, 默认^.*$匹配所有URL
  ui-config: # 排序规则
    operations-sorter: method # 按方法定义顺序排序
    tags-sorter: alpha # 按字母表排序
  docket: # 分组配置
    common:
      base-package: com.xxxx.a
      description: API接口文档
      title: xxx接口
      contact:
        name: xxx
        url: https://cn.bing.com/
    hq:
      base-package: com.xxxx.b
      description: API接口文档
      title: xxx接口
      contact:
        name: xxx
        url: https://zc.happyloves.cn:4443/wordpress/
    shop:
      base-package: com.xxxx.c
      description: API接口文档
      title: xxx接口
      contact:
        name: xxx
        url: https://zc.happyloves.cn
第三步、注解
@EnableSwagger2Doc // 启用Swagger2
@EnableSwaggerBootstrapUI //启用swagger-bootstrap-ui
@SpringBootApplication
public class WebApplication {
    public static void main(String[] args) {
        SpringApplication.run(WebApplication.class, args);
    }
}
编写代码
@Api(value = "DemoOne-DemoOne服务~~~~~~~~", tags = {"1-DemoOne-DemoOne服务"})
@Slf4j
@Validated
@RestController
@RequestMapping("/common/DemoOne")
public class DemoOneController {
    private final DemoOneService service;
    @Autowired
    public DemoOneController(DemoOneService service) {
        this.service = service;
    }
    //=====================================================================================DELETE=====================================================================================
    @ApiOperation(value = "根据主键ID删除", notes = "根据主键ID删除~~~~~~~~~~~~~")
    @DeleteMapping("/{id}")
    public ApiMessage deleteById(@PathVariable @Min(1) int id) throws Exception {
        return service.deleteById(id);
    }
    //=====================================================================================GET========================================================================================
    @ApiOperation(value = "获取所有数据", notes = "获取所有数据~~~~~~~~~~~~~")
    @GetMapping("/")
    public ApiMessage<List<DemoOneResponse>> getAllList() {
        return service.getAllList();
    }
    @ApiOperation(value = "根据主键ID获取数据", notes = "根据主键ID获取数据~~~~~~~~~~~~~")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "id", required = true, value = "主键ID", paramType = "path", dataType = "string"),
    })
    @GetMapping("/{id}/{name}")
    public ApiMessage<DemoOneResponse> getById(@PathVariable @Min(1) int id, @PathVariable @AssertFalse boolean name) {
        return service.getById(id);
    }
    //=====================================================================================POST=======================================================================================
    @ApiOperation(value = "新增DemoOne数据", notes = "新增DemoOne数据~~~~~~~~~~~~~")
    @PostMapping("/")
    public ApiMessage<DemoOneResponse> save(@RequestBody @Valid DemoOneRequest parameter) {
        return service.addDemoOne(parameter);
    }
    //=====================================================================================PUT========================================================================================
    @ApiOperation(value = "更新DemoOne数据", notes = "更新DemoOne数据~~~~~~~~~~~~~")
    @PutMapping("/")
    public ApiMessage<DemoOneResponse> update(@RequestBody @Valid DemoOneRequest parameter) {
        return service.update(parameter);
    }
大功告成!!!启动访问如下地址:
Swagger2地址:
http://${ip地址}{端口}/swagger-ui.html
swagger-bootstrap-ui地址:
http://${ip地址}{端口}/doc.html
赵小胖个人博客:https://zc.happyloves.cn:4443/wordpress/
SpringBoot2 整合 Swagger2文档 使用BootstrapUI页面的更多相关文章
- Spring Boot:整合Swagger文档
		
综合概述 spring-boot作为当前最为流行的Java web开发脚手架,越来越多的开发者选择用其来构建企业级的RESTFul API接口.这些接口不但会服务于传统的web端(b/s),也会服务于 ...
 - SpringBoot2 整合 Swagger2
		
SpringBoot2 整合 Swagger2 SpringBoot整合三板斧 第一步.引入pom <dependency> <groupId>com.spring4all&l ...
 - SpringBoot之Swagger2文档生成
		
SpringBoot之Swagger2文档生成 1.Swagger2介绍 编写和维护接口文档是每个程序员的职责,前面我们已经写好的接口现在需要提供一份文档,这样才能方便调用者使用.考虑到编写接口文档是 ...
 - Spring Boot项目使用Swagger2文档教程
		
[本文版权归微信公众号"代码艺术"(ID:onblog)所有,若是转载请务必保留本段原创声明,违者必究.若是文章有不足之处,欢迎关注微信公众号私信与我进行交流!] 前言 Sprin ...
 - js仿百度文库文档上传页面的分类选择器_第二版
		
仿百度文库文档上传页面的多级联动分类选择器第二版,支持在一个页面同一时候使用多个分类选择器. 此版本号把HTML,CSS,以及图片都封装到"category.js"中.解决因文件路 ...
 - swagger2文档使用
		
①.导入依赖 <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-sw ...
 - pywin32  pywin32  docx文档转html页面  word doc docx 提取文字 图片 html 结构
		
https://blog.csdn.net/X21214054/article/details/78873338# python docx文档转html页面 - 程序猿tx - 博客园 https:/ ...
 - flexpaper上传带中文名字的文档,在页面显示若出现404错误时,请在server.xml文件中进行编码utf-8
		
flexpaper上传带中文名字的文档,在页面显示若出现404错误时,请在server.xml文件中进行编码utf-8
 - 如何Spring Cloud Zuul作为网关的分布式系统中整合Swagger文档在同一个页面上
		
本文不涉及技术,只是单纯的一个小技巧. 阅读本文前,你需要对spring-cloud-zuul.spring-cloud-eureka.以及swagger的配置和使用有所了解. 如果你的系统也是用zu ...
 
随机推荐
- 关于C++命名空间namespace的理解与使用介绍
			
0X00 前言 所谓namespace,是指标识符的各种可见范围.C++标准程序库中的所有标识符都被定义于一个名为std的namespace中. 0x01 与C语言区别 <iostream> ...
 - 好看的UI框架
			
一.Web 1.semantic-ui: https://semantic-ui.com/elements/divider.html 二.H5 1.BUI: http://www.easybui.co ...
 - [JAVA]《JAVA开发手册》规约详解
			
[强制] 使用 Map 的方法 keySet()/values()/entrySet()返回集合对象时,不可以对其进行添加元素操作,否则会抛出 UnsupportedOperationExceptio ...
 - python基础内容扩展复习
			
目录 一.关于编辑器 二.解释型和编译型 三.数据类型 1 一切皆对象 2 深浅拷贝 3 可变类型和不可变类型 四.闭包函数 一.关于编辑器 python开发:pycharm(收费),vscode(免 ...
 - 对掌机游戏Pokemon的一部分系统的拆解流程图
			
整体系统拆解 POKEMON系统拆解 属性.技能.进化形态 属性提升系统 种族值说明: 所有Pokemon都拥有自己的种族的种族值,且固定(例如:小火龙:309, 皮卡丘: 320) 种族值是各项属性 ...
 - 转载一篇关于kafka零拷贝(zero-copy)通俗易懂的好文
			
原文地址 https://www.cnblogs.com/yizhou35/p/12026263.html 零拷贝就是一种避免CPU 将数据从一块存储拷贝到另外一块存储的技术. DMA技术是Direc ...
 - 关于Mint-UI中loadmore组件的兼容性问题
			
源代码 遇到的问题 写完了之后数据加载,渲染等等都是没有问题的,但是测试总是提上滑刷新不能用,因为是远程开发,测试提就得改,看代码看文档,看半天看不出来问题,想到了兼容性问题,发现也有人遇到这个坑.安 ...
 - oracle数据库备份还原命令
			
oracle数据库备份命令exp 用户名/密码@orcl file=d:\xxxxxx.dmp owner=用户名 oracle数据库还原命令sqlplus conn / as sysdba drop ...
 - vue : 无法加载文件 C:\Users\ui61895076\AppData\Roaming\npm\vue.ps1,因为在此系统上禁止运行脚本。有关详细信息,请参阅 https:/go.microsoft.com/fwlink/?LinkID=135170 中的 about_Execution_Policies。
			
说白了就是这个编辑器不能用罢了 执行以下代码 1.鼠标右击以管理员身份运行vscode; 2. 执行:get-ExecutionPolicy,显示Restricted,表示状态是禁止的; 3. 执行: ...
 - CentOS开机启动不了修复
			
1,如果启动时进度条,先修改为日志启动 启动后快速按任何键(Enter除外)进入如下界面 在按e进入 选择第二个选项卡 在按e进入将红色部分 rhgb quiet 删除,然后按Enter,在按b重启 ...