Swagger和postman

本文详细介绍了Swagger在API文档自动生成中的作用及其配置步骤,同时阐述了Postman作为HTTP请求测试工具的重要性,适合API开发及测试人员阅读。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

接口规范Swagger

1.为什么需要swagger

1)接口测试人员要通过接口描述测试接口 --黑盒测试
2)前端开发人员要通过接口描述使用接口.

2.swagger的作用

通过后端代码产生能够让前台开发或测试人员能够看懂的文档

3.Swagger使用步骤
3.1在crmyang_web模块中引入相关jar包
<!-- swagger引入包-->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>${springfox.version}</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>${springfox.version}</version>
        </dependency>
3.2在crmyang_web模块中新建config类

在这里插入图片描述
codeDemo

package cn.itsource.crmyang.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

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相当于写一个applicationContext.xml
@Configuration
@EnableWebMvc//开关配置 开启webmvc
@EnableSwagger2//开启swagger配置
@ComponentScan(basePackages="cn.itsource.crmyang.web.Controller")
public class SwaggerConfig {
    //相当于<bean id="" class="">  相当于注入bean
    @Bean
    public Docket api(){
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(this.apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("cn.itsource.crmyang.web.Controller"))
                .paths(PathSelectors.any())
                .build();
    }

    //生成的内容里面 产生接口信息
    private ApiInfo apiInfo(){
        @SuppressWarnings("deprecation")
        ApiInfo info=new ApiInfo(
                "杨怀强的接口文档",
                "描述信2020-2-25",
                "版本号1.0",
                "www.itsource.cn",
                "联系人:杨怀强",
                "2",
                "www.itsource.cn");
        return info;
    }
}

3.3在applicationContext-mvc.xml中扫描这个包
    <!-- 把swagger交给spring-->
    <context:component-scan base-package="cn.itsource.crmyang.config"></context:component-scan>
3.4运行测试

http://localhost/swagger-ui.html在这里插入图片描述

接口测试postman

1.为什么需要postman接口测试

我们基于springmvc写的controller对于前端来说就是接口,而且都是通过http协议访问,那后台写完后怎么测试呢?浏览器,只支持get。 要使用一些能够发送http各种请求的工具,其中postman就是很重要的一个。

2.什么是postman

就是一个工具,可以来发送各种http请求,可以用它来测试http协议接口.
postman就是httm协议接口测试工具

3.使用过程

3.1下载postman软件并注册
**3.2**
3.2get请求测试
在这里插入图片描述
3.3json格式的请求
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值