SpringBoot框架如何整合SwaggerUI
发表于:2024-10-16 作者:千家信息网编辑
千家信息网最后更新 2024年10月16日,这篇文章主要介绍了SpringBoot框架如何整合SwaggerUI的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇SpringBoot框架如何整合SwaggerUI文章都
千家信息网最后更新 2024年10月16日SpringBoot框架如何整合SwaggerUI
这篇文章主要介绍了SpringBoot框架如何整合SwaggerUI的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇SpringBoot框架如何整合SwaggerUI文章都会有所收获,下面我们一起来看看吧。
整合swagger进行模块测试
注意事项:为方便SpringBoot更好的整合Swagger,需要专门放置在一个模块中(maven子工程)
创建公共模块,整合swagger,为了所有模块进行使用
common/pom.xml,导入相关的依赖
org.springframework.boot spring-boot-starter-web provided com.baomidou mybatis-plus-boot-starter org.projectlombok lombok io.springfox springfox-swagger2 springfox-swagger-ui spring-boot-starter-data-redis
在公共模块下在创建一个模块,如service_base
在该模块下创建配置类(需要遵循SpringBoot规范,该代码固定)
package com.xsha.servicebase;import com.google.common.base.Predicates;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import springfox.documentation.builders.ApiInfoBuilder;import springfox.documentation.builders.PathSelectors;import springfox.documentation.service.ApiInfo;import springfox.documentation.service.Contact;import springfox.documentation.spi.DocumentationType;import springfox.documentation.spring.web.plugins.Docket;import springfox.documentation.swagger2.annotations.EnableSwagger2;@Configuration@EnableSwagger2public class SwaggerConfig { @Bean public Docket webApiConfig(){ return new Docket(DocumentationType.SWAGGER_2) .groupName("webApi") .apiInfo(webApiInfo()) .select() .paths(Predicates.not(PathSelectors.regex("/admin/.*"))) .paths(Predicates.not(PathSelectors.regex("/error.*"))) .build(); } private ApiInfo webApiInfo(){ return new ApiInfoBuilder() .title("网站标题") .description("接口文档的描述信息") .version("1.0") .contact(new Contact("java", "http://www.baidu.com", "1234567890@qq.com"))}
使用方式
在其他模块(最好是最外层的)的pom.xml引入上面的模块即可
com.xsha service_base 0.0.1-SNAPSHOT
在该模块的启动类上添加ComponentScan注解,指定需要扫描的包。例如:@ComponentScan(basePackages={"com.xsha"})
然后启动,访问地址:http://127.0.0.1:8001/swagger-ui.html
统一返回结果的格式(自定义结果)
在公共模块下在创建一个模块,如common-utils
创建一个专门管理状态码的接口
public interface ResultCode { //定义两个状态码 public static int SUCCESS = 20000; public static int ERROR = 40000;}
定义返回格式(较为固定)
package com.xsha.commonutils;import io.swagger.annotations.ApiModelProperty;import lombok.Data;import java.util.HashMap;import java.util.Map;// 统一返回结果类@Datapublic class R { @ApiModelProperty(value = "是否成功") private Boolean success; @ApiModelProperty(value = "返回码") private Integer code; @ApiModelProperty(value = "返回消息") private String message; @ApiModelProperty(value = "返回数据") private Mapdata = new HashMap (); // 把构造方法定为私有 private R() {} // 成功静态方法 public static R ok() { R r = new R(); r.setSuccess(true); r.setCode(ResultCode.SUCCESS); r.setMessage("成功"); return r; } // 失败静态方法 public static R error() { r.setSuccess(false); r.setCode(ResultCode.ERROR); r.setMessage("失败"); public R success(Boolean success){ this.setSuccess(success); return this; public R message(String message){ this.setMessage(message); public R code(Integer code){ this.setCode(code); public R data(String key, Object value){ this.data.put(key, value); public R data(Map map){ this.setData(map);}
使用方式
在其他模块(最好是最外层的)的pom.xml引入上面的模块即可
com.xsha common_utils 0.0.1-SNAPSHOT
每次返回的结果的类型必须是自定义的"返回格式"类类型
// please use rest style// 1.select all teachers data@ApiOperation(value = "所有数据列表")@GetMapping("findAll")public R findAllTeachers() { Listteachers = teacherService.list(null); return R.ok().data("results", teachers);}// request path mast have variable id// 2.logically delete teacher@ApiOperation(value = "逻辑删除数据")@DeleteMapping("{id}")public R logicDeleteTeacher(@ApiParam(name="id", value="讲师ID", required = true) @PathVariable String id) { boolean flag = teacherService.removeById(id); return flag? R.ok(): R.error();}
最后在swagger中测试即可
关于"SpringBoot框架如何整合SwaggerUI"这篇文章的内容就介绍到这里,感谢各位的阅读!相信大家对"SpringBoot框架如何整合SwaggerUI"知识都有一定的了解,大家如果还想学习更多知识,欢迎关注行业资讯频道。
模块
整合
框架
结果
成功
数据
方法
格式
知识
内容
外层
接口
方式
最好
状态
篇文章
类型
静态
面的
测试
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
设置数据库为单一用户
软件开发程序员学习课程
潮州软件开发怎么样
怀旧服服务器最新调查
君联资本 网络安全
数据库计算九九乘法表
网络安全隐私怎么做
网络安全专业博士留学
音乐数据库正在优化是什么意思
风河软件开发公司怎么样
国际服怎么装服务器
徐汇区营销软件开发大概费用
创建数据库实验总结和分析
河北麻将软件开发报价
我的世界服务器怎么挂机升级
gmail服务器邮件
广州天拓网络技术电话
126 imap服务器
生活咖软件开发商
软件开发中的b端和c端
华为 it软件开发
服务器管理员恢复到什么时候
杭州直销软件开发价位
pymsql3 创建数据库
数据库基础代码
数据库set 怎么用
hl编程软件开发者
蛋白组数据库
软件开发属于什么企业性质
uno软件开发