一,什么情况下需要展示分页和分栏的数据的文档?

分页时,页面上展示的是同一类型的列表的数据,如图:

分栏时,每行都是一个列表,而且展示的数据类型也可能不同

这也是两种常用的数据返回形式

说明:刘宏缔的架构森林是一个专注架构的博客,地址:https://www.cnblogs.com/architectforest

对应的源码可以访问这里获取: https://github.com/liuhongdi/

说明:作者:刘宏缔 邮箱: 371125307@qq.com

二,演示项目的相关信息

1,项目地址

https://github.com/liuhongdi/swagger3pagination

2,项目功能说明:

演示了分页、分栏时数据文档的展示

3,项目结构:如图:

三,配置文件说明:

1,pom.xml

        <!-- swagger3 begin -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>

四,java代码说明

1,Swagger3Config.java

@EnableOpenApi
@Configuration
public class Swagger3Config implements WebMvcConfigurer { @Bean
public Docket createRestApi() {
//返回文档摘要信息
return new Docket(DocumentationType.OAS_30)
.apiInfo(apiInfo())
.select()
//.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
.apis(RequestHandlerSelectors.withMethodAnnotation(Operation.class))
.paths(PathSelectors.any())
.build()
.globalRequestParameters(getGlobalRequestParameters())
.globalResponses(HttpMethod.GET, getGlobalResonseMessage())
.globalResponses(HttpMethod.POST, getGlobalResonseMessage());
} //生成接口信息,包括标题、联系人等
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("Swagger3接口文档")
.description("如有疑问,请联系开发工程师老刘。")
.contact(new Contact("刘宏缔", "https://www.cnblogs.com/architectforest/", "371125307@qq.com"))
.version("1.0")
.build();
} //生成全局通用参数
private List<RequestParameter> getGlobalRequestParameters() {
List<RequestParameter> parameters = new ArrayList<>();
parameters.add(new RequestParameterBuilder()
.name("appid")
.description("平台id")
.required(true)
.in(ParameterType.QUERY)
.query(q -> q.model(m -> m.scalarModel(ScalarType.STRING)))
.required(false)
.build());
parameters.add(new RequestParameterBuilder()
.name("udid")
.description("设备的唯一id")
.required(true)
.in(ParameterType.QUERY)
.query(q -> q.model(m -> m.scalarModel(ScalarType.STRING)))
.required(false)
.build());
parameters.add(new RequestParameterBuilder()
.name("version")
.description("客户端的版本号")
.required(true)
.in(ParameterType.QUERY)
.query(q -> q.model(m -> m.scalarModel(ScalarType.STRING)))
.required(false)
.build());
return parameters;
} //生成通用响应信息
private List<Response> getGlobalResonseMessage() {
List<Response> responseList = new ArrayList<>();
responseList.add(new ResponseBuilder().code("404").description("找不到资源").build());
return responseList;
}
}

swagger的通用配置

2,HomeController.java

@Api(tags = "首页信息管理")
@Controller
@RequestMapping("/home")
public class HomeController { @Operation(summary = "首页详情")
@GetMapping("/home")
@ResponseBody
public RestResult<PageResult<RowResult<Goods,Ad>>> home() {
//整体的返回
RestResult res = new RestResult(); //一行用到的list
List<Goods> rowList = new ArrayList();
Goods goodsone = new Goods();
goodsone.setGoodsId(3L);
goodsone.setGoodsName("电子书");
goodsone.setSubject("学python,学ai");
goodsone.setPrice(new BigDecimal(60));
goodsone.setStock(10);
rowList.add(goodsone); Goods goodstwo = new Goods();
goodstwo.setGoodsId(4L);
goodstwo.setGoodsName("蓝牙音箱");
goodstwo.setSubject("便携式音质优异");
goodstwo.setPrice(new BigDecimal(1200.00));
goodstwo.setStock(30);
rowList.add(goodstwo); //一行用到的result
RowResult oneRow = new RowResult();
oneRow.setListGoods(rowList);
oneRow.setRowType("goods"); //一行用到的list
List<Ad> rowList2 = new ArrayList();
Ad adone = new Ad();
adone.setTitle("保温杯");
adone.setImgurl("http://a.com/1.jpg");
rowList2.add(adone); Ad adtwo = new Ad();
adtwo.setTitle("茶具");
adtwo.setImgurl("http://a.com/2.jpg");
rowList2.add(adtwo); //一行用到的result
RowResult twoRow = new RowResult();
twoRow.setListAd(rowList2);
twoRow.setRowType("ad"); //一页用到的list
List<RowResult> pageList = new ArrayList();
pageList.add(oneRow);
pageList.add(twoRow); //一页用到的result
PageResult<List<RowResult>> pres= new PageResult();
pres.setList(pageList); //分页的信息
PaginationResult pares = new PaginationResult();
pares.setTotal(10);
pares.setCurrentPage(3);
pares.setPerPageCount(10);
pres.setPagination(pares); return res.success(pres);
}
}

