Springboot整合swagger3
2022/8/4 6:25:26
本文主要是介绍Springboot整合swagger3,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
在pom.xml里引入swagger3的依赖
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>springboot_xzit</artifactId> <groupId>com.xzit</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>day2_job</artifactId> <properties> <maven.compiler.source>11</maven.compiler.source> <maven.compiler.target>11</maven.compiler.target> </properties> <dependencies> <!-- 引入swagger3(springfox)的依赖 --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-boot-starter</artifactId> <version>3.0.0</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> </dependency> </dependencies> </project>
创建一个主类,加入springboot的注解
package com.xzit; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import springfox.documentation.oas.annotations.EnableOpenApi; @SpringBootApplication @EnableOpenApi public class Day2server { public static void main(String[] args) { SpringApplication.run(Day2server.class,args); } }
编写swagger配置类
package com.xzit.config; import io.swagger.annotations.Api; 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; @Configuration public class Swagger3Config { @Bean public Docket apiConfig(){ return new Docket(DocumentationType.OAS_30) .apiInfo(apiInfo()) .select() //设置通过什么方式定位到需要生成文档的接口 //定位了方法上的ApiOperation .apis(RequestHandlerSelectors.withClassAnnotation(Api.class)) //接口URL路径,any表示全部路径 .paths(PathSelectors.any()) .build(); } private ApiInfo apiInfo(){ return new ApiInfoBuilder() .title("伊万的测试项目") .description("项目描述信息") .contact(new Contact("伊万","http://www.baidu.com","ussa@qq.com")) .version("1.0") .build(); } }
创建一个自定义的application.yml配置文件
ant_path_matcher是swagger的配置 banner.txt是自定义springboot启动时显示的头图 restart: exclude是定义如static等文件夹里静态资源的变化不触发springboot服务的自动重启
spring: mvc: pathmatch: matching-strategy: ant_path_matcher banner: location: banner.txt devtools: restart: exclude: static/**,public/**,templates/**
创建一个Teacher类
package com.xzit.model; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; @Data @NoArgsConstructor @AllArgsConstructor @ApiModel(value = "老师对象",description = "老师对象,用来对应数据库表teacher") public class Teacher { @ApiModelProperty(value = "老师id",required = true,example = "1") private int id; @ApiModelProperty(value = "老师姓名",required = true,example = "伊万老师") private String name; @ApiModelProperty(value = "老师性别",required = true,example = "男") private String gender; @ApiModelProperty(value = "老师年龄",required = false,example = "26") private int age; }
创建一个controller类
package com.xzit.controller; import com.xzit.model.Teacher; import io.swagger.annotations.*; import org.springframework.web.bind.annotation.*; import java.util.ArrayList; import java.util.List; @RestController @RequestMapping("/teacher") @Api(tags = {"老师管理"})//用在类上,表示对类的说明tags表示说明该类的作用 public class TeacherController { @GetMapping() @ApiOperation(value = "显示全部老师数据") //用在方法上,说明方法的用途和作用 public Object list(){ List<Teacher> list=new ArrayList<>(); list.add(new Teacher(1,"廖老师","女",29)); list.add(new Teacher(2,"王老师","男",35)); list.add(new Teacher(3,"李老师","男",31)); list.add(new Teacher(4,"张老师","女",29)); return list; } @GetMapping("/{id}") @ApiOperation("按给定Id来查询老师信息") @ApiImplicitParams( @ApiImplicitParam( name = "id",value = "老师id",required = true, paramType = "path", dataType = "Integer",dataTypeClass = Integer.class ) ) public Object selectById(@PathVariable int id){ return new Teacher(id,"郭老师","男",22); } @DeleteMapping("/{id}") @ApiOperation("按给定ID来删除老师信息") public Object deleteById(@ApiParam(name = "id",value = "老师id") @PathVariable int id){ return "已删除给定的id为"+id+"的老师对象"; } @PostMapping() @ApiOperation("新增老师") public Object save(@ApiParam(name = "teacher",value = "老师对象")@RequestBody Teacher teacher){ return teacher; } @PutMapping() @ApiOperation("更新老师") public Object update(@RequestBody Teacher teacher){ return teacher; } }
这篇关于Springboot整合swagger3的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2025-01-07如何利用看板工具优化品牌内容创作与审批,确保按时发布?
- 2025-01-07百万架构师第十一课:源码分析:Spring 源码分析:Spring源码分析前篇|JavaGuide
- 2025-01-07质量检测标准严苛,这 6 款办公软件达标了吗?
- 2025-01-07提升品牌活动管理的效率:看板工具助力品牌活动日历的可视化管理
- 2025-01-07宠物商场的精准营销秘籍:揭秘看板软件的力量
- 2025-01-07“30了,资深骑手” | 程序员能有什么好出路?
- 2025-01-07宠物公园的营销秘籍:看板软件如何帮你精准触达目标客户?
- 2025-01-07从任务分解到资源优化:甘特图工具全解析
- 2025-01-07企业升级必备指南:从传统办公软件到SaaS工具的转型攻略
- 2025-01-07一文告诉你IT项目管理如何做到高效