当前位置: 首页 > news >正文

SpringBoot整合Swagger2快速指南

Swagger简介

Swagger是一款强大的API文档生成工具,它能够自动为RESTful API生成可视化文档,支持在线测试接口,极大提高了前后端协作效率。本文将详细介绍如何在SpringBoot项目中整合Swagger2。

环境准备

版本要求

重要提示:SpringBoot版本不能过高,推荐使用2.5.6版本:

<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.5.6</version>
</parent>

添加依赖

<!-- Swagger UI可视化界面 -->
<dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger-ui</artifactId><version>2.9.2</version>
</dependency>
<!-- Swagger2核心依赖 -->
<dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger2</artifactId><version>2.9.2</version>
</dependency>

Swagger配置类

基础配置类

创建SwaggerConfig.java配置类:

@Configuration
@EnableSwagger2
public class SwaggerConfig implements WebMvcConfigurer {@Beanpublic Docket createRestApi() {return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select().apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)).paths(PathSelectors.any()).build().enable(true);}private ApiInfo apiInfo() {return new ApiInfoBuilder().title("API文档").description("接口文档详情信息").version("1.0").contact(new Contact("开发者", "", "developer@example.com")).licenseUrl("").build();}@Overridepublic void addResourceHandlers(ResourceHandlerRegistry registry) {registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");}
}

配置说明

  • @EnableSwagger2:启用Swagger2功能
  • Docket:Swagger的核心配置类
  • apiInfo():设置文档的基本信息
  • addResourceHandlers():配置静态资源映射

Swagger注解详解

控制器层注解

类级别注解:

@Controller
@RequestMapping("/user")
@Api(tags = {"用户信息接口"})
public class UserController {// ...
}

方法级别注解:

@Controller
@RequestMapping("/user")
@Api(tags = {"用户信息接口"})
public class UserController {// ...
}

参数注解

简单参数:

@ApiImplicitParams({@ApiImplicitParam(value = "用户名", name = "userName", dataType = "string"),@ApiImplicitParam(value = "年龄", name = "age", dataType = "integer")
})
public List<User> findAll(String userName, int age) {// ...
}

实体类参数:

@ApiModel("用户类实体信息")
public class User {@ApiModelProperty(value = "用户id", example = "1")private Integer id;@ApiModelProperty(value = "用户名", example = "张三")private String username;// 其他属性...
}

接口测试

启动项目后访问以下URL进入Swagger UI界面:

Swagger UIhttp://localhost:8080/swagger-ui.html界面将展示所有配置了Swagger注解的API,你可以:

  1. 查看API详细说明
  2. 直接测试接口
  3. 查看请求/响应示例
http://www.dtcms.com/a/275586.html

相关文章:

  • Elasticsearch 线程池
  • nginx反向代理实现跨域请求
  • 从零到一:企业如何组建安全团队
  • Go语言高并发聊天室(二):WebSocket服务器实现
  • 白皮精读——2025医疗数据合规白皮书 【附全文阅读】
  • JVM 类加载过程
  • 5. JVM 的方法区
  • 云端docker小知识
  • Vue Vue-route (6)
  • 【SpringAI】7. 基于 milvus 的向量检索
  • Go语言生态成熟度分析:为何Go还无法像Java那样实现注解式框架?
  • 个人面经250712
  • JDK的Stream API使用详解
  • HTML(上)
  • 基于Opencv的缺陷检测实战
  • 《目标检测模块实践手册:从原理到落地的尝试与分享》第一期
  • 服务器怎么跑Python项目?
  • 【408考研知识点全面讲解计算机学科专业基础综合(408)】——数据结构之排序
  • 无法打开windows安全中心解决方案
  • 从基础加热到智能生态跨越:艾芬达用创新重构行业价值边界!
  • 14. 请谈一下浏览器的强缓存和协商缓存
  • Django母婴商城项目实践(四)
  • 算法魅力-BFS解决最短路问题
  • 鸿蒙开发竖的线
  • Typecho集成PHPMailer实现邮件订阅功能完整指南
  • 如何查看服务器当前用户的权限
  • Windows X64环境下mysql5.6.51安装指南
  • 联邦学习客户端异构数据特征对齐:挑战、方法与应用
  • 如何防范金融系统中的SQL注入攻击
  • QWidget的属性