6.Swagger的实战使用

news/2024/4/28 4:44:03/文章来源:https://blog.csdn.net/qq_44724899/article/details/130040803

六.Swagger的实战使用

1.什么是swagger

2.swagger的基本使用

3.swagger实战使用

六.Swagger的实战使用

1.什么是swagger

  • swagger是后端接口文档的生成并且提供ui界面进行测试
  • 过去用postman测试
    缺点:需要自己写地址,如果项目变了需要自己更改

2.swagger的基本使用

①mybatis-plus代码生成器支持swagger(项目中代码生成器可以直接加入swagger注解)

  • 原理
    代码生成器根据数据表中的描述字段来生成对实体类的文档描述
package com.atguigu.srb.core;import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import org.junit.Test;public class CodeGenerator {@Testpublic void genCode() {// 1、创建代码生成器AutoGenerator mpg = new AutoGenerator();// 2、全局配置GlobalConfig gc = new GlobalConfig();String projectPath = System.getProperty("user.dir");gc.setOutputDir(projectPath + "/src/main/java");gc.setAuthor("Likejin");//open等于true时,生成完代码后,打开资源管理器gc.setOpen(false); //生成后是否打开资源管理器//去掉service接口前缀Igc.setServiceName("%sService");	//去掉Service接口的首字母I//自增策略gc.setIdType(IdType.AUTO); //主键策略//自动生成接口文档@APIgc.setSwagger2(true);//开启Swagger2模式mpg.setGlobalConfig(gc);// 3、数据源配置DataSourceConfig dsc = new DataSourceConfig();dsc.setUrl("jdbc:mysql://localhost:13306/db200921_srb_core?useUnicode=true&characterEncoding=utf8&useSSL=false");dsc.setDriverName("com.mysql.jdbc.Driver");dsc.setUsername("root");dsc.setPassword("abc123");dsc.setDbType(DbType.MYSQL);mpg.setDataSource(dsc);// 4、包配置PackageConfig pc = new PackageConfig();pc.setParent("com.atguigu.srb.core");pc.setEntity("pojo.entity"); //此对象与数据库表结构一一对应,通过 DAO 层向上传输数据源对象。mpg.setPackageInfo(pc);// 5、策略配置//能够完成数据库表名从下划线到类的驼峰习惯StrategyConfig strategy = new StrategyConfig();strategy.setNaming(NamingStrategy.underline_to_camel);//数据库表映射到实体的命名策略//能够完成数据库字段名从下划线到类的属性驼峰习惯strategy.setColumnNaming(NamingStrategy.underline_to_camel);//数据库表字段映射到实体的命名策略//使用lombok的注解修饰strategy.setEntityLombokModel(true); // lombok//生成逻辑删除字段名strategy.setLogicDeleteFieldName("is_deleted");//逻辑删除字段名//符合阿里巴巴规范,去掉is前缀strategy.setEntityBooleanColumnRemoveIsPrefix(true);//去掉布尔值的is_前缀(确保tinyint(1))//和controller的生成有关,全部使用@RestControllerstrategy.setRestControllerStyle(true); //restful api风格控制器,restController返回json数据mpg.setStrategy(strategy);// 6、执行mpg.execute();}
}

②swagger文档的生成和ui界面

  • 引入pom
<!--swagger--><dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger2</artifactId></dependency><!--swagger ui--><dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger-ui</artifactId></dependency>
  • swaager的配置
    生成分组
package com.atguigu.srb.base.config;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
@EnableSwagger2
public class Swagger2Config {//生成admin分组@Beanpublic Docket adminApiConfig() {return new Docket(DocumentationType.SWAGGER_2).groupName("adminApi")//增加分组//增加分组的描述.apiInfo(adminApiInfo()).select()//对路径为/admin的接口分到一组.paths(Predicates.and(PathSelectors.regex("/admin/.*"))).build();}private ApiInfo adminApiInfo(){return new ApiInfoBuilder().title("尚融宝后台管理系统API文档").description("本文档描述了尚融宝管理系统各个接口的调用方式").contact(new Contact("like","www.baidu.com","13030@qq.com")).build();}//生成web分组@Beanpublic Docket webApiConfig() {return new Docket(DocumentationType.SWAGGER_2).groupName("webApiInfo").apiInfo(adminApiInfo()).select().paths(Predicates.and(PathSelectors.regex("/api/.*"))).build();}private ApiInfo webApiInfo(){return new ApiInfoBuilder().title("尚融宝网站API文档").description("本文档描述了尚融宝网站各个接口的调用方式").contact(new Contact("like","www.baidu.com","13030@qq.com")).build();}}

在这里插入图片描述

③实体类描述

  • 能够生成实体类的描述字段
    @ApiModel对实体类表名的描述
    @ApiModelProperty对实体类字段的描述
package com.atguigu.srb.core.pojo.entity;import java.math.BigDecimal;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import java.time.LocalDateTime;
import com.baomidou.mybatisplus.annotation.TableLogic;
import com.baomidou.mybatisplus.annotation.TableField;
import java.io.Serializable;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;/*** <p>* 积分等级表* </p>** @author Likejin* @since 2023-04-09*/
@Data
@EqualsAndHashCode(callSuper = false)
@ApiModel(value="IntegralGrade对象", description="积分等级表")
public class IntegralGrade implements Serializable {private static final long serialVersionUID = 1L;@ApiModelProperty(value = "编号")@TableId(value = "id", type = IdType.AUTO)private Long id;@ApiModelProperty(value = "积分区间开始")private Integer integralStart;@ApiModelProperty(value = "积分区间结束")private Integer integralEnd;@ApiModelProperty(value = "借款额度")private BigDecimal borrowAmount;@ApiModelProperty(value = "创建时间")private LocalDateTime createTime;@ApiModelProperty(value = "更新时间")private LocalDateTime updateTime;@ApiModelProperty(value = "逻辑删除(1:已删除,0:未删除)")@TableField("is_deleted")@TableLogicprivate Boolean deleted;}

在这里插入图片描述

  • 生成controller接口的描述
    @API 接口名称的描述
    @ApiOperation 接口内每一个方法的描述
package com.atguigu.srb.core.controller;import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;/*** <p>* 积分等级表 前端控制器* </p>** @author Likejin* @since 2023-04-09*/
@Api(tags = "网站积分接口")
@RestController
@RequestMapping("/api/core/integralGrade")
public class IntegralGradeController {@ApiOperation("测试接口")@GetMapping("/test")public void test(){return;}}
package com.atguigu.srb.core.controller.admin;import com.atguigu.srb.core.pojo.entity.IntegralGrade;
import com.atguigu.srb.core.service.IntegralGradeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;import javax.annotation.Resource;
import java.util.List;/*** <p>* 积分等级表 前端控制器* </p>** @author Likejin* @since 2023-04-09*/@Api(tags="积分等级管理")
@CrossOrigin
@RestController
@RequestMapping("/admin/core/integralGrade")
public class AdminIntegralGradeController {@Resourceprivate IntegralGradeService integralGradeService;@ApiOperation("积分等级列表")@GetMapping("/list")public List<IntegralGrade> listAll(){return integralGradeService.list();}@ApiOperation("根据ID删除数据记录")@DeleteMapping("/remove/{id}")public boolean removeById(@ApiParam("数据id")@PathVariable Long id){return integralGradeService.removeById(id);}}

在这里插入图片描述

3.swagger实战使用(代码同上)

①引入依赖

②Swagger配置config

  • 生成文档
  • 配置分组(利用接口地址配置分组)

③Swagger控制实体类

  • 对类名的描述@ApiModel
  • 对类属性的描述@ApiModelProperty

④Swagger控制controller

  • 对接口描述@Api
  • 对接口中具体方法描述@ApiOperation

⑤结果

  • 访问项目的接口文档
    http://localhost:8110/swagger-ui.html
  • 分组结果
    在这里插入图片描述
  • 接口描述
    在这里插入图片描述
  • 实体类描述
    在这里插入图片描述

未更新

未更新

未更新

未更新

未更新

未更新

未更新

未更新

未更新

未更新

未更新

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.luyixian.cn/news_show_283933.aspx

如若内容造成侵权/违法违规/事实不符,请联系dt猫网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

MySQL事务 【事务操作丨事务四大特性丨事务隔离级别丨事务原理】

在实际的开发过程中&#xff0c;一个业务操作如&#xff1a;转账&#xff0c;往往是要多次访问数据库才能完成的。转账是一个用户扣钱&#xff0c;另一个用户加钱。如果其中有一条 SQL 语句出现异常&#xff0c;这条 SQL 就可能执行失败。 事务是一组操作的集合&#xff0c;它…

计讯物联小型水库雨水情测报与大坝安全监测一体化解决方案,确保水库安全运行

方案背景 防洪治理工程是一项重大的民生工程&#xff0c;也是重大的生态工程。基于我国水灾频发的大背景下&#xff0c;小型水库作为防汛抗洪的重要基础设施&#xff0c;其雨水情测报与大坝安全监测是十分有必要的&#xff0c;不仅可为预防水灾、防汛决策提供大量可靠的数据和资…

深入浅出:理解 RPC 和 Dubbo 架构

简介 Apache Dubbo是一款高性能的Java RPC框架.其前身是阿里巴巴公司开源的一个高性能,轻量级的开源Java RPC框架,可以和Spring框架无缝集成. Dubbo 官网 RPC RPC介绍 Remote Procedure Call 远程过程调用,是分布式架构的核心,按响应方式分以下两种: 同步调用:客户端调用…

CAN通讯协议

1&#xff09; CAN介绍 a) 什么是CAN? b) CAN总线特点 c) CAN应用场景 2&#xff09;CAN物理层 a) CAN物理层特性 b) CAN收发器芯片介绍 3&#xff09;CAN协议层 a) CAN帧种类介绍 b) CAN数据帧介绍 c) CAN位时序介绍 d) CAN总线仲裁 a)、CAN介绍 CAN&#xff08;Controlle…

SpringBoot中配置文件加密及跨域支持

给application.properties文件中的某些值加密,比如数据库账号密码等. 引入依赖 <dependency><groupId>com.github.ulisesbocchio</groupId><artifactId>jasypt-spring-boot-starter</artifactId><version>3.0.3</version> </dep…

并行分布式计算 并行计算机体系结构

文章目录并行分布式计算 并行计算机体系结构并行计算机结构模型SIMD 单指令多数据流PVP 并行向量处理机SMP 对称多处理机MPP 大规模并行处理机DSM 分布式共享存储多处理机COW 工作站集群总结并行计算机访存模型UMA 均匀存储访问模型NUMA 非均匀存储访问模型COMA 全高速缓存存储…

Nestjs实战干货-概况-控制器-Controller

Controller 控制器 控制器负责处理传入的请求并向客户返回响应。 一个控制器的目的是接收应用程序的特定请求。路由机制控制哪个控制器接收哪些请求。通常&#xff0c;每个控制器有一个以上的路由&#xff0c;不同的路由可以执行不同的动作。 为了创建一个基本的控制器&#…

【游戏逆向】加密坐标浅析

这个游戏里面坐标有很多种存放方式。 例如明文存放的DOUBLE&#xff0c;加密的各种类型。 我们不知道哪一个对于我们是有用的,哪一些只是辅助UI或则掉到LUA虚拟机坑里的数据。 那就根据作用大小来决定,一一尝试吧。 最好去找修改之后有效果的地址&#xff0c;当然只是本地&…

MySQL中count(1)和count(*)哪个性能好?

当我们对某一张表中的数据需要统计数量的时候&#xff0c;我们通常会用到count(1)、count(*)或者count(字段)&#xff0c;而这三种哪个方式的count效率最高呢&#xff1f;先来说结论&#xff1a; count(1) count(*) > count(字段) 为什么会得到如上的结论&#xff0c;下面来…

1672_MIT 6.828 xv6中如何通过构建环境让系统中增加一个可执行调用文件

全部学习汇总&#xff1a; GreyZhang/g_unix: some basic learning about unix operating system. (github.com) 前面已经分析了如何实现一个系统调用&#xff0c;这个过程的梳理也已经整理成了一份学习笔记。这一次看一下&#xff0c;如何让OS的系统中增加这样的一个可执行的文…

Arduino2.0.4的安装以及上传错误:exit status2

一、安装并下载Arduino 可以进入到下面这个网站中下载会比较快。 Arduino IDE下载&#xff08;9月15日更新到2.0&#xff09;-Arduino爱好者 - Powered by Discuz! Arduino IDE下载&#xff08;9月15日更新到2.0&#xff09;-Arduino爱好者 - Powered by Discuz!Arduino IDE下…

【基于冗余缩减变换:Pan-Sharpening】

Pan-Sharpening Based on Transformer With Redundancy Reduction &#xff08;基于冗余缩减变换的全色锐化算法&#xff09; 基于深度神经网络&#xff08;DNN&#xff09;的泛锐化方法已经产生了最先进的结果。然而&#xff0c;在全色&#xff08;PAN&#xff09;图像和低空…

Python-Python基本用法(全:含基本语法、用户交互、流程控制、数据类型、函数、面向对象、读写文件、异常、断言等)

1 环境准备 编辑器&#xff1a;Welcome to Python.org 解释器&#xff1a;pycharm&#xff1a;Thank you for downloading PyCharm! (jetbrains.com) 2 Quick start 创建项目 new project create demo print(Dad!!)3 基本语法 3.1 print 直接打印 print(Dad!!)拼接打印…

AD20添加元件3D库

Altium Designer是画PCB常用的工具之一,为了PCB的美观性,我们可以采用3D的方式查看已经画好的PCB板。但在这之前需要准备好每个元器件的3D模型。 1、下载3D格式模型 http://www.3dcontentcentral.cn 当然要先注册账户。 在搜索栏输入你想要找的器件。 模型格式STEP AP214…

vue-cli 初始化工程

个人记录下vue-cli创建项目的步骤 卸载老版本的vue-cli (这不是必要的) npm uninstall vue-cli -g 如果本地使用 yarn的话,还需执行 yarn global remove vue-cli 安装全新的vue-cli npm install -g vue/cli 安装指定版本的vue-cli npm install -g vue/…

linux 安装git并拉取代码教程

#一步一步执行以下命令sudo apt install git #安装gitgit --version #查看安装版本号git config user.name jtr #设置用户名git config user.email jiangtrcloudskysec.com #设置邮箱ssh-keygen -t rsa -C "jiangtrcloudskysec.com" #生成秘钥&#xff0c;一直往下按…

自动化测试框架:DrissionPage(1)——安装与设置

发现了一款基于Python的网页自动化工具&#xff1a;DrissionPage。这款工具既能控制浏览器&#xff0c;也能收发数据包&#xff0c;甚至能把两者合而为一&#xff0c;简单来说&#xff1a;集合了WEB浏览器自动化的便利性和 requests 的高效率优点。 一、DrissionPage框架产生背…

【C++进阶】01:概述

概述 OVERVIEW概述C11新特性&#xff1a;C14新特性&#xff1a;C17新特性&#xff1a;C20新特性&#xff1a;C程序编译过程C内存模型CSTL1.Queue&Stack2.String3.MapC语言C语言面向过程编程✅✅面向对象编程&#xff08;类和对象&#xff09;❌✅泛型编程、元编程&#xff…

数据结构之七大排序

数据结构之七大排序&#x1f506;排序的概念及其运用排序的概念常见的排序算法&#x1f506;插入排序直接插入排序希尔排序&#x1f506;选择排序直接选择排序堆排序&#x1f506;交换排序冒泡排序快排&#x1f506;归并排序&#x1f506;非比较排序&#x1f506;结语&#x1f…

十二、Pytorch复现Residual Block

一、Residual Network 论文出处&#xff1a;Deep Residual Learning for Image Recognition 其核心模块&#xff1a; 二、复现Residual Block 这里以两层卷积层为例进行设计复现 resnet可以很好的解决梯度消失问题 Residual Block大致要点&#xff1a; 样本x传入模型&…