• 每日一记8.6


    swagger2和spring boot的整合

      在项目中接口文档是前后端对接的重要工具,而作为编写文档的后端人员每次在写完接口还需要整理这些文档,无疑是降低了工作的效率。这时如果有一款可以在编写代码的同时就顺便把文档写了的插件或者工具就会方便许多。swagger2就是这样一款API开发框架。

      swagger2是基于注解的方式,侵入接口中,动态的生成接口文档,方便前后端人员的查看。

    swagger2的配置

    1、swagger2是一个api框架,所以,在项目中使用他需要导入他的jar包

    在pom文件中添加依赖

    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger2</artifactId>
        <version>2.6.1</version>
    </dependency>
    
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger-ui</artifactId>
        <version>2.6.1</version>
    </dependency>

    2、创建Swagger2配置类

    @Configuration
    public class SwaggerConfig {
        
        @Bean
        public Docket createRestApi() {
            return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select()
                    .apis(RequestHandlerSelectors.basePackage("com.example.demo.controller")).paths(PathSelectors.any())
                    .build();
        }
        /**
         * 在接口文档中,添加的一些标题和描述
         */
        private ApiInfo apiInfo() {
            return new ApiInfoBuilder().title("springboot和swagger整合").description("接口文档")
                    .termsOfServiceUrl("测试url").contact("taizhu").version("1.0").build();
        }
    }

    3、在启动类上添加注解@EnableSwagger2


    到此,我们的swagger就配置完毕了。启动项目,在地址栏输入:http://localhost:8080/swagger-ui.html#/就可以看到swagger动态生成的api文档。

    swagger2的注解

    swagger2是基于注解开发的,所以每一个注解有什么含义,我们必须掌握。

    在CSDN上看到的比较全的注解以及解释。https://blog.csdn.net/weixin_41846320/article/details/82970204

     

  • 相关阅读:
    Relativity 01: Physical Meaning of Geometrical Propositions
    Algo 2: Asymptotic Order of Growth
    CShop Project : BeanUtils工具的使用
    137 __getattribute__
    134 isinstance和issubclass
    135 反射(hasattr和getattr和setattr和delattr)
    133 面向对象进阶实战之选课系统
    132 面向对象进阶小结
    131 类和对象的绑定方法及非绑定方法
    130 类的property特性
  • 原文地址:https://www.cnblogs.com/sunshine-2018/p/11306812.html
Copyright © 2020-2023  润新知