• swagger前端兄弟的调试工具


    swagger用途

      swagger是一个接口测试工具,前端程序员和后台程序是分离开发的,当前端程序员需要向后台进行交互,那么就需要一个工具去访问路径。发送一个请求,请求会携带一个或多个参数,然后接口会查看到后台查询到的数据。后台程序员一般都会用postman,这俩是一个意思。swagger还有个好处,就是页面好看。

    依赖配置

      在父级pom.xml导入依赖,依赖下载真的很慢,还是要耐心等待

            <!--
    为了减少程序员撰写文档时间,提高生产力,swagger2应运而生,使用swagger2可以减少编写过多文档,
    只要通过代码就能生成文档API,提供前端人员用于测试
    -->
            <dependency>
                <groupId>io.springfox</groupId>
                <artifactId>springfox-swagger2</artifactId>
                <version>2.4.0</version>
            </dependency>
            <dependency>
                <groupId>io.springfox</groupId>
                <artifactId>springfox-swagger-ui</artifactId>
                <version>2.4.0</version>
            </dependency>

      在application.properties中配置访问端口,这个是配置文件,所以放在api模块中

    #配置服务端口
    server.port=8088
    server.tomcat.uri-encoding=utf-8
    server.max-http-header-size=80KB

    启动类配置扫描包

    //启动类09
    @SpringBootApplication  //扫描所有包
    @MapperScan(basePackages = "com.wt.mapper")
    @ComponentScan(basePackages = {"com.wt"})
    public class Application {
        public static void main(String[] args) {
            SpringApplication.run(Application.class);
        }
    }

    配置swagger接口

      启动类和接口的放置地方

    package com.wt.config;
    
    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 Swagger2 {
    //   访问路径
        //http://localhost:8088/swagger-ui.html 原ui路径
        //配置swagger2核心配置 docket
        @Bean
        public Docket createRestApi() {
            return new Docket(DocumentationType.SWAGGER_2)  //指定api类型为swagger2
                    .apiInfo(apiInfo())//用于定义api文档汇总信息
                    .select()
                    .apis(RequestHandlerSelectors.basePackage("com.wt.controller")) //指定controller包
                    .paths(PathSelectors.any()) //所有controller
                    .build();
        }
    
        private ApiInfo apiInfo() {
            return new ApiInfoBuilder() //接口的信息建造者
            .title("裂变平台,接口api")   //文档页标题
            .contact(new Contact("wt",
                    "http://www.wt.com",
                    "xxxxyyyy@qq.com"))   //联系人信息
                    .description("专为微信裂变平台提供的api文档")  //详细信息
                    .version("1.0.1")  //文档版本号
                    .termsOfServiceUrl("https://www.wt.com") //网站地址
                    .build();
        }
    }

    测试

      测试是使用的properties中的端口:http://localhost:8088/swagger-ui.html,如果接口中有传递参数的话,这里也可以传递参数,在后面result可以查看相应的结果

      

     

  • 相关阅读:
    【模拟7.22】方程的解(拓展欧几里德)
    Dijkstra堆优化模板
    7.19考后总结
    《机器学习实战》读书笔记
    从K近邻算法、距离度量谈到KD树、SIFT+BBF算法
    《c程序设计语言》-3.2 字符串转换
    《c程序设计语言》-3.1 判断语句多少影响时间
    《c程序设计语言》-2.10 不用if-else 转换大小写
    《c程序设计语言》-2.9
    《c程序设计语言》-2.6~2.8
  • 原文地址:https://www.cnblogs.com/HelloM/p/14248250.html
Copyright © 2020-2023  润新知