Api管家系列(一):初探

时间:2023-03-09 16:46:00
Api管家系列(一):初探

前段时间发现一个很好用的API管理工具--API管家,用了一段时间,已经感觉离不开了,抱着分享使我快乐的想法,因为刚开始用的时候随便写过一篇简介,不是很详细,所以现在就重新写,把我这段时间使用的经验和大家一起讨论。

进入正题:打开浏览器,输入www.apigj.com 来到首页,接下来自然是登录或注册了

Api管家系列(一):初探

创建一个新账号,成功后会来到项目列表界面,会提示是否需要创建示例项目,点击确定后,就会创建一个新的项目出来

Api管家系列(一):初探

Api管家系列(一):初探

点击进入示例项目,示例项目一共有1个文件夹和5个接口,可以看到没有经过测试,都在未测试状态

Api管家系列(一):初探

点击某个接口,接口文档就显示出来了,有请求的URL,版本号,描述,请求方式,参数等等,左上角是对接口的操作按扭

Api管家系列(一):初探

点击编辑,可以看到编辑接口的界面,注意我红色圈出的位置是个tab控件,可以点击切换编辑的内容

Api管家系列(一):初探

把请求Class的openid直接删除,点击保存

Api管家系列(一):初探

回到接口文档页面,可以看到openid已经从请求Class里消失了,这里还有个特别的功能,我用红色图出了,点击会生成mock参数

Api管家系列(一):初探

Api管家系列(一):初探

点击左上角的生成按扭,这里就是Api管家最大特色了,可以直接生成代码

Api管家系列(一):初探

点击生成代码,会出现语言的选项,一共9种语言,对我来说是足够用了

Api管家系列(一):初探

举个栗子,我常用的Java(咖啡杯),又分3种引入包

Api管家系列(一):初探

选择Gson

Api管家系列(一):初探

请求和返回的Class文件就直接生成了,值的提一下的是ResSendSms继承于ResCommon,这个是在文档中编辑的,下次再展开细说编辑的功能

上生成的代码,里面自动带了文档中的描述作为注释,实在是太方便了

 /***
* @接口:SendSMS
* @URL:host + /sendsms
* @编码:www.apigj.com
* @版本号:1.1
***/ import java.io.IOException;
import java.util.ArrayList;
import java.util.List; import com.google.gson.TypeAdapter;
import com.google.gson.annotations.JsonAdapter;
import com.google.gson.annotations.SerializedName;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter; @JsonAdapter(ReqSendSms.ReqSendSmsTypeAdapter.class)
public class ReqSendSms {
// 类型版本,用于查询类型是否已过期
public static final int Version = 2; /***
* 参数描述:用户手机号
* 是否可为空:否
***/
@SerializedName("mobile")
private String mobile;
public String getMobile(){
return mobile;
}
public void setMobile(String mobile){
this.mobile = mobile;
} /***
* 检查类型完整性
*
* @return true代表类型通过完整性检查
***/
public boolean checkVarRequire() {
if(getMobile() == null){
System.out.println("Lake of (mobile)");
return false;
}
return true;
} public static class ReqSendSmsTypeAdapter<T> extends TypeAdapter<ReqSendSms> {
@Override
public void write(JsonWriter out, ReqSendSms value) throws IOException {
out.beginObject();
writeOBJ(out, (ReqSendSms)value);
out.endObject();
} protected void writeOBJ(JsonWriter out, ReqSendSms value) throws IOException {
out.name("mobile").value(value.getMobile());
} @Override
public ReqSendSms read(JsonReader in) throws IOException {
ReqSendSms res = new ReqSendSms();
in.beginObject();
while (in.hasNext()) {
String propertyName = in.nextName();
if(!readOBJ(res, in, propertyName)) {
in.skipValue();
}
}
in.endObject();
return res;
} protected boolean readOBJ(ReqSendSms res, JsonReader in, String propertyName) throws IOException {
if (propertyName.equals("mobile")) {
try {
res.setMobile(in.nextString());
} catch(IllegalStateException e) {in.skipValue();}
return true;
}
return false;
} } }