显示分栏的分页效果

3,GoodsController.java

@Api(tags = "商品信息管理接口")
@RestController
@RequestMapping("/goods")
public class GoodsController { @Operation(summary = "分页商品列表,得到一页上多件商品的信息")
@GetMapping("/list")
public RestResult<PageResult<Goods>> list() {
//整体返回的result
RestResult res = new RestResult();
//一页中的list
List<Goods> resList = new ArrayList(); Goods goodsone = new Goods();
goodsone.setGoodsId(3L);
goodsone.setGoodsName("电子书");
goodsone.setSubject("学python,学ai");
goodsone.setPrice(new BigDecimal(60));
goodsone.setStock(10);
resList.add(goodsone); Goods goodstwo = new Goods();
goodstwo.setGoodsId(4L);
goodstwo.setGoodsName("蓝牙音箱");
goodstwo.setSubject("便携式音质优异");
goodstwo.setPrice(new BigDecimal(1200.00));
goodstwo.setStock(30);
resList.add(goodstwo); //一页的result
PageResult<List<Goods>> pres= new PageResult();
pres.setList(resList); //一页的分页信息
PaginationResult pares = new PaginationResult();
pares.setTotal(10);
pares.setCurrentPage(3);
pares.setPerPageCount(10); pres.setPagination(pares); return res.success(pres);
} @Operation(summary = "提交订单")
@PostMapping("/order")
@ApiImplicitParams({
@ApiImplicitParam(name="userid",value="用户id",dataTypeClass = Long.class, paramType = "query",example="12345"),
@ApiImplicitParam(name="goodsid",value="商品id",dataTypeClass = Integer.class, paramType = "query",example="12345"),
@ApiImplicitParam(name="mobile",value="手机号",dataTypeClass = String.class, paramType = "query",example="13866668888"),
@ApiImplicitParam(name="comment",value="发货备注",dataTypeClass = String.class, paramType = "query",example="请在情人节当天送到")
})
public RestResult<String> order(@ApiIgnore @RequestParam Map<String,String> params) {
System.out.println(params);
RestResult res = new RestResult();
return res.success("success");
}
}

分页,页面上是元素的列表

4,RestResult.java

@ApiModel("api通用返回数据")
public class RestResult<T> { //uuid,用作唯一标识符,供序列化和反序列化时检测是否一致
private static final long serialVersionUID = 7498483649536881777L;
//标识代码,0表示成功,非0表示出错
@ApiModelProperty("标识代码,0表示成功,非0表示出错")
private Integer code; //提示信息,通常供报错时使用
@ApiModelProperty("提示信息,供报错时使用")
private String msg; //正常返回时返回的数据
@ApiModelProperty("返回的数据")
private T data; //constructor
public RestResult() {
} //constructor
public RestResult(Integer status, String msg, T data) {
this.code = status;
this.msg = msg;
this.data = data;
} //返回成功数据
public RestResult success(T data) {
return new RestResult(ResponseCode.SUCCESS.getCode(), ResponseCode.SUCCESS.getMsg(), data);
} public static RestResult success(Integer code,String msg) {
return new RestResult(code, msg, null);
} //返回出错数据
public static RestResult error(ResponseCode code) {
return new RestResult(code.getCode(), code.getMsg(), null);
} public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
} public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
} public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
} }

