接口测试工具Postman和接口文档Swagger了解

接口测试工具Postman

什么是Postman

写完以后,前端以后不是我们写,就算是我们写也应该先测试好接口,才写前端。所以要先测试,对于get请求可以使用浏览器地址访问,但是其他请求就不行。需要一些接口测试工具postman就是其中的一个,接口来我们就使用它来测试我们的登录接口
下载API路径(https://www.getpostman.com/)

使用

在这里插入图片描述

接口文档Swagger

在这里插入图片描述只需要在你的接口的项目中做一点配置:
导入swagger的依赖和一点配置,就可以通过浏览器访问接口

使用
1.导jar包
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.9.2</version>
        </dependency>
2.定义配置类
@Configuration //用于定义配置类
@EnableSwagger2
public class Swagger2 {
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("cn.itsource.aigou.controller"))//扫描接口的包路径
                //包:就是自己接口的包路径
                .paths(PathSelectors.any())
                .build();
    }
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("商品系统api")//接口文档名字
                .description("商品系统接口文档说明") //说明
                .contact(new Contact("wbtest", "", "wenbing@itsource.cn"))//邮件
                .version("1.0")// 版本
                .build();
    }
}

在这里插入图片描述

3.访问本项目地址:http://127.0.0.1:8001/swagger-ui.html
网关配置

前端开发时查询Swagger要记住指定的端口号,一旦开发模块过得就很麻烦,如果用网关访问的话只需要记住网关的端口,就方便开发,
注意:网关的swagger配置只是集合一下单个模块的swagger文档,所以每个接口都要进行以上配置

1.导jar包
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.9.2</version>
        </dependency>
2.定义配置类
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
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 //定义一个bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo());
    }
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("分布式购物系统")
                .description("购物系统接口文档说明")
                .termsOfServiceUrl("http://localhost:8081")
                .contact(new Contact("wbtest", "", "wenbing@itsoruce.cn"))
                .version("1.0")
                .build();
    }
}
3.yml配置
server:
  port: 9527
spring:
  application:
    name: MICROSERVICE-ZUUL-GATEWAY
eureka:
  client:
    service-url:
      defaultZone: http://localhost:7001/eureka
  instance:
    instance-id: gateway-9527.com
    prefer-ip-address: true #显示ip
zuul:
  routes:
    #========================== 通过网关zuul访问不同的模块 =====================
    myUser.serviceId: microservice-user-dev #这是调用满足条件的服务名,注意要小写MICROSERVICE-USER-DEV
    myUser.path: /user/** #这是所有路径前的通配
    #======================================================================
    myProduct.serviceId: aigou-product #这是调用满足条件的服务名,注意要小写
    myProduct.path: /product/** #这是所有路径前的通配
    #======================================================================
  ignored-services: "*" #用*来通配符,忽略从9527端口通过服务名来调用
  prefix: "/services" #这是所有路径的前缀
4.创建一个类实现SwaggerResourcesProvider
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;
import springfox.documentation.swagger.web.SwaggerResource;
import springfox.documentation.swagger.web.SwaggerResourcesProvider;

import java.util.ArrayList;
import java.util.List;

@Component
@Primary
public class DocumentationConfig implements SwaggerResourcesProvider {
    @Override
    public List<SwaggerResource> get() {
        List resources = new ArrayList<>();
        //aigou网关前缀+“/”+user网关路径+"/"+swagger的路径 
        //以后增加了接口就在这配置就ok
        resources.add(swaggerResource("用户系统", "/aigou/user/v2/api-docs", "2.0"));
        resources.add(swaggerResource("用户系统2", "/aigou/product/v2/api-docs", "2.0"));
        return resources;
    }

    private SwaggerResource swaggerResource(String name, String location, String version) {
        SwaggerResource swaggerResource = new SwaggerResource();
        swaggerResource.setName(name);
        swaggerResource.setLocation(location);
        swaggerResource.setSwaggerVersion(version);
        return swaggerResource;
    }
}
5.访问 http://127.0.0.1:9527/swagger-ui.html

在这里插入图片描述

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值