eclipse下(idea原理一样)
- 下载或clone项目https://gitee.com/durcframework/easyopen.git 下载zip
- eclipse右键import… -> Exsiting Maven Projects。选择easyopen目录
- 导入到eclipse后会有三个工程,等待相关jar包下载。
- 全部jar下载完毕后,启动easyopen-server项目,由于是spring-boot项目,直接运行EasyopenSpringbootApplication.java即可
- 在easyopen-sdk中找到SdkTest.java测试用例,运行单元测试。
编写业务类
- 新建一个java类名为HelloworldApi,并加上@ApiService注解
加上@ApiService注解后这个类就具有了提供接口的能力。
@ApiService
public class HelloWorldApi {
}
- 在类中添加一个方法
@Api(name = "hello")
public String helloworld() {
return "hello world";
}
这个方法很简单,就返回了一个字符串,方法被@Api标记,表示对应的接口,name是接口名。
到此,一个业务方法就写完了,接下来是编写sdk并测试。
编写SDK并测试
此过程在easyopen-sdk中进行。
- 新建Response响应类
public class HelloResp extends BaseResp<String> {
}
BaseResp的泛型参数指定返回体类型,这里指定String
- 新建Request请求类
public class HelloReq extends BaseNoParamReq<HelloResp> {
public HelloReq(String name) {
super(name);
}
}
- 编写单元测试
public class HelloTest extends TestCase {
String url = "http://localhost:8080/api";
String appKey = "test";
String secret = "123456";
// 创建一个实例即可
OpenClient client = new OpenClient(url, appKey, secret);
@Test
public void testGet() throws Exception {
HelloReq req = new HelloReq("hello"); // hello对应@Api中的name属性,即接口名称
HelloResp result = client.request(req); // 发送请求
if (result.isSuccess()) {
String resp = result.getBody();
System.out.println(resp); // 返回hello world
} else {
throw new RuntimeException(result.getMsg());
}
}
}
这样,一个完整的接口就写完了。