整体返回的result

5,RowResult.java

@ApiModel("每栏的信息")
public class RowResult<T,K> {
@ApiModelProperty("当前栏的类型")
private String rowType; @ApiModelProperty("当前栏的标题")
private String rowTitle; @ApiModelProperty("内容列表:商品")
private List<T> listGoods; @ApiModelProperty("内容列表:广告")
private List<K> listAd; public String getRowTitle() {
return this.rowTitle;
}
public void setRowTitle(String rowTitle) {
this.rowTitle = rowTitle;
} public String getRowType() {
return this.rowType;
}
public void setRowType(String rowType) {
this.rowType = rowType;
} public void setListGoods(List<T> listGoods) {
this.listGoods = listGoods;
}
public List<T> getListGoods() {
return this.listGoods;
} public void setListAd(List<K> listAd) {
this.listAd = listAd;
}
public List<K> getListAd() {
return this.listAd;
}
}

每行的result

6,PaginationResult.java

@ApiModel("分页信息")
public class PaginationResult { @ApiModelProperty("总页数")
private int total; @ApiModelProperty("当前第几页")
private int currentPage; @ApiModelProperty("每页的元素的数量")
private int perPageCount; public int getTotal() {
return this.total;
}
public void setTotal(int total) {
this.total = total;
} public int getCurrentPage() {
return this.currentPage;
}
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
} public int getPerPageCount() {
return this.perPageCount;
}
public void setPerPageCount(int perPageCount) {
this.perPageCount = perPageCount;
}
}

分页信息的result

7,PageResult.java

@ApiModel("分页返回数据")
public class PageResult<T> { @ApiModelProperty("列表,分页中的数据")
private List<T> list; @ApiModelProperty("页数信息")
private PaginationResult pagination; public PageResult() {
} public void setList(List list) {
this.list = list;
}
public List<T> getList() {
return this.list;
} public void setPagination(PaginationResult pagination) {
this.pagination = pagination;
}
public PaginationResult getPagination() {
return this.pagination;
}
}

分页的result

8,Goods.java,Ad.java,ResponseCode.java

等文件可以访问github.com

五,测试效果

1,访问分页效果:

http://127.0.0.1:8080/goods/list

返回:

查看swagger:

2,查看分栏并分页的效果:访问:

http://127.0.0.1:8080/home/home

返回:

查看swagger:

六,查看spring boot的版本 :

  .   ____          _            __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.3.3.RELEASE)

