SpringBoot基于Swagger2怎么构建API文档
发表于:2026-10-31 作者:千家信息网编辑
千家信息网最后更新 2026年10月31日,本文小编为大家详细介绍"SpringBoot基于Swagger2怎么构建API文档",内容详细,步骤清晰,细节处理妥当,希望这篇"SpringBoot基于Swagger2怎么构建API文档"文章能帮助
千家信息网最后更新 2026年10月31日SpringBoot基于Swagger2怎么构建API文档
本文小编为大家详细介绍"SpringBoot基于Swagger2怎么构建API文档",内容详细,步骤清晰,细节处理妥当,希望这篇"SpringBoot基于Swagger2怎么构建API文档"文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。
一、添加依赖
io.springfox springfox-swagger2 2.7.0 io.springfox springfox-swagger-ui 2.7.0
二、创建Swagger2配置类
package com.offcn.config;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.spi.DocumentationType;import springfox.documentation.spring.web.plugins.Docket;import springfox.documentation.swagger2.annotations.EnableSwagger2;@Configuration//表示该类为一个配置类,相当于spring中的xml配置文件@EnableSwagger2 //开启在线文档public class SwaggerConfig { //1.声明 api 文档的属性 private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("Spring Boot中使用Swagger2构建RESTful APIs") .description("优就业") .termsOfServiceUrl("http://www.ujiuye.com/") .contact("小刘同学") .version("1.0") .build(); } //配置核心配置信息 public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.basePackage("com.offcn.controller")) .paths(PathSelectors.any()) .build(); }}三、修改Controller 增加文档注释
通过@ApiOperation注解来给API增加说明
通过@ApiImplicitParams@ApiImplicitParam注解来给参数增加说明
package com.offcn.controller;import com.offcn.dao.UserDao;import com.offcn.entity.User;import io.swagger.annotations.ApiImplicitParam;import io.swagger.annotations.ApiImplicitParams;import io.swagger.annotations.ApiOperation;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.DeleteMapping;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RequestMapping("/rest")@RestControllerpublic class RestFulController { @Autowired private UserDao userDao; @GetMapping("/getUserById") @ApiOperation(value="查找指定id用户信息", notes="根据id查找用户信息") @ApiImplicitParams({ @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Integer"), }) public User getUserById(Integer id){ User user = userDao.getOne(id); return user; } @DeleteMapping("/del") @ApiOperation(value="删除指定id用户信息", notes="根据id删除用户信息") @ApiImplicitParams({ @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Integer"), }) public String delUserById(Integer id){ userDao.deleteById(id); return "success"; }}四、查看Swagger2文档
重启项目
访问:
http://localhost:8080/swagger-ui.html
读到这里,这篇"SpringBoot基于Swagger2怎么构建API文档"文章已经介绍完毕,想要掌握这篇文章的知识点还需要大家自己动手实践使用过才能领会,如果想了解更多相关内容的文章,欢迎关注行业资讯频道。
文档
用户
信息
配置
文章
内容
注解
妥当
参数
同学
属性
思路
文件
新知
更多
核心
步骤
注释
知识
知识点
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
咖啡网
天津市摩尔互联网科技
上海睿民互联网科技有限公司
金融科技创新监管互联网金融
互联网科技金融
平安科技 互联网
猪兼强互联网科技
礼当家互联网科技
互联网科技作文
深圳齐家互联网科技
沃里互联网科技
上游互联网科技
浙江互联网科技研学营
智腾互联网科技有限公司
山东子淼互联网科技有限公司
北京市互联网新科技
扫呗互联网科技有限公司
吉林省码上互联网科技电话
互联网对科技影响
宁波允成互联网科技有限公司
广东猪兼强互联网科技有限公司
世界互联网领先科技成果
互联网科技有限公司 翻译
深圳前海聚择互联网科技有限公司
深圳艾果互联网通讯科技
长沙渺峰互联网科技有限公司
科技互联网公司两年变化
保山浩联互联网科技有限公司
科技互联网企业在行动
科技发展第三阶段互联网时代