闲谈 Swagger3

Swagger3介绍

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

导入springdoc-openapi依赖

    
        org.springframework.boot
        spring-boot-starter-parent
        3.1.0
         
    

        
            org.springdoc
            springdoc-openapi-starter-webmvc-ui
            2.0.4
        

 编写Swagger3的配置类

@Configuration
public class OpenApiConfig {


    @Bean
    public OpenAPI mallTinyOpenAPI() {
        return new OpenAPI()
                .info(
                        new Info().title("SpringDoc API")
                                .description("SpringDoc API 演示")
                                .version("v1.0.0")
                                .license(new License().name("Apache 2.0")))
                .externalDocs(new ExternalDocumentation()
                        .description("SpringBoot项目全套文档"));
    }


    @Bean
    public GroupedOpenApi account() {
        return GroupedOpenApi.builder()
                .group("A-User")
                .displayName("A-User System")
                .pathsToMatch("/api/**/SysUser/**")
                .build();
    }

    @Bean
    public GroupedOpenApi publicApi() {
        return GroupedOpenApi.builder()
                .group("brand")
                .pathsToMatch("/api/**/message/**")
                .build();
    }

}

使用注解为Swagger3 

@RestController
@RequestMapping(value = "/api/{channel}/{version}/SysUser")
public interface UserSystemServer {

    @PostMapping(value = "/code")
    @Operation(summary = "管理员#发送验证码", description = "发送验证码", tags = {"Code Management"})
    @Parameters(value = {
            @Parameter(name = "version", description = "协议版本", example = "v1"),
            @Parameter(name = "channel", description = "渠道", example = "web")
    })
    Result code(@PathVariable String channel,@PathVariable String version, @RequestBody Map account);

    @ResponseBody
    @PostMapping(value = "/add/user")
    @Operation(summary = "新增用户", description = "新增用户", tags = {"User Management"})
    @Parameters(value = {
            @Parameter(name = "version", description = "协议版本", example = "v1"),
            @Parameter(name = "channel", description = "渠道", example = "web")
    })
    Result addUser(@PathVariable String channel,@PathVariable String version, String name);

    @ResponseBody
    @PostMapping(value = "/del/user")
    @Operation(summary = "删除用户", description = "删除用户", tags = {"User Management"})
    @Parameters(value = {
            @Parameter(name = "version", description = "协议版本", example = "v1"),
            @Parameter(name = "channel", description = "渠道", example = "web")
    })
    Result delUser(@PathVariable String channel,@PathVariable String version, String name);


    @ResponseBody
    @PostMapping(value = "/update/user")
    @Operation(summary = "编辑用户", description = "编辑用户", tags = {"User Management"})
    @Parameters(value = {
            @Parameter(name = "version", description = "协议版本", example = "v1"),
            @Parameter(name = "channel", description = "渠道", example = "web")
    })
    Result updateUser(@PathVariable String channel,@PathVariable String version, @RequestBody Map account);
}
@RestController
public class SysUserController implements UserSystemServer {


    @Override
    public Result code(String channel, String version, Map account) {
        return Result.ok().success("test");
    }

    @Override
    public Result addUser(String channel, String version, String name) {
        return Result.ok().success("test");
    }

    @Override
    public Result delUser(String channel, String version, String name) {
        return Result.ok().success("test");
    }

    @Override
    public Result updateUser(String channel, String version, Map account) {
        return Result.ok().success("test");
    }
}

测试 

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

闲谈 Swagger3_第1张图片

你可能感兴趣的:(Swagger,java,spring,boot)