搭建SpringBoot + Gradle + MyBatisPlus3.x + Swagger整合在线和离线API
1、简要说明
2、核心功能
在线文档
:
http://doc.xiaominfo.com/
该UI增强包主要包括两大核心功能:
文档说明
和
在线调试
3、UI增强
同时,swagger-bootstrap-ui在满足以上功能的同时,还提供了文档的增强功能,这些功能是官方swagger-ui所没有的,每一个增强的功能都是贴合实际,考虑到开发者的实际开发需要,是比不可少的功能,主要包括:
个性化配置 :通过个性化ui配置项,可自定义UI的相关显示信息 离线文档 :根据标准规范,生成的在线markdown离线文档,开发者可以进行拷贝生成markdown接口文档,通过其他第三方markdown转换工具转换成html或pdf,这样也可以放弃swagger2markdown组件 接口排序 :自1.8.5后,ui支持了接口排序功能,例如一个注册功能主要包含了多个步骤,可以根据swagger-bootstrap-ui提供的接口排序规则实现接口的排序,step化接口操作,方便其他开发者进行接口对接4、UI特点
5、 Maven与Gradle配置中引入Jar包
springfox-swagger
的增强UI包,所以基础功能依然依赖Swagger,
springfox-swagge
r的jar包必须引入
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
Gradle配置
compile "io.springfox:springfox-swagger2:${swagger2_version}"
然后引入SwaggerBootstrapUi的jar包
maven 配置
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>swagger-bootstrap-ui</artifactId>
<version>${lastVersion}</version>
</dependency>
Gradle配置
compile "com.github.xiaoymin:swagger-bootstrap-ui:${swagger_bsi_version}"
6、代码实现
EnableSwagger2 启动Swagger配置
EnableSwaggerBootstrapUI 启动Swagger的UI配置
@Configuration
@EnableSwagger2
@EnableSwaggerBootstrapUI
public class SwaggerConfiguration {
private final String BASE_PACKAGE = "com.flong.springboot";
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
//此组名可以通过数据库查出来加载里面,通过模块路径进行扫描不同微服务名的包路径。
.groupName("用户管理")
.select()
.apis(RequestHandlerSelectors.basePackage(BASE_PACKAGE))
.paths(PathSelectors.any())
.build();
* api信息
* @return
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("Swagger RESTful APIs")
.description("Swagger RESTful APIs")
.termsOfServiceUrl("http://github.com/jilongliang/")
.contact("liangjl")
.version("1.0")
.build();
@ApiModel("用户实体")
public class User extends Model<User> implements Serializable {
@TableId(type = IdType.ID_WORKER)
@ApiModelProperty(value = "用户Id")
private Long userId;
* 用户名
@ApiModelProperty(value = "用户名")
private String userName;
@ApiModelProperty(value = "密码")
private String passWord;
* 逻辑删除(0-未删除,1-已删除)
@TableLogic
@ApiModelProperty(value = "逻辑删除(0-未删除,1-已删除)")
private String delFlag;
* 创建时间,允许为空,让数据库自动生成即可
@ApiModelProperty(value = "创建时间")
private Date createTime;
@ApiOperation(value = "添加用户",notes = "添加用户")
public void add() {
userMapper.insert(User.builder().userName("周伯通").passWord("123456").build());
* @param user
@PutMapping("/updateById")
@ApiOperation(value = "修改用户",notes = "通过用户Id进行修改用户信息")
public void updateById(@RequestBody User user) {
userMapper.updateById(user);
* 删除通过多个主键Id进行删除
* @param ids
@DeleteMapping("/deleteByIds")
@ApiOperation(value = "修改删除",notes = "支持多个用户Id进行删除用户")
public void deleteByIds(@RequestBody List<String> ids) {
userMapper.deleteBatchIds(ids);
* 通过指定Id进行查询
* @param userId
@GetMapping("/getOne/{userId}")
@ApiOperation(value = "查询一个用户信息",notes = "通过指定Id进行查询")
public User getOne(@PathVariable("userId") Long userId) {
User user = userMapper.selectById(userId);
System.out.println(JSON.toJSON(user));
return user ;
* 用户分页,参数有多个使用下标索引进行处理.如果有两个参数(如用户名和地址):conditionList[0].fieldName=userName、 conditionList[0].fieldName=address
* 未转码请求分页地址: http://localhost:7011/user/page?current=1&size=20&conditionList[0].fieldName=userName&conditionList[0].operation=LIKE&conditionList[0].value=周
* 已转码请求分页地址: http://localhost:7011/user/page?current=1&size=20&conditionList[0].fieldName=userName&conditionList[0].operation=LIKE&conditionList[0].value=%E5%91%A8
* @param page
* @param conditions 条件
* @return
@GetMapping("/page")
@ApiOperation(value = "用户分页查询",notes = "通过多个条件进行查询用户信息")
public IPage<User> page(Page page, Conditions conditions) {
QueryWrapper<User> build = BuildConditionWrapper.build(conditions.getConditionList(), User.class);
build.lambda().orderByDesc(User::getCreateTime);
return userService.page(page, build);
7、访问路径
http://{port}/doc.html
http://localhost:7011/doc.html
8、代码工程结构与运行结构
9、工程源代码
工程代码在 swagger 分支 https://github.com/jilongliang/springboot/tree/swagger 同时把代码合并到分支.