博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
怎样给Swagger换皮肤?
阅读量:6922 次
发布时间:2019-06-27

本文共 2675 字,大约阅读时间需要 8 分钟。

怎样给Swagger换皮肤?

上文我们讲到在Spring Boot中集成Swagger2的组件,那今天我们就来聊聊怎样给Swagger换个皮肤呢?环境搭建:使用Spring Boot依赖swagger-spring-boot-starter进行快速构建。具体swagger-spring-boot-starter可以参考: 。构建工具是Maven,开发工具是IDEA,JDK版本是1.8。

第一步:Maven快速构建Spring Boot的web项目

1543234878600

第二步:解压,IDEA导入项目

1543235052853

第三步:集成swagger-spring-boot-starter

pom中依赖:

com.spring4all
swagger-spring-boot-starter
1.8.0.RELEASE

1543235220552

添加@EnableSwagger2Doc添加允许启用swagger注解,默认情况下就能产生所有当前Spring MVC加载的请求映射文档。

第四步:配置swagger

# 在application.properties进行配置swagger.title=码歌学院APIswagger.description=码歌学院相关接口API文档swagger.version=1.1swagger.base-package=com.mage

其他具体配置请参考GitHub,。注意在IDEA中配置文件存在中文,那么需要将其配置文件的编码设置成utf-8。具体设置:File -> Settings -> Editor -> File Encodings将Properties Files (*.properties)下的Default encoding for properties files设置为UTF-8,将Transparent native-to-ascii conversion前的勾选上。

第五步:编写TestController

package com.mage.swagger02.controller;import com.mage.swagger02.model.Test;import io.swagger.annotations.Api;import io.swagger.annotations.ApiImplicitParam;import io.swagger.annotations.ApiOperation;import io.swagger.annotations.ApiParam;import org.springframework.web.bind.annotation.*;@RestController@RequestMapping("test")@Api(tags = "测试API接口")public class TestController {    @GetMapping("")    @ApiOperation(value="获取列表数据", notes="获取列表下测试数据")    public String list() {        return "查询列表数据!";    }    @GetMapping("{id}")    @ApiOperation(value="获取ID数据", notes="根据ID获取某条测试数据")    @ApiImplicitParam(name = "id", value = "主键id", paramType = "path", required = true)    public String find(@PathVariable Integer id) {        return String.format("根据主键查询数据: %d", id);    }    @PostMapping("")    @ApiOperation(value="新增数据")    @ApiParam(name = "test", value = "添加的测试模型实体")    public String add(@RequestBody Test test) {        return "插入数据!";    }    @PutMapping("{id}")    @ApiOperation(value="更新数据", notes="根据ID更新测试数据")    @ApiImplicitParam(name = "id", value = "主键id", paramType = "path", required = true)    public String update(@PathVariable Integer id, @ApiParam(name = "test", value = "更新的测试模型实体") @RequestBody Test test) {        return String.format("根据主键更新一条记录: %d", id);    }    @DeleteMapping("{id}")    @ApiOperation(value="删除数据", notes="根据ID删除测试数据")    @ApiImplicitParam(name = "id", value = "主键id", paramType = "path", required = true)    public String delete(@PathVariable Integer id) {        return String.format("根据主键删除记录: %d", id);    }}

第六步:启动执行,浏览器输入:8080/swagger-ui.html

1543236194060

第七步:换皮肤

大家如果觉得swagger这种皮肤不好看,那么可以更换,只需要在pom中引入一下jar包:

com.github.caspar-chen
swagger-ui-layer
1.1.2

然后浏览器输入::8080/docs.html

1543236493872

好了换肤完成,源码下载:

转载地址:http://ajecl.baihongyu.com/

你可能感兴趣的文章
个人笔记之json实现模糊查询
查看>>
把文本以图片的形式保存
查看>>
事务(JDBC、Spring)
查看>>
黑马程序员--SQL基础复习(三)
查看>>
Lambda表达式
查看>>
C/S模型之Socket通讯 服务侦听端
查看>>
查看一个进程打开的文件
查看>>
51nod 1240 莫比乌斯函数【数论+莫比乌斯函数】
查看>>
Firebug调试经验与技巧
查看>>
soundtouch change pitch matlab implementation
查看>>
对一个新知识领域的学习路径
查看>>
ios 获取当前时间
查看>>
算法之求质数(Java语言)
查看>>
Python之旅.第三章.函数
查看>>
WebService学习总结(一)
查看>>
Node.js安装及环境配置之Windows篇
查看>>
初学css为博客园文章某个超链接添加 icon
查看>>
LAMMPS Data Format
查看>>
标准检查点
查看>>
linux常用命令
查看>>