手把手教你从0开始配置IDEA中的Swagger
创作时间:
作者:
@小白创作中心
手把手教你从0开始配置IDEA中的Swagger
引用
CSDN
1.
https://blog.csdn.net/jialuosi/article/details/132871295
Swagger 是一组用于设计、构建、文档化和使用 RESTful Web 服务的开源工具和框架。它允许开发团队设计、构建和测试 API,并提供易于理解的文档,以便开发人员和消费者能够快速了解和使用 API。Swagger 通常与各种编程语言和框架一起使用,以简化 API 的开发和维护过程。
第一步:用IDEA搭建一个Spring Boot项目
- 打开 IntelliJ IDEA,选择 "File" -> "New" -> "Project",创建一个新的 Spring Boot 项目。
- 在项目创建向导中,选择所需的 Spring Boot 版本和依赖,然后完成项目创建。等待依赖加载完毕。
- 项目建立后的文件目录结构如下:
第二步:添加Swagger相关依赖
- 打开
pom.xml文件,添加以下三个依赖:
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.7.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.7.0</version>
</dependency>
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring-boot-starter</artifactId>
<version>2.0.8</version>
</dependency>
- 配置
application.properties文件,主要是 JDBC 相关配置:
# 将路径替换为你的路径
spring.datasource.url=jdbc:mysql://localhost:3306/food?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false&allowMultiQueries=true
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driverClassName = com.mysql.cj.jdbc.Driver
在
com.swaggertest下新建config文件夹,然后新建SwaggerConfig和WebMvcConfigurer两个 Java 文件。配置
SwaggerConfig:
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket docket() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
Contact contact = new Contact("加洛斯", "666", "xxxxx@qq.com");
return new ApiInfo(
"文档标题",
"文档标题的描述",
"版本1.0",
"urn:tos",
contact,
"Apache 2.0",
"http://www.apache.org/licenses/LICENSE-2.0",
new ArrayList());
}
}
- 配置
WebMvcConfigurer:
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
@Configuration
public class WebMvcConfigurer extends WebMvcConfigurationSupport {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
}
}
第三步:启动SpringBoot
- 启动 Spring Boot 应用程序,访问
http://localhost:8080/doc.html或http://localhost:8080/swagger-ui.html即可查看接口文档。
第四步:使用Swagger
- 新建一个
entity包,在此包下建立一个食物实体类Food和foodtype。注意实体类要遵循小驼峰命名法。
@Data
@ApiModel(value = "食物类Model")
public class Food {
@ApiModelProperty(value = "食物ID")
private String foodId;
@ApiModelProperty(value = "食物名称")
private String foodName;
@ApiModelProperty(value = "食物种类ID")
private String foodTypeId;
@ApiModelProperty(value = "食物种类名")
private String foodTypeName;
public Food(String foodId, String foodName, String foodTypeId, String foodTypeName) {
this.foodId = foodId;
this.foodName = foodName;
this.foodTypeId = foodTypeId;
this.foodTypeName = foodTypeName;
}
}
@Data
@ApiModel(value = "食物种类Model")
public class FoodType {
@ApiModelProperty(value = "食物种类ID")
private String foodTypeId;
@ApiModelProperty(value = "食物种类名")
private String foodTypeName;
@ApiModelProperty(value = "食物种类所属列表")
private List<Food> foodLists;
}
- 新建
Controller包下的foodcontroller,配置相关注解。
@RestController
@Api(value = "食物Controller", tags = { "获取食物相关接口" })
@RequestMapping(value = "/food")
public class FoodController {
@ApiOperation(value = "获取食物种类类")
@GetMapping("/getFoodtype")
public FoodType test2() throws Exception {
return null;
}
@ApiOperation(value = "获取食物类")
@GetMapping("/getFood")
public List<Food> test1() throws Exception {
Food food = new Food("1", "锅包肉","1", "肉类");
List<Food> foodList = new ArrayList<>();
foodList.add(food);
return foodList;
}
}
- 访问
http://localhost:8080/doc.html#/home查看效果:
热门推荐
瑞香花期的秘密:冬季如何养护?
冬季瑞香花养护秘籍大公开!
岭南春困大作战:孙教授教你告别疲劳
异地住院,乡村合作医疗如何报销?需要哪些材料?
大S的公益之路:从汶川地震捐款到环保倡导者的蜕变
从低谷到幸福:大S离婚后的最后时光
从离婚风波到直播女王:张兰的逆袭之路
大S骤逝后,汪小菲具俊晔谁更深情?
“终结艾滋病流行”不能只是口号
拿个脸盆就能预防晕车?春运中,真正的“防晕车”秘诀是…
对乙酰氨基酚异丙嗪注射液不能和哪些药一起使用
孕期遭遇尴尬——“漏尿”怎么办?
磷酸铁锂电池:冬季续航大挑战
新能源车电池黑科技:磷酸铁锂低温性能大突破!
冬季续航焦虑?新型材料让磷酸铁锂电池"不怕冷"!
上海注册信息技术公司的完整攻略
如何像苹果和阿里一样用名字提升品牌?
企业名称注册风险与商标保护全攻略
如何给你的初创企业起个像“苹果”或“特斯拉”一样的好名?
租房管理费合法吗?律师来解答
DIY起泡胶:从材料准备到成品的完整指南
从心理学角度看AI能否成为人类的朋友?
AI伴侣:你的新朋友还是孤独的救星?
打羽毛球,轻松缓解颈椎骨质增生
颈椎保卫战:从日常习惯开始
双十一后,低头族如何预防颈椎病?
秋冬颈椎保养,中医来支招!
打工人必看:颈椎病自救指南
手机屏幕漏液?教你一招搞定!
如何分析市场主力动向