SpringBoot与SpringDoc整合Swagger3超简洁

想必大家之前不愿意用swagger一定是因为它的配置极其繁琐,但是配置好了swagger确实较postman好用,传统的SpringBoot使用Swagger时配置及其繁琐,随着SpringBoot的不断更新,SpringDoc整合了Swagger,并且提供了非常简洁的整合方式

1、添加SpringDoc的依赖:

        <!--添加springDoc依赖-->
        <dependency>
            <groupId>org.springdoc</groupId>
            <artifactId>springdoc-openapi-spring-boot-2-webmvc</artifactId>
            <version>3.1.5</version>
        </dependency>

注意上述的版本适合于SpringBoot2.6.2版本,框架版本不同时可能会爆启动错误!!

2、然后在SpringBoot的yml文件中,简单定义SpringDoc的基本设置

springdoc:
  api-docs:
    enabled: true
    path: /你自己的项目名称.html
  swagger-ui:
    path: /swagger-ui.html
    disable-swagger-default-url: on
spring:
  mvc:
    pathmatch:
      matching-strategy: ant_path_matcher

3、创建一个配置类,加一些注解。

import io.swagger.v3.oas.annotations.OpenAPIDefinition;
import io.swagger.v3.oas.annotations.enums.SecuritySchemeType;
import io.swagger.v3.oas.annotations.info.Info;
import io.swagger.v3.oas.annotations.security.SecurityScheme;
import org.springframework.context.annotation.Configuration;

@Configuration
@OpenAPIDefinition(
        info = @Info(
                //参数的内容都是根据自己的项目自己起的
                title = "emos-api",
                description = "Emos管理系统的后端Java项目",
                version = "1.0"
        )
)
//为Swagger整合JWT支持,项目没有令牌模块下面这个注解可以不写
@SecurityScheme(
        name = "Token",
        type = SecuritySchemeType.HTTP,
        bearerFormat = "JWT",
        scheme = "bearer"
)
public class SwaggerConfig {

}

4、创建controller层的一个类即可测试:

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping("/user")
@Tag(name = "登录模块")//方法的显示标题
public class deletes {
    @PostMapping("/test")
    //参数用于提示用户这个接口是干什么的
    @Operation(summary = "测试Swagger")
    public String test() {
        return "测试Swagger成功";
    }

}

5、打开下面这个网址,然后输入在文本框中输入第二部的第四行的“/你的项目名称.html”即可访问,我给项目起的名字是study,所以我应该输入/study.html

 

http://127.0.0.1:8080/yml配置名称/swagger-ui/index.html?configUrl=/yml配置名称/yml配置名称.html/swagger-config

所以我的链接地址是:

http://127.0.0.1:8080/study/swagger-ui/index.html?configUrl=/study/study.html/swagger-config

 

继续向下翻就可以看到刚刚自己定义的方法啦。

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
好的,针对您的问题,我可以给出以下步骤: 1. 在 pom.xml 文件中添加 Swagger2 和 Swagger UI 的依赖: ```xml <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>3.0.0</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>3.0.0</version> </dependency> ``` 2. 创建 Swagger 配置类,用于配置 Swagger 的基本信息和扫描的包路径: ```java @Configuration @EnableSwagger2 public class SwaggerConfig { @Bean public Docket api() { return new Docket(DocumentationType.SWAGGER_2) .select() .apis(RequestHandlerSelectors.basePackage("com.example.demo.controller")) .paths(PathSelectors.any()) .build() .apiInfo(apiInfo()); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("Spring Boot 3 整合 Swagger") .description("使用 Swagger 构建 API 文档") .version("1.0.0") .build(); } } ``` 3. 在 Controller 类中添加 Swagger 注解,用于描述接口信息: ```java @RestController @RequestMapping("/user") @Api(tags = "用户管理") public class UserController { @GetMapping("/{id}") @ApiOperation(value = "根据 ID 获取用户信息", notes = "根据传入的 ID 参数获取用户信息") @ApiImplicitParam(name = "id", value = "用户 ID", required = true, dataType = "Long", paramType = "path") public User getUserById(@PathVariable Long id) { // 根据 ID 查询用户信息 return userService.getUserById(id); } // 其他接口方法... } ``` 4. 启动应用程序,访问 http://localhost:8080/swagger-ui.html 即可查看生成的 API 文档。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值