怎样给Swagger换皮肤?
上文我们讲到在Spring Boot中集成Swagger2的组件,那今天我们就来聊聊怎样给Swagger换个皮肤呢?环境搭建:使用Spring Boot依赖swagger-spring-boot-starter进行快速构建。具体swagger-spring-boot-starter可以参考:https://github.com/SpringForA... 。构建工具是Maven,开发工具是IDEA,JDK版本是1.8。
第一步:Maven快速构建Spring Boot的web项目
第二步:解压,IDEA导入项目
第三步:集成swagger-spring-boot-starter
pom中依赖:
<dependency>
<groupId>com.spring4all</groupId>
<artifactId>swagger-spring-boot-starter</artifactId>
<version>1.8.0.RELEASE</version>
</dependency>
添加@EnableSwagger2Doc添加允许启用swagger注解,默认情况下就能产生所有当前Spring MVC加载的请求映射文档。
第四步:配置swagger
# 在application.properties进行配置
swagger.title=码歌学院API
swagger.description=码歌学院相关接口API文档
swagger.version=1.1
swagger.base-package=com.mage
其他具体配置请参考GitHub,https://github.com/SpringForA...。注意在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);
}
}
第六步:启动执行,浏览器输入http://localhost:8080/swagger-ui.html
第七步:换皮肤
大家如果觉得swagger这种皮肤不好看,那么可以更换,只需要在pom中引入一下jar包:
<dependency>
<groupId>com.github.caspar-chen</groupId>
<artifactId>swagger-ui-layer</artifactId>
<version>1.1.2</version>
</dependency>
然后浏览器输入:http://localhost:8080/docs.html
好了换肤完成,源码下载:https://github.com/magekang/s...
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。