接口规范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.2get请求测试
3.3json格式的请求