SpringBoot整合swagger2
1.使用 Swagger2 可以减少编写过多的文档,只需要通过代码就能生成文档API,提供给前端人员常方便。
在Java类中添加Swagger的注解即可生成Swagger接口,常用Swagger注解如下:
@Api:修饰整个类,描述Controller的作用 @ApiOperation:描述一个类的一个方法,或者说一个接口
@ApiParam:单个参数描述 @ApiModel:用对象来接收参数 @ApiModelProperty:用对象接收参数时,描述对
象的一个字段 @ApiResponse:HTTP响应其中1个描述 @ApiResponses:HTTP响应整体描述 @ApiIgnore:使用
该注解忽略这个API @ApiError :发生错误返回的信息 @ApiImplicitParam:一个请求参数
@ApiImplicitParams:多个请求参数
@ApiImplicitParam属性:
例如:
1 2 3 4 | @ApiImplicitParams ({ @ApiImplicitParam (name = "page" , value = "页码" , required = true , paramType = "path" , dataType = "int" ), @ApiImplicitParam (name = "size" , value = "每页记录数" , required = true , paramType = "path" , dataType = "int" ) }) |
2.Swagger2依赖
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | <!-- swagger2 配置 --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version> 2.4 . 0 </version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version> 2.4 . 0 </version> </dependency> <dependency> <groupId>com.github.xiaoymin</groupId> <artifactId>swagger-bootstrap-ui</artifactId> <version> 1.6 </version> </dependency> |
3.代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | package com.qingfeng.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.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 Swagger2 { // 配置swagger2核心配置 docket @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) // 指定api类型为swagger2 .apiInfo(apiInfo()) // 用于定义api文档汇总信息 .select() .apis(RequestHandlerSelectors .basePackage( "com.imooc.controller" )) // 指定controller包 .paths(PathSelectors.any()) // 所有controller .build(); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title( "某某平台接口api" ) // 文档页标题 .contact( new Contact( "imooc" , "https://www.baidu.com" , "123456xxx@163.com" )) // 联系人信息 .description( "某某平台提供的api文档" ) // 详细信息 .version( "1.0.1" ) // 文档版本号 .termsOfServiceUrl(https: //www.baidu.com"") // 网站地址 .build(); } } |
4.启动你的项目,访问地址
1 2 3 | 两个都可以,推荐使用第二个 http: //localhost: 端口号/swagger-ui.html 原路径 http: //localhost:端口号/doc.html 原路径 |
5.访问的也面试英文的,我们可以在Controller里面进行中文优化
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | package com..qingfeng.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.RequestMethod; import org.springframework.web.bind.annotation.RestController; import springfox.documentation.annotations.ApiIgnore; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; //@ApiIgnore //忽略 就是这个Controller的所有的方法都不会显示在Swagger2的网页上 @RestController @Api (value = "Hello接口controller" , tags = { "Hello接口相关的api" }) public class HelloController { @ApiOperation (value = "发送Hello" , notes = "发送Hello" , httpMethod = "GET" ) //在Swagger2页面方法的详细 @RequestMapping (value = "/hello" ,method = RequestMethod.GET) public Object hello(){ return "Hello World" ; } } |
6.我们还可已在实体类里面优化
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | package com.qingfeng.pojo; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; @ApiModel (value = "用户对象" , description = "从客户端,由用户传入的数据封装在此entity中" ) public class User { // example = "root" 是一个示例 @ApiModelProperty (value = "用户名" , name = "username" , example = "root" , required = true ) private String username; @ApiModelProperty (value = "密码" , name = "password" , example = "123456" , required = true ) private String password; public String getUsername() { return username; } public void setUsername(String username) { this .username = username; } public String getPassword() { return password; } public void setPassword(String password) { this .password = password; } } |
分类:
Springboot的整合
标签:
swagger2
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南