SpringBoot配置Swagger3(含源码)
时间:2021-07-18
本文章向大家介绍SpringBoot配置Swagger3(含源码),主要包括SpringBoot配置Swagger3(含源码)使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
1.在pom.xml中添加依赖包
<!-- swagger --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-boot-starter</artifactId> <version>3.0.0</version> </dependency>
2.添加swagger配置文件
package com.llltony.springboot.config; 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.oas.annotations.EnableOpenApi; import springfox.documentation.service.ApiInfo; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; /** * 基于Swagger生成API文档 * * @EnableOpenApi:启动OpenApi的类 * @author: lll */ @Configuration @EnableOpenApi public class SwaggerConfiguration { @Bean public Docket createRestApi() { return new Docket(DocumentationType.OAS_30).apiInfo(apiInfo()).select() .apis(RequestHandlerSelectors.basePackage("com.llltony.springboot.controller")).paths(PathSelectors.any()) .build(); } private ApiInfo apiInfo() { return new ApiInfoBuilder().title("Tsl API文档").description("Tsl API文档") // .contact(new Contact("联系人", "www.baidu.com", "286279186@qq.com")) .version("1.0").build(); } }
3.在bean文件中添加对应的swagger注解ApiModel和ApiModelProperty
package com.llltony.springboot.bean; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; import java.io.Serializable; import java.util.List; @ApiModel(value = "EmployeeVo", description = "EmployeeVo类") public class EmployeeVo implements Serializable { @ApiModelProperty(value = "员工队列") List<Employee> employeeLst; public void setEmployeeLst(List<Employee> employeeLst) { this.employeeLst = employeeLst; } public List<Employee> getEmployeeLst() { return employeeLst; } }
4.在controller中添加swagger注解Api和ApiOperation
package com.llltony.springboot.controller; import com.llltony.springboot.bean.Employee; import com.llltony.springboot.bean.EmployeeVo; import com.llltony.springboot.bean.ResultMap; import com.llltony.springboot.service.EmployeeService; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.web.bind.annotation.*; import javax.annotation.Resource; import java.io.IOException; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.UUID; @RestController @RequestMapping("/employee") @Api(tags = "员工") public class EmployeeController { @Resource EmployeeService employeeService; @Resource RabbitTemplate rabbitTemplate; //增加学生 @RequestMapping(value = "/", method = RequestMethod.POST) @ApiOperation(value="增加学生",notes = "增加学生信息") public ResultMap saveEmp(@RequestBody EmployeeVo employeeVo) { ResultMap resultMap = new ResultMap(); try { employeeService.saveEmp(employeeVo); resultMap.setStatus("200"); resultMap.setMessage("保存成功"); } catch (Exception e) { resultMap.setStatus("500"); resultMap.setMessage("保存失败"); } return resultMap; } //通过消息队列异步增加学生 @RequestMapping(value = "/addRebbitMq", method = RequestMethod.POST) @ApiOperation(value="通过消息队列异步增加学生",notes = "通过消息队列异步增加学生") public ResultMap addRebbitMq(@RequestBody EmployeeVo employeeVo) { ResultMap resultMap = new ResultMap(); try { String messageId = String.valueOf(UUID.randomUUID()); String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")); Map<String, Object> map = new HashMap<>(); map.put("messageId", messageId); map.put("messageData", employeeVo); map.put("createTime", createTime); //将消息携带绑定键值:TestDirectRouting 发送到交换机TestDirectExchange rabbitTemplate.convertAndSend("EmployeeDirectExchange", "EmployeeDirectRouting", map); // employeeService.saveEmp(employeeVo); resultMap.setStatus("200"); resultMap.setMessage("保存成功"); } catch (Exception e) { resultMap.setStatus("500"); resultMap.setMessage("保存失败"); } return resultMap; } //删除学生 @DeleteMapping("/{ids}") @ApiOperation(value="删除学生",notes = "删除学生") public ResultMap delEmp(@PathVariable("ids") String ids) { ResultMap resultMap = new ResultMap(); try { employeeService.delEmp(ids); resultMap.setStatus("200"); resultMap.setMessage("删除成功"); } catch (Exception e) { resultMap.setStatus("500"); resultMap.setMessage("删除失败"); } return resultMap; } //查询学生 @GetMapping("/{id}") @ApiOperation(value="查询学生",notes = "查询学生") public Employee getEmp(@PathVariable("id") Integer id) throws IOException { return employeeService.getEmpById(id); } //查询所有的学生 @GetMapping("/getAll") @ApiOperation(value="查询所有的学生",notes = "查询所有的学生") public List<Employee> getAllEmp() { return employeeService.getAllEmp(); } //修改学生 @PutMapping("/") @ApiOperation(value="修改学生",notes = "修改学生") public ResultMap updateEmp(@RequestBody Employee employee) { ResultMap resultMap = new ResultMap(); try { employeeService.updateEmp(employee); resultMap.setStatus("200"); resultMap.setMessage("保存成功"); } catch (Exception e) { resultMap.setStatus("500"); resultMap.setMessage("保存失败"); } return resultMap; } }
5.配置完成,在浏览器中请求:http://127.0.0.1:8181/swagger-ui/index.html
6.注意事项
如果使用了默认的访问静态配置:static-locations: classpath:html,会导致访问swagger报错。
解决这个问题需要自己重写静态资源访问方法,代码如下:
package com.llltony.springboot.config; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport; public class WebConf extends WebMvcConfigurationSupport { @Override protected void addResourceHandlers(ResourceHandlerRegistry registry) { //映射static路径的请求到static目录下 registry.addResourceHandler("/static/**") .addResourceLocations("classpath:/static/"); super.addResourceHandlers(registry); } }
7.资源
原文地址:https://www.cnblogs.com/LiLiliang/p/15026884.html
- nodejs 语法学习(持续更新)
- Django——模板层(template)(模板语法、自定义模板过滤器及标签、模板继承)
- - Templates should only be responsible for mapping the state to the UI. Avoid placing tags with side
- Django - - - -视图层之视图函数(views)
- fiddler mock ==> AutoResponder
- 基于Node.js开发跨平台窗口程序
- Django视图层之路由配置系统(urls)
- java String时间转为时间戳
- linux 简易启动脚本
- 2017年我国大数据产业发展五大新突破
- Django---MTV模型、基本命令、简单配置
- Vue.js 入门
- 解读python中SocketServer源码
- java学习:Hibernate学习-用oracle sequence序列生成ID的配置示例
- java教程
- Java快速入门
- Java 开发环境配置
- Java基本语法
- Java 对象和类
- Java 基本数据类型
- Java 变量类型
- Java 修饰符
- Java 运算符
- Java 循环结构
- Java 分支结构
- Java Number类
- Java Character类
- Java String类
- Java StringBuffer和StringBuilder类
- Java 数组
- Java 日期时间
- Java 正则表达式
- Java 方法
- Java 流(Stream)、文件(File)和IO
- Java 异常处理
- Java 继承
- Java 重写(Override)与重载(Overload)
- Java 多态
- Java 抽象类
- Java 封装
- Java 接口
- Java 包(package)
- Java 数据结构
- Java 集合框架
- Java 泛型
- Java 序列化
- Java 网络编程
- Java 发送邮件
- Java 多线程编程
- Java Applet基础
- Java 文档注释
- Spring4定时器 cronTrigger和simpleTrigger实现方法
- Tomcat远程调试
- IDEA建立Spring MVC Hello World 详细入门教程
- 嗯,手搓一个TinyPng压缩图片的WebpackPlugin也SoEasy啦
- Spring源码解析——核心类介绍
- IO之Basic IO
- 静态方法中注入bean
- 高通量数据中批次效应的鉴定和处理(五)- 预测并校正可能存在的混杂因素
- JVM 学习笔记(一)
- JVM 学习笔记(二)
- JVM 学习笔记(三)
- 算法数据结构 | 图论基础算法——拓扑排序
- JVM 学习笔记(四)
- Python 如何实时绘制数据
- JVM 学习笔记 (六)