zoukankan      html  css  js  c++  java
  • 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可以查看相应的结果

      

     

  • 相关阅读:
    HTML5 的成长之路
    黑马程序员——JAVA基础之数组
    黑马程序员——JAVA基础之函数,重载,内存结构
    黑马程序员——JAVA基础之程序控制流结构之循环结构,循环嵌套
    黑马程序员——JAVA基础之程序控制流结构之判断结构,选择结构
    黑马程序员——JAVA基础之语法、命名规则
    黑马程序员——JAVA基础之常用DOS命令和环境变量的配置
    Android应用开发高效工具集1---ant构建简单Android项目
    10个你能参与并学习的Java开源项目
    Anroid 异常:is not valid; is your activity running?
  • 原文地址:https://www.cnblogs.com/HelloM/p/14248250.html
Copyright © 2011-2022 走看看