mirror of
https://gitee.com/durcframework/SOP.git
synced 2025-08-11 21:57:56 +08:00
修复SDKPOS异常
This commit is contained in:
@@ -17,7 +17,7 @@
|
||||
<dependency>
|
||||
<groupId>com.squareup.okhttp3</groupId>
|
||||
<artifactId>okhttp</artifactId>
|
||||
<version>3.10.0</version>
|
||||
<version>3.14.0</version>
|
||||
</dependency>
|
||||
|
||||
<!-- json处理 -->
|
||||
|
@@ -27,27 +27,25 @@ public class OpenClient {
|
||||
|
||||
private static final char DOT = '.';
|
||||
private static final char UNDERLINE = '_';
|
||||
public static final String GATEWAY_CODE_NAME = "code";
|
||||
public static final String GATEWAY_MSG_NAME = "msg";
|
||||
public static final String DATA_SUFFIX = "_response";
|
||||
|
||||
private String url;
|
||||
private String appKey;
|
||||
private String appId;
|
||||
private String privateKey;
|
||||
|
||||
private OpenConfig openConfig;
|
||||
private OpenRequest openRequest;
|
||||
|
||||
public OpenClient(String url, String appKey, String privateKey) {
|
||||
this(url, appKey, privateKey, DEFAULT_CONFIG);
|
||||
public OpenClient(String url, String appId, String privateKey) {
|
||||
this(url, appId, privateKey, DEFAULT_CONFIG);
|
||||
}
|
||||
|
||||
public OpenClient(String url, String appKey, String privateKey, OpenConfig openConfig) {
|
||||
public OpenClient(String url, String appId, String privateKey, OpenConfig openConfig) {
|
||||
if (openConfig == null) {
|
||||
throw new IllegalArgumentException("openConfig不能为null");
|
||||
}
|
||||
this.url = url;
|
||||
this.appKey = appKey;
|
||||
this.appId = appId;
|
||||
this.privateKey = privateKey;
|
||||
this.openConfig = openConfig;
|
||||
|
||||
@@ -74,13 +72,13 @@ public class OpenClient {
|
||||
* @return 返回Response
|
||||
*/
|
||||
public <T extends BaseResponse> T execute(BaseRequest<T> request, String accessToken) {
|
||||
RequestForm requestForm = request.createRequestForm();
|
||||
RequestForm requestForm = request.createRequestForm(this.openConfig);
|
||||
// 表单数据
|
||||
Map<String, String> form = requestForm.getForm();
|
||||
if (accessToken != null) {
|
||||
form.put(this.openConfig.getAccessTokenName(), accessToken);
|
||||
}
|
||||
form.put(this.openConfig.getAppKeyName(), this.appKey);
|
||||
form.put(this.openConfig.getAppKeyName(), this.appId);
|
||||
|
||||
String content = SopSignature.getSignContent(form);
|
||||
String sign = null;
|
||||
@@ -106,7 +104,7 @@ public class OpenClient {
|
||||
}
|
||||
|
||||
protected String doExecute(String url, RequestForm requestForm, Map<String, String> header) {
|
||||
return openRequest.request(this.url, requestForm, header);
|
||||
return openRequest.request(url, requestForm, header);
|
||||
}
|
||||
|
||||
protected <T extends BaseResponse> T parseResponse(String resp, BaseRequest<T> request) {
|
||||
|
@@ -4,6 +4,7 @@ import com.gitee.sop.sdk.common.OpenConfig;
|
||||
import com.gitee.sop.sdk.common.UploadFile;
|
||||
import okhttp3.Cookie;
|
||||
import okhttp3.CookieJar;
|
||||
import okhttp3.FormBody;
|
||||
import okhttp3.HttpUrl;
|
||||
import okhttp3.MediaType;
|
||||
import okhttp3.MultipartBody;
|
||||
@@ -13,6 +14,7 @@ import okhttp3.RequestBody;
|
||||
import okhttp3.Response;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
@@ -38,6 +40,7 @@ public class OpenHttp {
|
||||
httpClient = new OkHttpClient.Builder()
|
||||
.connectTimeout(openConfig.getConnectTimeoutSeconds(), TimeUnit.SECONDS) // 设置链接超时时间,默认10秒
|
||||
.readTimeout(openConfig.getReadTimeoutSeconds(), TimeUnit.SECONDS)
|
||||
.writeTimeout(openConfig.getWriteTimeoutSeconds(), TimeUnit.SECONDS)
|
||||
.cookieJar(new CookieJar() {
|
||||
public void saveFromResponse(HttpUrl httpUrl, List<Cookie> list) {
|
||||
cookieStore.put(httpUrl.host(), list);
|
||||
@@ -69,23 +72,35 @@ public class OpenHttp {
|
||||
}
|
||||
|
||||
/**
|
||||
* 提交json字符串到请求体
|
||||
* 提交表单
|
||||
*
|
||||
* @param url
|
||||
* @param json
|
||||
* @param form
|
||||
* @param header header内容
|
||||
* @return
|
||||
* @throws IOException
|
||||
*/
|
||||
public String postJsonBody(String url, String json, Map<String, String> header) throws IOException {
|
||||
RequestBody body = RequestBody.create(JSON, json);
|
||||
Request.Builder builder = new Request.Builder().url(url).post(body);
|
||||
public String postFormBody(String url, Map<String, String> form, Map<String, String> header) throws IOException {
|
||||
FormBody.Builder paramBuilder = new FormBody.Builder(StandardCharsets.UTF_8);
|
||||
for (Map.Entry<String, String> entry : form.entrySet()) {
|
||||
paramBuilder.add(entry.getKey(), entry.getValue());
|
||||
}
|
||||
FormBody formBody = paramBuilder.build();
|
||||
Request.Builder requestBuilder = new Request.Builder()
|
||||
.url(url)
|
||||
.post(formBody);
|
||||
// 添加header
|
||||
addHeader(builder, header);
|
||||
addHeader(requestBuilder, header);
|
||||
|
||||
Request request = builder.build();
|
||||
Response response = httpClient.newCall(request).execute();
|
||||
return response.body().string();
|
||||
Request request = requestBuilder.build();
|
||||
Response response = httpClient
|
||||
.newCall(request)
|
||||
.execute();
|
||||
try {
|
||||
return response.body().string();
|
||||
} finally {
|
||||
response.close();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -125,7 +140,11 @@ public class OpenHttp {
|
||||
|
||||
Request request = builder.build();
|
||||
Response response = httpClient.newCall(request).execute();
|
||||
return response.body().string();
|
||||
try {
|
||||
return response.body().string();
|
||||
} finally {
|
||||
response.close();
|
||||
}
|
||||
}
|
||||
|
||||
private void addHeader(Request.Builder builder, Map<String, String> header) {
|
||||
|
@@ -36,7 +36,7 @@ public class OpenRequest {
|
||||
if (files != null && files.size() > 0) {
|
||||
return openHttp.postFile(url, form, header, files);
|
||||
} else {
|
||||
return openHttp.postJsonBody(url, JsonUtil.toJSONString(form), header);
|
||||
return openHttp.postFormBody(url, form, header);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
return this.causeException(e);
|
||||
|
@@ -12,9 +12,11 @@ public class OpenConfig {
|
||||
/** 默认版本号 */
|
||||
private String defaultVersion = SdkConfig.DEFAULT_VERSION;
|
||||
/** 接口属性名 */
|
||||
private String apiName = "method";
|
||||
private String methodName = "method";
|
||||
/** 版本号名称 */
|
||||
private String versionName = "version";
|
||||
/** 编码名称 */
|
||||
private String charsetName = "charset";
|
||||
/** appKey名称 */
|
||||
private String appKeyName = "app_id";
|
||||
/** data名称 */
|
||||
@@ -25,9 +27,11 @@ public class OpenConfig {
|
||||
private String timestampPattern = "yyyy-MM-dd HH:mm:ss";
|
||||
/** 签名串名称 */
|
||||
private String signName = "sign";
|
||||
/** 签名类型名称 */
|
||||
private String signTypeName = "sign_type";
|
||||
/** 格式化名称 */
|
||||
private String formatName = "format";
|
||||
/** 格式类型 */
|
||||
/** 格式类型名称 */
|
||||
private String formatType = "json";
|
||||
/** accessToken名称 */
|
||||
private String accessTokenName = "app_auth_token";
|
||||
@@ -39,4 +43,6 @@ public class OpenConfig {
|
||||
private int connectTimeoutSeconds = 10;
|
||||
/** http读取超时时间 */
|
||||
private int readTimeoutSeconds = 10;
|
||||
/** http写超时时间 */
|
||||
private int writeTimeoutSeconds = 10;
|
||||
}
|
||||
|
@@ -1,13 +1,12 @@
|
||||
package com.gitee.sop.sdk.request;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.gitee.sop.sdk.common.OpenConfig;
|
||||
import com.gitee.sop.sdk.common.RequestForm;
|
||||
import com.gitee.sop.sdk.common.SdkConfig;
|
||||
import com.gitee.sop.sdk.common.UploadFile;
|
||||
import com.gitee.sop.sdk.response.BaseResponse;
|
||||
import com.gitee.sop.sdk.util.ClassUtil;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
@@ -55,9 +54,16 @@ public abstract class BaseRequest<T extends BaseResponse> {
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public BaseRequest() {
|
||||
this.method = this.method();
|
||||
this.version = this.version();
|
||||
this.setMethodVersion(this.method(), this.version());
|
||||
}
|
||||
|
||||
protected BaseRequest(String method, String version) {
|
||||
this.setMethodVersion(method, version);
|
||||
}
|
||||
|
||||
private void setMethodVersion(String method, String version) {
|
||||
this.method = method;
|
||||
this.version = version == null ? SdkConfig.DEFAULT_VERSION : version;
|
||||
this.responseClass = (Class<T>) ClassUtil.getSuperClassGenricType(this.getClass(), 0);
|
||||
}
|
||||
|
||||
@@ -65,20 +71,20 @@ public abstract class BaseRequest<T extends BaseResponse> {
|
||||
return SdkConfig.DEFAULT_VERSION;
|
||||
}
|
||||
|
||||
public RequestForm createRequestForm() {
|
||||
public RequestForm createRequestForm(OpenConfig openConfig) {
|
||||
// 公共请求参数
|
||||
Map<String, String> params = new HashMap<String, String>();
|
||||
params.put("method", this.method);
|
||||
params.put("format", this.format);
|
||||
params.put("charset", this.charset);
|
||||
params.put("sign_type", this.signType);
|
||||
params.put("timestamp", this.timestamp);
|
||||
params.put("version", this.version);
|
||||
params.put(openConfig.getMethodName(), this.method);
|
||||
params.put(openConfig.getFormatName(), this.format);
|
||||
params.put(openConfig.getCharsetName(), this.charset);
|
||||
params.put(openConfig.getSignTypeName(), this.signType);
|
||||
params.put(openConfig.getTimestampName(), this.timestamp);
|
||||
params.put(openConfig.getVersionName(), this.version);
|
||||
|
||||
// 业务参数
|
||||
String biz_content = buildBizContent();
|
||||
|
||||
params.put("biz_content", biz_content);
|
||||
params.put(openConfig.getDataName(), biz_content);
|
||||
|
||||
RequestForm requestForm = new RequestForm(params);
|
||||
requestForm.setFiles(this.files);
|
||||
@@ -97,10 +103,6 @@ public abstract class BaseRequest<T extends BaseResponse> {
|
||||
return method;
|
||||
}
|
||||
|
||||
protected void setMethod(String method) {
|
||||
this.method = method;
|
||||
}
|
||||
|
||||
public void setVersion(String version) {
|
||||
this.version = version;
|
||||
}
|
||||
|
@@ -8,12 +8,11 @@ import com.gitee.sop.sdk.response.CommonResponse;
|
||||
public class CommonRequest extends BaseRequest<CommonResponse> {
|
||||
|
||||
public CommonRequest(String method) {
|
||||
this.setMethod(method);
|
||||
super(method, null);
|
||||
}
|
||||
|
||||
public CommonRequest(String method, String version) {
|
||||
this.setMethod(method);
|
||||
this.setVersion(version);
|
||||
super(method, version);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@@ -1,7 +1,5 @@
|
||||
package com.gitee.sop.sdk.response;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author tanghc
|
||||
*/
|
||||
|
@@ -8,5 +8,5 @@ import java.util.Date;
|
||||
public class GetStoryResponse extends BaseResponse {
|
||||
private Long id;
|
||||
private String name;
|
||||
private Date gmtCreate;
|
||||
private Date gmt_create;
|
||||
}
|
||||
|
@@ -41,7 +41,7 @@ public class SdkTest extends TestCase {
|
||||
// 返回结果
|
||||
System.out.println(response);
|
||||
} else {
|
||||
System.out.println(response);
|
||||
System.out.println("错误,subCode:" + response.getSubCode() + ", subMsg:" + response.getSubMsg());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -65,7 +65,7 @@ public class SdkTest extends TestCase {
|
||||
JSONObject jsonObject = JSON.parseObject(body);
|
||||
System.out.println(jsonObject);
|
||||
} else {
|
||||
System.out.println(response);
|
||||
System.out.println("错误,subCode:" + response.getSubCode() + ", subMsg:" + response.getSubMsg());
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user