Swagger-Springboot-mybatis-mysql
时间:2022-07-22
本文章向大家介绍Swagger-Springboot-mybatis-mysql,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
文章目录
- 此项目说明
- 环境
- 源代码
- 一、引入Swagger依赖
- 二、配置Swagge配置文件
- 三、编写Controller层
- 四、数据库结构和数据
- 五、测试结果
- 测试1、原始Ui界面:
- 测试2、BS的UI:
此项目说明
此项目是我闲着的时候,整理出来的。因为我主要是做后端的,接口可能测试比较麻烦,所以就想起来了使用Swagger来管理API,然后还起到了测试的作用,还省得费劲去整理API文档了。一举两得,灰常的完美鸭!
环境
maven
SpringBoot
注:下文全是在SpringBoot2.x和已有Maven环境的基础上进行做的。
如果没有请移步:
SpringBoot一1(初次使用)+HelloWord
源代码
本项目源代码
SpringBoot一1(初次使用)+HelloWord
Maven环境配置-必会
一、引入Swagger依赖
<!-- 引入swgger相关依赖 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.6.1</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.6.1</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-asl</artifactId>
<version>1.9.13</version>
</dependency>
为了让界面漂亮引入bootstrap-ui
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>swagger-bootstrap-ui</artifactId>
<version>1.9.1</version>
</dependency>
原始的界面:
引入bootstrap-ui后的:
此工程的目录结构:
二、配置Swagge配置文件
在项目下的config包下创建SwaggerConfig.java文件
用来配置Swagger的信息的,例如:
项目的简介
做着
版本
服务URL
分组名称等
这些信息会在主页进行显示,如图所示:
SwaggerConfig.java
package cn.mybatistest.Config;
import com.google.common.base.Predicates;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
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;
@Configuration //必须存在
@EnableSwagger2 //必须存在 启动Swagger
@EnableWebMvc //必须存在
@ComponentScan("cn.mybatistest.controller")//必须存在 扫描的API Controller包
public class SwaggerConfig {
@Bean //创建一个Docket容器
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.groupName("学生信息查询系统")
.select()
.apis(RequestHandlerSelectors.any()) //.basePackage("net.zoneday.excel.controller")
.paths(PathSelectors.any())
// 不显示错误的接口地址
.paths(Predicates.not(PathSelectors.regex("/error.*"))) //错误路径不监控
.build()
.apiInfo(apiInfo());
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("学生信息查询系统")
.description("这是一个利用Swagger写的一个学生信息查询系统的API管理工具")
.termsOfServiceUrl("http://www.sdgzs.com")
.contact(new Contact("郑晖", "http://www.sdgzs.com", "8042965@sdgzs.com"))
.license("Apache License 2.0")
.licenseUrl("")
.version("1.0.0")
.build();
}
}
在项目下的config包下创建WebMvcConfig.java文件
此文件是用来配置UI的访问界面的路径和静态资源的配置,如果不配置这个,是无法访问UI界面的。
package cn.mybatistest.Config;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
//原始的swagger UI界面的配置
registry.addResourceHandler("swagger-ui.html")
.addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/");
//引入BS后的的swagger UI界面的配置
registry.addResourceHandler("doc.html")
.addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/");
}
}
三、编写Controller层
以查询学号查询学生姓名为例
这里只展示出COntroller层的代码,如果想要其他的,请自行下载源代码工程文件。
package cn.mybatistest.controller;
import cn.mybatistest.dao.StudentDao;
import cn.mybatistest.service.StudentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@RequestMapping("/student")
@Api(value = "查询学生信息")
public class StudentController {
@Autowired
StudentService studentService;
/**
* 通过学号查询学生姓名
* @param sno
* @return
*/
@PostMapping("/getStudentById")
@ResponseBody
@ApiOperation("使用学号查询学生名字")
public String getStudentById(@ApiParam(value = "请输入学生学号",required = true) @RequestParam(name="sno")String sno){
String Sname = studentService.getStudentById(sno);
return Sname;
}
}
四、数据库结构和数据
/*
Navicat MySQL Data Transfer
Source Server : 127.0.0.1
Source Server Version : 50553
Source Host : localhost:3306
Source Database : test5
Target Server Type : MYSQL
Target Server Version : 50553
File Encoding : 65001
Date: 2019-08-04 10:03:42
*/
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for student
-- ----------------------------
DROP TABLE IF EXISTS `student`;
CREATE TABLE `student` (
`Sno` varchar(12) NOT NULL COMMENT '学号',
`Sdept` varchar(20) NOT NULL,
`Sname` varchar(20) NOT NULL,
`Clbum` varchar(20) NOT NULL,
`Sex` varchar(4) NOT NULL,
`Age` int(3) NOT NULL,
`DormID` varchar(20) NOT NULL,
`PoiticsStatus` varchar(20) NOT NULL,
`HomeAddress` varchar(50) NOT NULL,
`Rest` varchar(200) NOT NULL,
PRIMARY KEY (`Sno`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of student
-- ----------------------------
INSERT INTO `student` VALUES ('201711106044', '计算机学院', 'zhenghui', '网络171C', '男', '21', '竹一630', '共青团员', '', '这是计算机学院的人');
INSERT INTO `student` VALUES ('201711106003', '计算机学院', 'wangwu', '网络171C', '男', '21', '竹三412', '党员', '济南市', '这个也是好人');
INSERT INTO `student` VALUES ('201811101001', '机电学院', '张志洋', '机电181', '男', '18', '竹三111', '', '', '');
INSERT INTO `student` VALUES ('201611101001', '艺术学院', '李小明', '技术161', '女', '0', '松四504', '', '', '');
五、测试结果
测试1、原始Ui界面:
输入:
http://localhost:8080/swagger-ui.html#
测试2、BS的UI:
- ASP.NET Ajax 库
- ASP.NET进程优化
- 多说 提速:js内页页脚加载、静态文件CDN
- 微信小程序的王者时代
- [程序设计语言]-[核心概念]-02:名字、作用域和约束(Bindings)
- NUMA架构
- 如何处理 Python 入门难以进步的现象?
- 编写前置和后置条件的连贯接口库:CuttingEdge.Conditions
- [程序设计语言]-[核心概念]-03:控制流
- 简单代码实现“网站维护中”“coming soon” 效果
- 使用Topshelf创建Windows 服务
- 自定义AuthorizeAttribute
- 系统进程管理工具Process Explorer
- jquery 操作DOM元素(1)
- MySQL 教程
- MySQL 安装
- MySQL 管理与配置
- MySQL PHP 语法
- MySQL 连接
- MySQL 创建数据库
- MySQL 删除数据库
- MySQL 选择数据库
- MySQL 数据类型
- MySQL 创建数据表
- MySQL 删除数据表
- MySQL 插入数据
- MySQL 查询数据
- MySQL where 子句
- MySQL UPDATE 查询
- MySQL DELETE 语句
- MySQL LIKE 子句
- mysql order by
- Mysql Join的使用
- MySQL NULL 值处理
- MySQL 正则表达式
- MySQL 事务
- MySQL ALTER命令
- MySQL 索引
- MySQL 临时表
- MySQL 复制表
- 查看MySQL 元数据
- MySQL 序列 AUTO_INCREMENT
- MySQL 处理重复数据
- MySQL 及 SQL 注入
- MySQL 导出数据
- MySQL 导入数据
- MYSQL 函数大全
- MySQL Group By 实例讲解
- MySQL Max()函数实例讲解
- mysql count函数实例
- MYSQL UNION和UNION ALL实例
- MySQL IN 用法
- MySQL between and 实例讲解
- java-覆盖equals和hashcode方法
- java-单链表反转解法及分析
- JAVA-判断两个单链表是否相交并求交点
- 删除排序数组中重复元素的方法
- zookeeper-3.4.10伪集群模式搭建及简单操作
- 04 Confluent_Kafka权威指南 第四章: kafka消费者:从kafka读取数据
- JAVA中的单例模式分析(doublecheck和枚举实现)
- 有关JAVA自动装箱-拆箱的分析
- 10 Confluent_Kafka权威指南 第十章:监控kafka
- 解决elasticsearch“Too many open files in system”问题
- elk基于jolokia监控springboot应用jvm方案
- elasticsearch集群更换节点操作
- 对storm1.2.3并行度的理解
- RabbitMQ集群搭建过程
- java中的reference(一): GC与4种基本的Reference(强软弱虚)