• springboot配置swagger


    Swagger 是什么

    在这里插入图片描述

    Swagger 是一个用于构建、文档和调用 RESTful Web 服务的强大工具。它提供了以下几方面的好处:

    自动生成 API 文档: Swagger 可以自动生成 API 文档,包括接口的描述、输入参数、输出参数、请求示例、响应示例等信息。这使得开发人员、测试人员和客户端开发人员能够轻松地理解和使用 API。

    可视化交互界面: Swagger 生成的文档通常包括一个可视化的交互界面,允许用户测试 API 端点而无需编写任何代码。这简化了开发和测试的过程。

    标准化接口设计: Swagger 鼓励开发团队使用标准的注解或规范来定义 API,这样可以更容易地创建一致性的 API 设计。这对于多个开发团队协同工作的大型项目特别有用。

    客户端代码生成: Swagger 可以生成客户端代码,使客户端开发人员能够使用 API 而无需手动编写 HTTP 请求和数据模型的代码。这减少了代码重复和错误。

    减少文档维护成本: Swagger 自动生成的文档与实际代码同步,因此当 API 发生更改时,文档也会相应更新,减少了手动维护文档的工作。

    安全集成: Swagger 可以与身份验证和授权机制集成,帮助开发人员更轻松地确保 API 的安全性。

    代码注释: 使用 Swagger 注解,可以更清晰地记录 API 的用途、参数、响应等信息。这有助于提高代码的可维护性。

    Swagger配置springboot

    代码展示

    添加 Maven 依赖:首先,在你的 Spring Boot 项目的 pom.xml 文件中,添加 Swagger2 依赖。以下是一个示例:

    
    <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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0modelVersion>
        <parent>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-parentartifactId>
            <version>2.5.6version> 
        parent>
        <groupId>com.examplegroupId>
        <artifactId>swaggerdemo01artifactId>
        <version>0.0.1-SNAPSHOTversion>
        <name>swaggerdemo01name>
        <description>Demo project for Spring Bootdescription>
        <properties>
            <java.version>8java.version>
        properties>
        <dependencies>
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starterartifactId>
            dependency>
    
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-testartifactId>
                <scope>testscope>
            dependency>
    
    
    
            <dependency>
                <groupId>io.springfoxgroupId>
                <artifactId>springfox-swagger2artifactId>
                <version>2.9.2version>
            dependency>
            <dependency>
                <groupId>io.springfoxgroupId>
                <artifactId>springfox-swagger-uiartifactId>
                <version>2.9.2version>
            dependency>
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-webartifactId>
            dependency>
            <dependency>
                <groupId>com.vaadin.external.googlegroupId>
                <artifactId>android-jsonartifactId>
                <version>0.0.20131108.vaadin1version>
                <scope>compilescope>
            dependency>
            <dependency>
                <groupId>com.alibabagroupId>
                <artifactId>fastjsonartifactId>
                <version>2.0.32version>
                <scope>compilescope>
            dependency>
    
    
        dependencies>
    
    
    project>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64

    创建 Swagger 配置类:在你的项目中创建一个配置类,通常命名为 SwaggerConfig,并添加 @Configuration 注解。

    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;
    import springfox.documentation.swagger2.annotations.EnableSwagger2;
    
    @Configuration
    @EnableSwagger2
    public class SwaggerConfig {
        @Bean
        public Docket createRestApi() {
            return new Docket(DocumentationType.SWAGGER_2)
                    .pathMapping("/")
                    .select()
                    .apis(RequestHandlerSelectors.basePackage("com.example.swaggerdemo.controller"))
                    .paths(PathSelectors.any())
                    .build().apiInfo(new ApiInfoBuilder()
                            .title("雷达数据修正算法相关接口")
                            .description("雷达数据修正算法逻辑展示")
                            .version("9.0")
                            .contact(new Contact("z s","blog.git.net"," "))
                            .license("The Apache License")
                            .licenseUrl("http://www.s t.com/")
                            .build());
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32

    启用 Swagger:在你的 Spring Boot 应用程序主类上添加 @EnableSwagger2 注解。

    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import springfox.documentation.swagger2.annotations.EnableSwagger2;
    
    @SpringBootApplication
    @EnableSwagger2
    public class Swaggerdemo01Application {
    
        public static void main(String[] args) {
            SpringApplication.run(Swaggerdemo01Application.class, args);
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    编写 API 文档注释:在你的控制器类和方法上使用 Swagger 注解编写接口文档的注释,包括参数、响应等信息。示例:

    import io.swagger.annotations.Api;
    import io.swagger.annotations.ApiOperation;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    @Api(tags = "示例控制器")
    public class AccountController {
    
        @GetMapping("/hello")
        @ApiOperation("获取欢迎消息")
        public String hello() {
            return "Hello, World!";
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    访问 Swagger UI:启动你的应用程序后,你可以通过浏览器访问 Swagger UI,通常在 http://localhost:8080/swagger-ui.html。

    这些步骤将帮助你在 Spring Boot 项目中整合 Swagger,以便生成和展示 API 文档。你可以根据你的项目需求和喜好进行更多的配置和定制。

    在这里插入图片描述

    如果想展示实体类文档
    实体类

    @Data
    @ApiModel(value = "User", description = "用户实体类")
    public class People {
    
        @ApiModelProperty(value = "用户Name, required = true")
        private String name;
    
        @ApiModelProperty(value = "用户ID", required = true)
        private Long id;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    controller层相应代码

    @RestController
    @Api(tags = "示例控制器")
    public class AccountController {
    
    
        @ApiOperation(value = "获取用户信息", notes = "根据用户ID获取用户信息")
        @ApiResponses(value = {
                @ApiResponse(code = 200, message = "成功", response = People.class),
                @ApiResponse(code = 404, message = "未找到用户")
        })
        @GetMapping("/user/{id}")
        public ResponseEntity<People> getUserInfo(@PathVariable Long id) {
    
            return null;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    总结

    总之,Swagger 是一种提高 API 开发和维护效率的工具,它使开发者能够更轻松地构建、测试和文档化 API,并提供了可视化的交互界面,以改进开发流程和加速 API 的采用。

  • 相关阅读:
    高阶数据结构:二叉搜索树
    界面控件DevExtreme JS & ASP.NET Core 2024年度产品规划预览(一)
    torch.argmax()使用
    备赛蓝桥杯-算法-动态规划
    什么是多进程-多线程-多协程 ---- 文件系统
    液压比例方向阀放大器组装
    Python+Pytest+Allure+Git+Jenkins数据驱动接口自动化测试框架
    字符串匹配——KMP算法
    Vue高级篇--实现前后端分离
    【SEC 学习】Vim 的基本使用
  • 原文地址:https://blog.csdn.net/futurn_hero/article/details/133884187