请求类 ReqSendSms.java

 /***
* @接口:SendSMS
* @URL:host + /sendsms
* @编码:www.apigj.com
* @版本号:1.1
***/ import java.io.IOException;
import java.util.ArrayList;
import java.util.List; import com.google.gson.TypeAdapter;
import com.google.gson.annotations.JsonAdapter;
import com.google.gson.annotations.SerializedName;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter; @JsonAdapter(ResCommon.ResCommonTypeAdapter.class)
public class ResCommon {
// 类型版本,用于查询类型是否已过期
public static final int Version = 1; /***
* 参数描述:返回码
* 是否可为空:否
***/
@SerializedName("code")
private Integer code;
public Integer getCode(){
return code;
}
public void setCode(Integer code){
this.code = code;
} /***
* 参数描述:返回提示
* 是否可为空:是
***/
@SerializedName("msg")
private String msg;
public String getMsg(){
return msg;
}
public void setMsg(String msg){
this.msg = msg;
} /***
* 检查类型完整性
*
* @return true代表类型通过完整性检查
***/
public boolean checkVarRequire() {
if(getCode() == null){
System.out.println("Lake of (code)");
return false;
}
return true;
} public static class ResCommonTypeAdapter<T> extends TypeAdapter<ResCommon> {
@Override
public void write(JsonWriter out, ResCommon value) throws IOException {
out.beginObject();
writeOBJ(out, (ResCommon)value);
out.endObject();
} protected void writeOBJ(JsonWriter out, ResCommon value) throws IOException {
out.name("code").value(value.getCode());
out.name("msg").value(value.getMsg());
} @Override
public ResCommon read(JsonReader in) throws IOException {
ResCommon res = new ResCommon();
in.beginObject();
while (in.hasNext()) {
String propertyName = in.nextName();
if(!readOBJ(res, in, propertyName)) {
in.skipValue();
}
}
in.endObject();
return res;
} protected boolean readOBJ(ResCommon res, JsonReader in, String propertyName) throws IOException {
if (propertyName.equals("code")) {
try {
res.setCode(in.nextInt());
} catch(IllegalStateException e) {in.skipValue();}
return true;
}
else if (propertyName.equals("msg")) {
try {
res.setMsg(in.nextString());
} catch(IllegalStateException e) {in.skipValue();}
return true;
}
return false;
} } }

返回类的父类 ResCommon.java

 /***
* @接口:SendSMS
* @URL:host + /sendsms
* @编码:www.apigj.com
* @版本号:1.1
***/ import java.io.IOException;
import java.util.ArrayList;
import java.util.List; import com.google.gson.TypeAdapter;
import com.google.gson.annotations.JsonAdapter;
import com.google.gson.annotations.SerializedName;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter; @JsonAdapter(ResSendSms.ResSendSmsTypeAdapter.class)
public class ResSendSms extends ResCommon {
// 类型版本,用于查询类型是否已过期
public static final int Version = 2; /***
* 参数描述:事件ID,记录接收短信的用户
* 是否可为空:否
***/
@SerializedName("eventid")
private String eventid;
public String getEventid(){
return eventid;
}
public void setEventid(String eventid){
this.eventid = eventid;
} /***
* 检查类型完整性
*
* @return true代表类型通过完整性检查
***/
public boolean checkVarRequire() {
if(!super.checkVarRequire()){
return false;
}
if(getEventid() == null){
System.out.println("Lake of (eventid)");
return false;
}
return true;
} public static class ResSendSmsTypeAdapter<T> extends ResCommonTypeAdapter<ResSendSms> {
@Override
public void write(JsonWriter out, ResCommon value) throws IOException {
out.beginObject();
writeOBJ(out, (ResSendSms)value);
out.endObject();
} protected void writeOBJ(JsonWriter out, ResSendSms value) throws IOException {
super.writeOBJ(out, value);
out.name("eventid").value(value.getEventid());
} @Override
public ResSendSms read(JsonReader in) throws IOException {
ResSendSms res = new ResSendSms();
in.beginObject();
while (in.hasNext()) {
String propertyName = in.nextName();
if(!readOBJ(res, in, propertyName)) {
in.skipValue();
}
}
in.endObject();
return res;
} protected boolean readOBJ(ResSendSms res, JsonReader in, String propertyName) throws IOException {
if (super.readOBJ(res, in, propertyName)) {
return true;
}else
if (propertyName.equals("eventid")) {
try {
res.setEventid(in.nextString());
} catch(IllegalStateException e) {in.skipValue();}
return true;
}
return false;
} } }

返回类 ResSendSms.java

具体请求返回代码还是要自己写的,希望API管家继续完善,以后也可以自动生成把URL和请求方法,请求头包括进去,那就更完美了(懒癌又发作了)。。。