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 服务.总体 ...
随机推荐
- vue 存储cookie 与使用
安装cookie依赖包 npm i @vueuse/integrations 安装cookie npm i universal-cookie 使用cookie import {useCookies} ...
- P11378[GESP202412 七级]燃烧 题解
闲话 花了一个小时. 主要原因:条初始值硬控我半小时,题目看错硬控我半小时(悲). 正文 看题目,就是求从哪个点出发所得到的所有单调下降序列的总长度最长(这个描述好奇怪,不过意思是对的). 题目中说的 ...
- WebP图片使用踩坑
前情 WebP是一种同时提供了有损压缩与无损压缩(可逆压缩)的图片文件格式,最初在2010年发布,目标是减少文件大小,相较于传统的 PNG.JPG,甚至是动图 GIF 的格式,WebP 比它们的空间更 ...
- Flutter 错误The argument type 'Color' can't be assigned to the parameter type 'MaterialStateProperty<Color?>?'.dart(argument_type_not_assignable)
MaterialStateProperty<Color?>?和Color 当为TextButton等button添加颜色时,使用ButtonStyle为其添加颜色 TextButton( ...
- 【PHP】连接数据库验证登陆
界面 <!doctype html> <html lang="en"> <head> <!-- Required meta tags -- ...
- Nginx日志重定向到标准输出
背景静态站点使用`docker`部署时,希望`nginx前台启动`的同时可以将错误日志和访问日志全部重定向到标准输出,便于采集和处理! 实现只需要修改`nginx.conf`中`3行`关于日志的配置就 ...
- Spark面试题汇总及答案(推荐收藏)
一.面试题 Spark 通常来说,Spark与MapReduce相比,Spark运行效率更高.请说明效率更高来源于Spark内置的哪些机制? hadoop和spark使用场景? spark如何保证宕机 ...
- 工具大全-dirsearch探测Web目录
dirsearch介绍 dirsearch是一款开源的.基于Python开发的命令行工具,主要用于对Web服务器进行目录和文件的扫描,以发现潜在的安全漏洞. dirsearch下载地址: https: ...
- hhhhhhomework 验证码界面(非全部自己完成)
import javax.swing.*;//import 代表"引入" //javax.swing 代表"路径" (在javax文件夹下的swing文件夹) ...
- KMS for Windows 11
I. 镜像下载 Windows 镜像下载地址:站点1,站点2 II. 手动激活 参考文档:Easy ways to activate Windows 11 for FREE without a pro ...