• swagger简介


    一、swagger****的作用

    根据在代码中使用自定义的注解来生成接口文档,这个在前后端分离的项目中很重要。这样做的好处是 在开发接口时可以通过swagger 将接口文档定义好,同时也方便以后的维护。

    在没有swagger之前,我们可以使用word,excel等功能来书写接口定义文档,但又有一个弊端,即: 在接口发送改变时需要及时的同步接口文档,否则实际的接口与接口文档不相符,则接口文件就失去了作用,甚至会起到反作用。

    二、swagger****的优点

    号称时最流行的 API 框架

    接口文档在线生成,避免同步的麻烦

    可以支持在线对接口执行测试

    支持多语言

    三、SpringBoot集成swagger

    1.导入依赖

    io.springfox

    springfox-swagger2

    2.9.2

    io.springfox

    springfox-swagger-ui

    2.9.2

    2. swagger配置类

    package com.zking.mini_program.config;
    
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.web.bind.annotation.RestController;
    import springfox.documentation.builders.ApiInfoBuilder;
    import springfox.documentation.builders.PathSelectors;
    import springfox.documentation.builders.RequestHandlerSelectors;
    import springfox.documentation.service.ApiInfo;
    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).apiInfo(apiInfo())
                    .select() .apis(RequestHandlerSelectors.withClassAnnotation(RestController.class))
                    .paths(PathSelectors.ant("/swaggerdemo/**"))
                    .build();
        }
        private ApiInfo apiInfo(){
            return new ApiInfoBuilder()
                    .title("SwaggerDemo API DOC")
                    .description("SwaggerDemo API DOC")
                    .version("1.0") .termsOfServiceUrl("https://www.baidu.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

    3.写好后启动项目

    访问路径

    Swagger UI

    4.携带数据发送请求

    5.响应结果

    四、常用注解

    例如:在类上注解

    html中

    先自我介绍一下,小编13年上师交大毕业,曾经在小公司待过,去过华为OPPO等大厂,18年进入阿里,直到现在。深知大多数初中级java工程师,想要升技能,往往是需要自己摸索成长或是报班学习,但对于培训机构动则近万元的学费,着实压力不小。自己不成体系的自学效率很低又漫长,而且容易碰到天花板技术停止不前。因此我收集了一份《java开发全套学习资料》送给大家,初衷也很简单,就是希望帮助到想自学又不知道该从何学起的朋友,同时减轻大家的负担。添加下方名片,即可获取全套学习资料哦

  • 相关阅读:
    你的项目需要自动化测试吗?
    【广度优先搜索】leetcode 107. 二叉树的层序遍历 II
    【MySQL中auto_increment有什么作用?】
    用队列实现栈(C语言版本)
    蓝桥杯第十一届电子类单片机组程序设计
    基于MIMO+16QAM系统的VBLAST译码算法matlab仿真
    异地恋挺痛苦的
    Java版工程行业管理系统源码-专业的工程管理软件-提供一站式服务
    防止砍单、封号:亚马逊、沃尔玛测评方案优化建议
    AWS认证SAA-C03每日一题
  • 原文地址:https://blog.csdn.net/Ajekseg/article/details/126080256