spring boot:swagger3文档展示分页和分栏的列表数据(swagger 3.0.0 / spring boot 2.3.3)的更多相关文章

  1. (转)WEB页面导出为Word文档后分页&横向打印的方法

    <html>    <HEAD>        <title>WEB页面导出为Word文档后分页&横向打印的方法 </title>    < ...

  2. 集成 Spring Doc 接口文档和 knife4j-SpringBoot 2.7.2 实战基础

    优雅哥 SpringBoot 2.7.2 实战基础 - 04 -集成 Spring Doc 接口文档和 knife4j 前面已经集成 MyBatis Plus.Druid 数据源,开发了 5 个接口. ...

  3. [Django REST framework - 自动生成接口文档、分页]

    [Django REST framework - 自动生成接口文档.分页] 自动生成接口文档 # 后端人员写好接口,编写接口文档,给前端人员看,前端人员依照接口文档开发 # 公司里主流 -后端,使用w ...

  4. 倒排索引 获取指定单词的文档集合 使用hash去重单词term 提高数据压缩率的方法

    倒排索引源于实际应用中需要根据属性的值来查找记录.这种索引表中的每一项都包括一个属性值和具有该属性值的各记录的地址.由于不是由记录来确定属性值,而是由属性值来确定记录的位置,因而称为倒排索引(inve ...

  5. Swagger2 生成 Spring Boot API 文档

    Swagger 是一个规范和完整的框架,用于生成.描述.调用和可视化 RESTful 风格的 Web 服务.本文主要介绍了在 Spring Boot 添加 Swagger 支持, 生成可自动维护的 A ...

  6. spring boot☞Swagger2文档构建及单元测试

    首先,回顾并详细说明一下在快速入门中使用的@Controller.@RestController.@RequestMapping注解.如果您对Spring MVC不熟悉并且还没有尝试过快速入门案例,建 ...

  7. Spring MVC 指导文档解读(一)

    22.1 指导文档章节 In the Web MVC framework, each DispatcherServlet has its own WebApplicationContext, whic ...

  8. 0x02 Spring Cloud 学习文档

    每个Spring项目都有自己的; 它详细解释了如何使用项目功能以及使用它们可以实现的功能. Spring Cloud 版本 参考文档 API文档 Finchley SR2 CURRENT GA Ref ...

  9. Spring学习----- Spring配置文件xml文档的schema约束

    1.配置文件示例. <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="htt ...

随机推荐

  1. 通达OA任意用户登录漏洞复现

    前言 今年hw挺火爆的,第一天上来就放王炸,直接搞得hw暂停 昨天晚上无聊,复现了一下通达oa的洞,也有现成的exp可以使用,比较简单 0x00 漏洞概述 通达OA是一套国内常用的办公系统,此次发现的 ...

  2. Idea没安装几款好用的插件,怎么风骚的写代码???

    ​ 工欲善其事,必先利其器,好的工具可以提升我们的开发效率,越来越多的Java程序员从Eclipse转到了Jetbrains家的Idea.今天给大家介绍的是我常用的十几款Idea必装的插件. ​ Ti ...

  3. ⏰ Moment.js 宣布停止开发,现在该用什么?

    本文整理自 Monent.js 官方英文公告 https://momentjs.com/docs/#/-project-status/ Moment.js 宣布停止开发,进入维护状态. 这是一个大而全 ...

  4. Spring使用@Async实现异步

    使用场景 在实际项目中,一个接口如果需要处理很多数据,如果是同步执行,通过网络请求接口可能会出现请求超时.这时候就需要使用异步执行处理了. 使用经验 代码 异步服务类 @Service // Spri ...

  5. 提权 EXP

    windows: 漏洞列表 #Security Bulletin #KB #Description #Operating System CVE-2017-0213 [Windows COM Eleva ...

  6. JAVA并发笔记

    重入锁的特性, 避免死锁, 如果有锁的话, 不用重新加锁, 直接增加锁的次数.. Synchronize, ReentrantLock都是重入锁. 读写锁, ReentrantReadWriteLoc ...

  7. 双向最大匹配算法——基于词典规则的中文分词(Java实现)

    目录 一.中文分词理论描述 二.算法描述 1.正向最大匹配算法 2.反向最大匹配算法 3.双剑合璧 三.案例描述 四.JAVA实现完整代码 五.组装UI 六.总结 前言 这篇将使用Java实现基于规则 ...

  8. java版集成Allure报告--注释使用说明

    testNG集成Allure报告--注释使用说明 前置条件 首先需要下载allure的zip包解压,然后配置环境变量即可(win).allure的GitHub下载地址: 然后执行testn.xml或者 ...

  9. spark-2-RDD

    RDD提供了一个抽象的数据架构,我们不必担心底层数据的分布式特性,只需将具体的应用逻辑表达为一系列转换处理,不同RDD之间的转换操作形成依赖关系,可以实现管道化,从而避免了中间结果的存储,大大降低了数 ...

  10. Docker操作命令——查看、停止、删除容器

    列出所有容器 ID docker ps -aq 停止所有容器 docker stop $(docker ps -aq) 停止单个容器 docker stop 要停止的容器名 删除所有容器 docker ...