当前位置:   article > 正文

闲谈 Swagger3_springdoc-openapi-starter-webmvc-ui

springdoc-openapi-starter-webmvc-ui

Swagger3介绍

SpringDoc也是 spring 社区维护的一个项目(非官方),帮助使用者将 swagger3 集成到 Spring 中。
也是用来在 Spring 中帮助开发者生成文档,并可以轻松的在spring boot中使用。

导入springdoc-openapi依赖

  1. <parent>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-parent</artifactId>
  4. <version>3.1.0</version>
  5. <relativePath/> <!-- lookup parent from repository -->
  6. </parent>
  1. <dependency>
  2. <groupId>org.springdoc</groupId>
  3. <artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
  4. <version>2.0.4</version>
  5. </dependency>

 编写Swagger3的配置类

  1. @Configuration
  2. public class OpenApiConfig {
  3. @Bean
  4. public OpenAPI mallTinyOpenAPI() {
  5. return new OpenAPI()
  6. .info(
  7. new Info().title("SpringDoc API")
  8. .description("SpringDoc API 演示")
  9. .version("v1.0.0")
  10. .license(new License().name("Apache 2.0")))
  11. .externalDocs(new ExternalDocumentation()
  12. .description("SpringBoot项目全套文档"));
  13. }
  14. @Bean
  15. public GroupedOpenApi account() {
  16. return GroupedOpenApi.builder()
  17. .group("A-User")
  18. .displayName("A-User System")
  19. .pathsToMatch("/api/**/SysUser/**")
  20. .build();
  21. }
  22. @Bean
  23. public GroupedOpenApi publicApi() {
  24. return GroupedOpenApi.builder()
  25. .group("brand")
  26. .pathsToMatch("/api/**/message/**")
  27. .build();
  28. }
  29. }

使用注解为Swagger3 

  1. @RestController
  2. @RequestMapping(value = "/api/{channel}/{version}/SysUser")
  3. public interface UserSystemServer {
  4. @PostMapping(value = "/code")
  5. @Operation(summary = "管理员#发送验证码", description = "发送验证码", tags = {"Code Management"})
  6. @Parameters(value = {
  7. @Parameter(name = "version", description = "协议版本", example = "v1"),
  8. @Parameter(name = "channel", description = "渠道", example = "web")
  9. })
  10. Result code(@PathVariable String channel,@PathVariable String version, @RequestBody Map<String, Object> account);
  11. @ResponseBody
  12. @PostMapping(value = "/add/user")
  13. @Operation(summary = "新增用户", description = "新增用户", tags = {"User Management"})
  14. @Parameters(value = {
  15. @Parameter(name = "version", description = "协议版本", example = "v1"),
  16. @Parameter(name = "channel", description = "渠道", example = "web")
  17. })
  18. Result addUser(@PathVariable String channel,@PathVariable String version, String name);
  19. @ResponseBody
  20. @PostMapping(value = "/del/user")
  21. @Operation(summary = "删除用户", description = "删除用户", tags = {"User Management"})
  22. @Parameters(value = {
  23. @Parameter(name = "version", description = "协议版本", example = "v1"),
  24. @Parameter(name = "channel", description = "渠道", example = "web")
  25. })
  26. Result delUser(@PathVariable String channel,@PathVariable String version, String name);
  27. @ResponseBody
  28. @PostMapping(value = "/update/user")
  29. @Operation(summary = "编辑用户", description = "编辑用户", tags = {"User Management"})
  30. @Parameters(value = {
  31. @Parameter(name = "version", description = "协议版本", example = "v1"),
  32. @Parameter(name = "channel", description = "渠道", example = "web")
  33. })
  34. Result updateUser(@PathVariable String channel,@PathVariable String version, @RequestBody Map<String, Object> account);
  35. }
  1. @RestController
  2. public class SysUserController implements UserSystemServer {
  3. @Override
  4. public Result code(String channel, String version, Map<String, Object> account) {
  5. return Result.ok().success("test");
  6. }
  7. @Override
  8. public Result addUser(String channel, String version, String name) {
  9. return Result.ok().success("test");
  10. }
  11. @Override
  12. public Result delUser(String channel, String version, String name) {
  13. return Result.ok().success("test");
  14. }
  15. @Override
  16. public Result updateUser(String channel, String version, Map<String, Object> account) {
  17. return Result.ok().success("test");
  18. }
  19. }

测试 

访问:http://localhost:8888/swagger-ui.html

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/107969
推荐阅读
相关标签
  

闽ICP备14008679号