手把手教你SpringBoot快速集成Swagger的配置过程
作者:常安、 发布时间:2023-10-30 01:16:22
标签:SpringBoot,集成,Swagger
导语
相信大家无论是做前端还是做后端的,都被接口接口文档所折磨过,前端抱怨接口文档和后端给的不一致,后端抱怨写接口文档很麻烦,所以Swagger就诞生了。直接配置即可自动生成接口文档,而且提供了高效的API测试
话不多说直接开干
导入SpringBoot集成Swagger所需要的依赖
<!--web方便测试-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- swagger2核心包 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<!-- swagger-ui 可视化界面 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
Swagger可视化界面可分为三个区域
Swagger相关配置
package com.example.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
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;
import java.util.ArrayList;
@Configuration
@EnableSwagger2 //开启Swagger的使用
public class SwaggerConfig {
@Bean //Swagger的使用主要是要将docket对象传入IOC容器
public Docket docket(){
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo()) //关于文档的各种信息
.enable(true) //使Swagger生效
.groupName("常安祖")
.select()//选择扫描的接口
.apis(RequestHandlerSelectors.basePackage("com.example.controller"))//指定扫描的接口
.build();
}
public ApiInfo apiInfo(){
Contact contact = new Contact("长安","https://blog.csdn.net/weixin_45647685","719801748@qq.com");//个人的联系方式
return new ApiInfo("长安的文档", "长安的开发文档", "1.0", "urn:tos",null, "Apache 2.0", "http://www.apache.org/licenses/LICENSE-2.0", new ArrayList());//文档的各种信息
}
}
@ApiModel( ) //主要用来标注返回的实体类
@ApiModelProperty( ) //主要用来标注实体类中的属性
案例:
@ApiModel("用户的实体类")
public class User implements Serializable {
@ApiModelProperty("用户的id")
private Integer id;
@ApiModelProperty("用户的姓名")
private String name;
@ApiModelProperty("用户的年纪")
private Integer age;
public Integer getId() {
return id;
}
public User(Integer id, String name, Integer age) {
this.id = id;
this.name = name;
this.age = age;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
@ApiModelProperty用来标注API接口
案例:
package com.yangzihao.controller;
import com.yangzihao.entity.User;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
@RestController
public class UserController {
@ApiModelProperty("得到一个User")
@GetMapping("/getUser")
public User getUser(){
return new User(1,"测试",18);
}
}
进入Swagger可视化界面
使用Swagger进行接口测试
执行
来源:https://blog.csdn.net/weixin_45647685/article/details/113850616
0
投稿
猜你喜欢
- 反射方式获取JPA Entity属性和值在记录日志或者调试的时候,往往需要输出数据库查询或者写入的值,或者在接 * 互的时候,可能需要将实体转
- 一、获取android工程里面的各种资源的id; 1.1 string型 比如下面: << string name=”OK”&g
- 递归生成一个如图的菜单,编写两个类数据模型Menu、和创建树形的MenuTree。通过以下过程实现:1.首先从菜单数据中获取所有根节点。2.
- 谷歌官方推出了一种侧滑菜单的实现方式(抽屉效果),即 DrawerLayout,这个类是在Support Library里的,需要加上and
- 一、问题Flutter原有的图片缓存机制,是通过PaintingBinding.instance!.imageCache来管理缓存的,这个缓
- Tomcat 如何实现WebSocketWebSocket协议属于HTML5标准,越来越多浏览器已经原生支持WebSocket,它能让客户端
- 就网络和应用程序而言,键盘快捷键很重要,今天我们要谈的便是让这类快捷键得以在Flutter运作的小部件:Focus、Shortcuts和Ac
- 1 配置多数据源时,application.yml 的有关mybatis的配置是失效的,因为他不知道配置哪一个数据源2 applicatio
- Jsch是什么?JSch 是SSH2的一个纯Java实现。它允许你连接到一个sshd 服务器,使用端口转发,X11转发,文件传输等等。你可以
- timer和timertask是jdk自带的定时任务实现,无需导入第三方jar包来完成1、指定多久之后执行此任务,注意:只会执行一次publ
- 报错翻译: compileSdkVersion android-24”需要JDK 1.8或更高版本编译。报错现象如下图:原因:st
- 本文实例为大家分享了flutter实现底部导航栏的具体代码,供大家参考,具体内容如下一.flutter底部导航栏常用组件BottomNavi
- SpringCloudStream配置以下配置摘自《SpringCloud微服务实战》,配置主要包括两大部分:Stream配置(基础配置、通
- 在Android中使用SQLite数据库的入门指南,打算分下面几部分与大家一起分享, 1、什么是SQLite 2、Android中使用SQL
- resultMap的Colum和property属性1: resultMap标签当我们的数据库字段与实体类的属性不一致时,就需要使用该标签进
- reduce()简介Reduce 原意:减少,缩小根据指定的计算模型将Stream中的值计算得到一个最终结果解释:reduce 操
- 前言本篇文章 中写到的是 flutter 调用了Android 原生的 TextView 案例添加原生组件的流程基本上可以描述为:1 and
- 前后端分离开发中,一般都会遇到请求跨域问题。而且一般也会遇到登陆失效问题。今天就以springboot和vue为例来看如何解决上述问题增加过
- 前言这里主要简单介绍如何使用Camera+SurfaceView自定义相机拍照,如果是Camera2或者是TextureView的可以前往主
- 目前常用的ORM框架有 Mybatis(batis)、MybatisPlus,Hibernate、Jpa等几个框架,今天就简单介绍一下搭建M