Springboot集成Swagger2显示字段属性说明
新建spring boot工程
添加依赖
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
新建swagger配置
import org.springframework.beans.factory.annotation.Value;
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.builders.RequestHandlerSelectors;
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;
/**
* @author felix
* @ 日期 2019-05-27 12:03
*/
@Configuration
@EnableSwagger2
public class SwaggerConfiguration {
@Value("${swagger.enabled}")
private boolean enable;
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.enable(enable)
.select()
.apis(RequestHandlerSelectors.basePackage("com.fecred.villagedoctor.controller"))
.paths(PathSelectors.any())
.build();
}
@Bean
public Docket healthApi() {
return new Docket(DocumentationType.SWAGGER_2)
.groupName("分组名称")
.apiInfo(apiInfo())
.enable(enable)
.select()
//分组的controller层
.apis(RequestHandlerSelectors.basePackage("com.xxx.controller"))
.paths(PathSelectors.regex(".*/health/.*"))
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("标题")
.description("一般描述信息")
.termsOfServiceUrl("http://localhost:8999/")
.contact(new Contact("联系人", "邮箱", "邮箱"))
.version("1.0")
.build();
}
}
新建前后端响应工具类
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
/**
* @author ie
* @ 日期 2019-10-25 14:24
*/
@Data
public class Result<T> {
@ApiModelProperty(value = "状态值")
private int code;
@ApiModelProperty(value = "提示信息")
private String message;
@ApiModelProperty(value = "结果")
private T payload;
private Result<T> code(int code) {
this.code = code;
return this;
}
private Result<T> message(String message) {
this.message = message;
return this;
}
private Result<T> payload(T payload) {
this.payload = payload;
return this;
}
public static <T> Result<T> ok() {
return new Result<T>().code(ResultCode.SUCCESS.getCode()).message(ResultCode.SUCCESS.getMessage()).payload(null);
}
public static <T> Result<T> ok(T payLoad) {
return new Result<T>().code(ResultCode.SUCCESS.getCode()).message(ResultCode.SUCCESS.getMessage()).payload(payLoad);
}
public static <T> Result<T> fail() {
return new Result<T>().code(ResultCode.FAIL.getCode()).message(ResultCode.FAIL.getMessage()).payload(null);
}
public static <T> Result<T> result(int code, String message, T payload) {
return new Result<T>().code(code).message(message).payload(payload);
}
}
状态码
@Getter
public enum ResultCode {
//成功
SUCCESS(20000, "成功"),
//失败
FAIL(40000, "失败"),
//未认证(签名错误)
UNAUTHORIZED(40001, "未认证(签名错误)"),
//接口不存在
NOT_FOUND(40004, "接口不存在"),
//服务器内部错误
INTERNAL_SERVER_ERROR(50000, "服务器内部错误"),
//TOKEN已过期
TOKEN_INVAILD(10001, "TOKEN已过期"),
//TOKEN无效
TOKEN_NOTFOUND(10002, "TOKEN无效");
private final int code;
private final String message;
ResultCode(int code, String message) {
this.code = code;
this.message = message;
}
}
Controller 层使用
@GetMapping(value = "/detail")
@ApiOperation(value = "根据id查询用户")
public Result<User> detail(@RequestParam("userId") Long userId) {
log.info("根据userId查询用户信息【{}】", userId);
User user = userService.findByUserId(userId);
log.info("用户信息【{}】", user.toString());
return Result.ok(user);
}
PageInfo 封装
用于 PageHelper显示属性值
import com.github.pagehelper.Page;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.util.Collection;
import java.util.List;
/**
* @author felix
* @ 日期 2019-06-01 15:57
*/
@Data
public class MyPageInfo<T> {
@ApiModelProperty(value = "当前页")
private int pageNum;
@ApiModelProperty(value = "每页的数量")
private int pageSize;
@ApiModelProperty(value = "当前页的数量")
private int size;
/**
* 由于startRow和endRow不常用,这里说个具体的用法
* 可以在页面中"显示startRow到endRow 共size条数据"
*/
@ApiModelProperty(value = "当前页面第一个元素在数据库中的行号")
private int startRow;
@ApiModelProperty(value = "当前页面最后一个元素在数据库中的行号")
private int endRow;
@ApiModelProperty(value = "总页数")
private int pages;
@ApiModelProperty(value = "前一页")
private int prePage;
@ApiModelProperty(value = "下一页")
private int nextPage;
@ApiModelProperty(value = "是否为第一页")
private boolean firstPage = false;
@ApiModelProperty(value = "是否为最后一页")
private boolean lastPage = false;
@ApiModelProperty(value = "是否有前一页")
private boolean hasPreviousPage = false;
@ApiModelProperty(value = "是否有下一页")
private boolean hasNextPage = false;
@ApiModelProperty(value = "导航页码数")
private int navigatePages;
@ApiModelProperty(value = "所有导航页号")
private int[] navigatepageNums;
@ApiModelProperty(value = "导航条上的第一页")
private int navigateFirstPage;
@ApiModelProperty(value = "导航条上的最后一页")
private int navigateLastPage;
@ApiModelProperty(value = "总页数")
private long total;
@ApiModelProperty(value = "结果集")
private List<T> list;
public MyPageInfo() {
this.firstPage = false;
this.lastPage = false;
this.hasPreviousPage = false;
this.hasNextPage = false;
}
public MyPageInfo(List<T> list) {
this(list, 8);
this.list = list;
if (list instanceof Page) {
this.total = ((Page) list).getTotal();
} else {
this.total = (long) list.size();
}
}
public MyPageInfo(List<T> list, int navigatePages) {
this.firstPage = false;
this.lastPage = false;
this.hasPreviousPage = false;
this.hasNextPage = false;
if (list instanceof Page) {
Page page = (Page) list;
this.pageNum = page.getPageNum();
this.pageSize = page.getPageSize();
this.pages = page.getPages();
this.size = page.size();
if (this.size == 0) {
this.startRow = 0;
this.endRow = 0;
} else {
this.startRow = page.getStartRow() + 1;
this.endRow = this.startRow - 1 + this.size;
}
} else if (list instanceof Collection) {
this.pageNum = 1;
this.pageSize = list.size();
this.pages = this.pageSize > 0 ? 1 : 0;
this.size = list.size();
this.startRow = 0;
this.endRow = list.size() > 0 ? list.size() - 1 : 0;
}
if (list instanceof Collection) {
this.navigatePages = navigatePages;
this.calcNavigatepageNums();
this.calcPage();
this.judgePageBoudary();
}
}
private void calcNavigatepageNums() {
//当总页数小于或等于导航页码数时
if (pages <= navigatePages) {
navigatepageNums = new int[pages];
for (int i = 0; i < pages; i++) {
navigatepageNums[i] = i + 1;
}
} else { //当总页数大于导航页码数时
navigatepageNums = new int[navigatePages];
int startNum = pageNum - navigatePages / 2;
int endNum = pageNum + navigatePages / 2;
if (startNum < 1) {
startNum = 1;
//(最前navigatePages页
for (int i = 0; i < navigatePages; i++) {
navigatepageNums[i] = startNum++;
}
} else if (endNum > pages) {
endNum = pages;
//最后navigatePages页
for (int i = navigatePages - 1; i >= 0; i--) {
navigatepageNums[i] = endNum--;
}
} else {
//所有中间页
for (int i = 0; i < navigatePages; i++) {
navigatepageNums[i] = startNum++;
}
}
}
}
private void calcPage() {
if (this.navigatepageNums != null && this.navigatepageNums.length > 0) {
this.navigateFirstPage = this.navigatepageNums[0];
this.navigateLastPage = this.navigatepageNums[this.navigatepageNums.length - 1];
if (this.pageNum > 1) {
this.prePage = this.pageNum - 1;
}
if (this.pageNum < this.pages) {
this.nextPage = this.pageNum + 1;
}
}
}
private void judgePageBoudary() {
this.firstPage = this.pageNum == 1;
this.lastPage = this.pageNum == this.pages || this.pages == 0;
this.hasPreviousPage = this.pageNum > 1;
this.hasNextPage = this.pageNum < this.pages;
}
}
#对pagehelp的使用方式
@GetMapping(value = "/list")
@ApiOperation(value = "获取用户列表")
public ResponseData<MyPageInfo<User>> getList(@RequestParam(value = "page", defaultValue = "1") Integer page, @RequestParam(value = "size", defaultValue = "10") Integer size) {
PageHelper.startPage(page, size);
List<User> list = userService.getList();
log.info("查询用户列表记录数为 :【{}】", list.size());
MyPageInfo<User> pageInfo = new MyPageInfo<>(list);
return ResultGenerator.successResult(pageInfo);
}
参考
转载地址:https://blog.csdn.net/pingpei1133/article/details/94429402
Springboot集成Swagger2显示字段属性说明的更多相关文章
- SpringBoot集成Swagger2实现Restful(类型转换错误解决办法)
1.pom.xml增加依赖包 <dependency> <groupId>io.springfox</groupId> <artifactId>spri ...
- springboot集成swagger2构建RESTful API文档
在开发过程中,有时候我们需要不停的测试接口,自测,或者交由测试测试接口,我们需要构建一个文档,都是单独写,太麻烦了,现在使用springboot集成swagger2来构建RESTful API文档,可 ...
- SpringBoot集成Swagger2在线文档
目录 SpringBoot集成Swagger2在线文档 前言 集成SpringBoot 登录接口文档示例 代码 效果 注解说明 总结 SpringBoot集成Swagger2在线文档 前言 不得不说, ...
- springboot 集成swagger2.x 后静态资源报404
package com.bgs360.configuration; import org.springframework.context.EnvironmentAware; import org.sp ...
- SpringBoot集成Swagger2并配置多个包路径扫描
1. 简介 随着现在主流的前后端分离模式开发越来越成熟,接口文档的编写和规范是一件非常重要的事.简单的项目来说,对应的controller在一个包路径下,因此在Swagger配置参数时只需要配置一 ...
- springboot集成swagger2报Illegal DefaultValue null for parameter type integer
springboot集成swagger2,实体类中有int类型,会报" Illegal DefaultValue null for parameter type integer"的 ...
- SpringBoot集成Swagger2 以及汉化 快速教程
(一) Swagger介绍 Swagger 是一款RESTFUL接口的文档在线自动生成+功能测试功能软件 (二)为什么使用Swagger 在现在的开发过程中还有很大一部分公司都是以口口相传的方式来进行 ...
- springboot 集成swagger2
使用Swagger 可以动态生成Api接口文档,在项目开发过程中可以帮助前端开发同事减少和后端同事的沟通成本,而是直接参照生成的API接口文档进行开发,提高了开发效率.这里以springboot(版本 ...
- SpringBoot 集成Swagger2自动生成文档和导出成静态文件
目录 1. 简介 2. 集成Swagger2 2.1 导入Swagger库 2.2 配置Swagger基本信息 2.3 使用Swagger注解 2.4 文档效果图 3. 常用注解介绍 4. Swagg ...
- 【java框架】SpringBoot(3) -- SpringBoot集成Swagger2
1.SpringBoot web项目集成Swagger2 1.1.认识Swagger2 Swagger 是一个规范和完整的框架,用于生成.描述.调用和可视化 RESTful 风格的 Web 服务.总体 ...
随机推荐
- 【邮件伪造】SPF与DKIM验证原理及实战解析(上)
0x01 前言 大家好,我是VoltCary 本篇文章是系列邮件安全专题的第一篇,主要帮助大家掌握邮件安全的基础知识. 基础内容包括: SMTP协议 邮件安全验证原理与过程 SPF验证与DKIM签名验 ...
- 西门子PLC与上位机通信方案梳理
一.前言 大家好!我是付工. 西门子PLC是工控领域使用非常多的一种PLC品牌,对于上位机开发人员来说,对于西门子PLC的通信,我们一般可以采取哪些通信方式呢? 今天跟大家分享一下上位机实现与西门子P ...
- Spring AOP基础、快速入门
介绍 AOP,面向切面编程,作为面向对象的一种补充,将公共逻辑(事务管理.日志.缓存.权限控制.限流等)封装成切面,跟业务代码进行分离,可以减少系统的重复代码和降低模块之间的耦合度.切面就是那些与业务 ...
- 【字符串哈希+二分】AcWing3508 最长公共子串
题解 首先思考暴力枚举长度为 \(len∈[1, min(strlen(s), strlen(t))]\),最差情况下为字符串 \(s\) 和字符串 \(t\) 全为长度为 \(10000\) 的全英 ...
- Nvidia Jetson Xavier NX安装GPU版pytorch与torchvision
前提是已经安装好了系统,并通过JetPack配置完了cuda.cudnn.conda等库. 1. 安装GPU版pytorch 在base环境上新建环境,python版本3.8,激活并进入. conda ...
- 【C#】【FFmpeg】获取电脑可用音视频设备并输出到下拉列表框
[重要]不要边看文本边操作,本文由错误纠正,先看完一遍再说. 要使用的FFmpeg命令 ffmpeg -list_devices true -f dshow -i dummy 会输出的信息 通过正则取 ...
- Linux系统安装python3.8与卸载教程
ln -sf /usr/local/python311/bin/python3.11 /usr/local/bin/python3ln -sf /usr/local/python311/bin/pyd ...
- 【转载】 Locust 官方文档
链接:https://www.jianshu.com/p/40102e9a24cb 安装 一般直接通过 pip 就可以安装: $ pip install locust 注意: Locust 1.x 版 ...
- 为什么 Llama 3.3 70B 比 GPT-4o 和 Claude 3.5 Sonnet 更优秀
过去七天的 AI 新闻如狂风暴雨般涌来,AI 世界发生了许多重大变化.在这篇文章中,我们将深入探讨来自 Llama 3.3 70B.GPT-4o 和 Claude 3.5 Sonnet 等主要参与者的 ...
- Qt开源作品16-通用无边框拖动拉伸
一.前言 相信各位CS结构开发的程序员,多多少少都遇到过需要美化界面的事情,一般都不会采用系统的标题栏,这样就需要无边框标题栏窗体,默认的话无边框的标题栏都不支持拉伸和拖动的,毕竟去掉了标题栏则意味着 ...