pull/3/head
tanghc 4 years ago
parent c8598b5a0e
commit 8239a536b9
  1. 17
      sop-sdk/sdk-java/src/main/java/com/gitee/sop/sdk/client/OpenClient.java
  2. 2
      sop-sdk/sdk-java/src/main/java/com/gitee/sop/sdk/request/DemoFileUploadRequest.java
  3. 2
      sop-sdk/sdk-java/src/main/java/com/gitee/sop/sdk/request/GetStoryRequest.java
  4. 2
      sop-sdk/sdk-java/src/main/java/com/gitee/sop/sdk/response/BaseResponse.java
  5. 8
      sop-sdk/sdk-java/src/test/java/com/gitee/sop/sdk/SdkTest.java

@ -2,6 +2,7 @@ package com.gitee.sop.sdk.client;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;
import com.gitee.sop.sdk.common.DataNameBuilder; import com.gitee.sop.sdk.common.DataNameBuilder;
import com.gitee.sop.sdk.common.OpenConfig; import com.gitee.sop.sdk.common.OpenConfig;
import com.gitee.sop.sdk.common.RequestForm; import com.gitee.sop.sdk.common.RequestForm;
@ -36,17 +37,17 @@ public class OpenClient {
/** /**
* 接口请求url * 接口请求url
*/ */
private String url; private final String url;
/** /**
* 平台提供的appId * 平台提供的appId
*/ */
private String appId; private final String appId;
/** /**
* 开放平台提供的私钥 * 开放平台提供的私钥
*/ */
private String privateKey; private final String privateKey;
/** /**
* 开放平台提供的公钥 * 开放平台提供的公钥
@ -56,17 +57,17 @@ public class OpenClient {
/** /**
* 配置项 * 配置项
*/ */
private OpenConfig openConfig; private final OpenConfig openConfig;
/** /**
* 请求对象 * 请求对象
*/ */
private OpenRequest openRequest; private final OpenRequest openRequest;
/** /**
* 节点处理 * 节点处理
*/ */
private DataNameBuilder dataNameBuilder; private final DataNameBuilder dataNameBuilder;
/** /**
* 构建请求客户端 * 构建请求客户端
@ -193,7 +194,7 @@ public class OpenClient {
protected <T extends BaseResponse> T parseResponse(String resp, BaseRequest<T> request) { protected <T extends BaseResponse> T parseResponse(String resp, BaseRequest<T> request) {
String method = request.getMethod(); String method = request.getMethod();
String rootNodeName = dataNameBuilder.build(method); String rootNodeName = dataNameBuilder.build(method);
JSONObject jsonObject = JSON.parseObject(resp); JSONObject jsonObject = JSON.parseObject(resp, Feature.OrderedField);
String errorResponseName = this.openConfig.getErrorResponseName(); String errorResponseName = this.openConfig.getErrorResponseName();
boolean errorResponse = jsonObject.containsKey(errorResponseName); boolean errorResponse = jsonObject.containsKey(errorResponseName);
if (errorResponse) { if (errorResponse) {
@ -210,7 +211,7 @@ public class OpenClient {
} }
} }
T t = data.toJavaObject(request.getResponseClass()); T t = data.toJavaObject(request.getResponseClass());
t.setBody(data.toJSONString()); t.setBody(jsonObject.getString(rootNodeName));
return t; return t;
} }

@ -8,6 +8,6 @@ import com.gitee.sop.sdk.response.DemoFileUploadResponse;
public class DemoFileUploadRequest extends BaseRequest<DemoFileUploadResponse> { public class DemoFileUploadRequest extends BaseRequest<DemoFileUploadResponse> {
@Override @Override
protected String method() { protected String method() {
return "demo.file.upload"; return "file.upload";
} }
} }

@ -5,7 +5,7 @@ import com.gitee.sop.sdk.response.GetStoryResponse;
public class GetStoryRequest extends BaseRequest<GetStoryResponse> { public class GetStoryRequest extends BaseRequest<GetStoryResponse> {
@Override @Override
protected String method() { protected String method() {
return "alipay.story.find"; return "story.get";
} }
} }

@ -22,6 +22,8 @@ import lombok.Setter;
@Getter @Getter
public abstract class BaseResponse { public abstract class BaseResponse {
@JSONField(name = "request_id")
private String requestId;
private String code; private String code;
private String msg; private String msg;
@JSONField(name = "sub_code") @JSONField(name = "sub_code")

@ -48,19 +48,19 @@ public class SdkTest extends TestCase {
if (response.isSuccess()) { if (response.isSuccess()) {
// 返回结果 // 返回结果
System.out.println(String.format("成功!response:%s\n响应原始内容:%s", System.out.println(String.format("response:%s",
JSON.toJSONString(response), response.getBody())); JSON.toJSONString(response)));
} else { } else {
System.out.println("错误,subCode:" + response.getSubCode() + ", subMsg:" + response.getSubMsg()); System.out.println("错误,subCode:" + response.getSubCode() + ", subMsg:" + response.getSubMsg());
} }
} }
// 懒人版,如果不想添加Request,Response,Model。可以用这种方式,返回全部是String,后续自己处理json // 懒人版,如果不想添加Request,Response,可以用这种方式,返回全部是String,后续自己处理json
@Test @Test
public void testLazy() { public void testLazy() {
// 创建请求对象 // 创建请求对象
CommonRequest request = new CommonRequest("alipay.story.find"); CommonRequest request = new CommonRequest("story.get");
// 请求参数 // 请求参数
Map<String, Object> bizModel = new HashMap<>(); Map<String, Object> bizModel = new HashMap<>();
bizModel.put("name", "白雪公主"); bizModel.put("name", "白雪公主");

Loading…
Cancel
Save