完善SDK

This commit is contained in:
六如
2025-02-02 21:28:05 +08:00
parent e09c0106a0
commit aa8f044c70
6 changed files with 113 additions and 133 deletions

View File

@@ -1,33 +1,35 @@
# sdk-java
开放平台把接口开发完毕后一般需要开发对应的SDK提供给ISV。SOP提供了一个基础的SDK开发包
sdk for java
开发者可以在此基础上做开发就拿sdk-java来说具体步骤如下
SDK只依赖了三个jar包
## sdk-java
SDK依赖了三个jar包
- okhttp.jar 用于网络请求
- fastjson.jar 用于json处理
- commons-logging.jar 日志处理
## 接口封装步骤
+ okhttp.jar 用于网络请求
+ fastjson.jar 用于json处理
+ commons-logging.jar 日志处理
### 接口封装步骤
比如获取故事信息接口
- 接口名:alipay.story.find
- 版本号1.0
- 参数:name 故事名称
- 返回信息
+ 接口名story.get
+ 版本号1.0
+ 参数:id
+ 返回信息
```
```json
{
"alipay_story_find_response": {
"msg": "Success",
"code": "10000",
"name": "白雪公主",
"id": 1,
"gmtCreate": 1554193987378
},
"sign": "xxxxx"
"subCode": "",
"subMsg": "",
"code": "0",
"msg": "success",
"data": {
"addTime": "2024-11-08 10:21:23",
"name": "乌鸦喝水",
"id": 1
}
}
```
@@ -36,31 +38,31 @@ SDK只依赖了三个jar包
1.在`model`包下新建一个类,定义业务参数
```java
@Data
public class GetStoryModel {
@JSONField(name = "name")
private String name;
private Integer id;
}
```
2.在`response`包下新建一个返回类GetStoryResponse,继承`BaseResponse`
2.在`response`包下新建一个返回类GetStoryResponse
里面填写返回的字段
```
```plain
@Data
public class GetStoryResponse extends BaseResponse {
public class GetStoryResponse {
private Long id;
private String name;
private Date gmtCreate;
private Date addTime;
}
```
3.在`request`包下新建一个请求类,继承`BaseRequest`
BaseRequest中有个泛型参数`GetStoryResponse`类,表示这个请求对应的返回类。
BaseRequest中有个泛型参数`GetStoryResponse`类,表示这个请求对应的返回类。
重写`method()`方法,填接口名。
如果要指定版本号,可重写`version()`方法,或者后续使用`request.setVersion(version)`进行设置
@@ -69,14 +71,24 @@ BaseRequest中有个泛型参数填`GetStoryResponse`类,表示这个请求
public class GetStoryRequest extends BaseRequest<GetStoryResponse> {
@Override
protected String method() {
return "alipay.story.find";
return "story.get";
}
}
}
```
## 使用方式
可重写`getRequestMethod()`方法指定HTTP请求method默认是POST。
```java
@Override
protected RequestMethod getRequestMethod() {
return RequestMethod.GET;
}
```
**建议读请求用GET写请求用POST**
### 使用方式
```java
String url = "http://localhost:8081/api";
String appId = "2019032617262200001";
@@ -85,55 +97,25 @@ String privateKey = "你的私钥";
// 声明一个就行
OpenClient client = new OpenClient(url, appId, privateKey);
// 标准用法
@Test
public void testGet() {
// 创建请求对象
GetStoryRequest request = new GetStoryRequest();
// 请求参数
GetStoryModel model = new GetStoryModel();
model.setName("白雪公主");
model.setId(1);
request.setBizModel(model);
// 发送请求
GetStoryResponse response = client.execute(request);
Result<GetStoryResponse> result = client.execute(request);
if (response.isSuccess()) {
if (result.isSuccess()) {
GetStoryResponse response = result.getData();
// 返回结果
System.out.println(response);
System.out.println(String.format("response:%s",
JSON.toJSONString(response)));
} else {
System.out.println(response);
}
}
```
## 使用方式2(懒人版)
如果不想添加Request,Response,Model。可以用这种方式返回body部分是字符串后续自己处理
body对应的是alipay_story_find_response部分
```java
@Test
public void testLazy() {
// 创建请求对象
CommonRequest request = new CommonRequest("alipay.story.find");
// 请求参数
Map<String, Object> bizModel = new HashMap<>();
bizModel.put("name", "白雪公主");
request.setBizModel(bizModel);
// 发送请求
CommonResponse response = client.execute(request);
if (response.isSuccess()) {
// 返回结果body对应的是alipay_story_find_response部分
String body = response.getBody();
JSONObject jsonObject = JSON.parseObject(body);
System.out.println(jsonObject);
} else {
System.out.println(response);
System.out.println("错误subCode:" + result.getSubCode() + ", subMsg:" + result.getSubMsg());
}
}
```