提交 8cd41ec2 authored 作者: 李勤's avatar 李勤

excute

上级 c3f8fe24
...@@ -10,7 +10,10 @@ import lombok.extern.slf4j.Slf4j; ...@@ -10,7 +10,10 @@ import lombok.extern.slf4j.Slf4j;
import org.apache.http.client.CookieStore; import org.apache.http.client.CookieStore;
import org.jeecg.common.util.ThreadLocalConfig; import org.jeecg.common.util.ThreadLocalConfig;
import org.jeecg.modules.iost.ims.Util.JsonUtil;
import org.jeecg.modules.iost.ims.kingdeeapi.HttpUtil;
import org.jeecg.modules.iost.ims.service.impl.ImsNondefectiveFlittingServiceImpl; import org.jeecg.modules.iost.ims.service.impl.ImsNondefectiveFlittingServiceImpl;
import org.springframework.http.ResponseEntity;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
...@@ -48,9 +51,29 @@ public class KingdeeApiClient { ...@@ -48,9 +51,29 @@ public class KingdeeApiClient {
map2.put("Costime", Costime); map2.put("Costime", Costime);
ThreadLocalConfig.set(map2); ThreadLocalConfig.set(map2);
} }
execute(servicename, parameters);
return send; return send;
} }
public void execute(String serviceName, Object[] parameters) throws Exception {
Map<String, Object> header = new HashMap<>(1);
header.put("cookie", this._cookieStore);
Map<String, Object> request = new HashMap<>(1);
request.put("parameters", parameters);
String url = this._serverUrl + serviceName + ".common.kdsvc";
long start = System.currentTimeMillis();
String requestStr = JsonUtil.toString(request);
ResponseEntity<String> responseEntity = HttpUtil.httpPost(url, header, requestStr);
long costTime = System.currentTimeMillis() - start;
log.info("Costime2_"+String.valueOf(costTime));
}
public <T> ApiRequest<T> executeAsync(String servicename, Object[] parameters, Class<T> returnType, IAsyncActionCallBack<T> callback) throws Exception { public <T> ApiRequest<T> executeAsync(String servicename, Object[] parameters, Class<T> returnType, IAsyncActionCallBack<T> callback) throws Exception {
ApiRequest<T> request = this.createRequest(servicename, parameters, returnType); ApiRequest<T> request = this.createRequest(servicename, parameters, returnType);
ApiHttpClient<T> httpClient = new ApiHttpClient(callback); ApiHttpClient<T> httpClient = new ApiHttpClient(callback);
......
package org.jeecg.modules.iost.ims.kingdeeapi;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.util.CollectionUtils;
import org.springframework.web.client.RestTemplate;
import java.util.Map;
import java.util.Set;
public class HttpUtil {
private static final Logger log = LoggerFactory.getLogger(HttpUtil.class);
/**
* 连接超时时间
*/
private static final int CONN_TIMEOUT = 12000;
/**
* 请求超时时间
*/
private static final int READ_TIMEOUT = 12000;
/**
* 请求工具
*/
private static RestTemplate restTemplate;
static {
//设置超时时间
SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
requestFactory.setConnectTimeout(CONN_TIMEOUT);
requestFactory.setReadTimeout(READ_TIMEOUT);
restTemplate = new RestTemplate(requestFactory);
}
/**
* 设置header公共参数
*/
private static HttpHeaders initHeader() {
HttpHeaders headers = new HttpHeaders();
headers.add("Accept", "application/json");
headers.add("Accpet-Encoding", "gzip");
headers.add("Content-Encoding", "UTF-8");
headers.add("Content-Type", "application/json; charset=UTF-8");
return headers;
}
/**
* * 判断一个对象是否为空
*
* @param object Object
* @return true:为空 false:非空
*/
public static boolean isNull(Object object) {
return object == null;
}
public static boolean isNotNull(Object object) {
return !isNull(object);
}
/**
* 设置header参数
*/
private static void setHeaderParam(HttpHeaders httpHeaders, Map<String, Object> headers) {
if (!CollectionUtils.isEmpty(headers)) {
Set<String> keys = headers.keySet();
for (String key : keys) {
if(isNotNull(headers.get(key)))
{
httpHeaders.add(key, headers.get(key).toString());
}
}
}
}
/**
* 发送Get请求
*/
public static ResponseEntity<String> httpGet(String url) {
//初始化header公共参数
HttpHeaders httpHeaders = initHeader();
//发送请求
HttpEntity<String> httpEntity = new HttpEntity<>(null, httpHeaders);
ResponseEntity<String> result = null;
try {
result = restTemplate.exchange(url, HttpMethod.POST, httpEntity, String.class);
}
catch(Exception ex)
{
try {
result = restTemplate.exchange(url, HttpMethod.POST, httpEntity, String.class);
}
catch(Exception e)
{
result = restTemplate.exchange(url, HttpMethod.POST, httpEntity, String.class);
}
}
return result;
}
/**
* 发送Get请求
*/
public static ResponseEntity<String> httpGet(String url, Map<String, Object> param) {
//初始化header公共参数
HttpHeaders httpHeaders = initHeader();
//组装查询参数
url = setParam(url, param);
//发送请求
HttpEntity<String> httpEntity = new HttpEntity<>(null, httpHeaders);
ResponseEntity<String> result = null;
try {
result = restTemplate.exchange(url, HttpMethod.POST, httpEntity, String.class);
}
catch(Exception ex)
{
try {
result = restTemplate.exchange(url, HttpMethod.POST, httpEntity, String.class);
}
catch(Exception e)
{
result = restTemplate.exchange(url, HttpMethod.POST, httpEntity, String.class);
}
}
return result;
}
private static String setParam(String url, Map<String, Object> param) {
Set<String> keys = param.keySet();
StringBuilder builder = new StringBuilder();
builder.append("?");
for (String key : keys) {
builder.append(key).append("=").append(param.get(key)).append("&");
}
builder.deleteCharAt(builder.lastIndexOf("&"));
url = url + builder.toString();
return url;
}
/**
* 发送Get请求
*/
public static ResponseEntity<String> httpGet(String url, Map<String, Object> headers, Map<String, Object> param) {
//如果查询参数为空,则调用不带参数的Get请求
if (CollectionUtils.isEmpty(param)) {
return httpGet(url, headers);
}
//组装查询参数
url = setParam(url, param);
//发送请求
return httpGet(url, headers);
}
/**
* 发送Post请求
*/
public static ResponseEntity<String> httpPost(String url, String json) {
//初始化header公共参数
HttpHeaders httpHeaders = initHeader();
//发送请求
return toPost(url, httpHeaders, json);
}
/**
* 发送Post请求
*/
public static ResponseEntity<String> httpPost(String url, Map<String, Object> header, String json) {
//初始化header公共参数
HttpHeaders httpHeaders = initHeader();
setHeaderParam(httpHeaders, header);
//发送请求
return toPost(url, httpHeaders, json);
}
/**
* 发送请求
*/
private static ResponseEntity<String> toPost(String url, HttpHeaders httpHeaders, String json) {
HttpEntity<String> httpEntity = new HttpEntity<>(json, httpHeaders);
ResponseEntity<String> result = null;
try {
result = restTemplate.exchange(url, HttpMethod.POST, httpEntity, String.class);
}
catch(Exception ex)
{
try {
result = restTemplate.exchange(url, HttpMethod.POST, httpEntity, String.class);
}
catch(Exception e)
{
result = restTemplate.exchange(url, HttpMethod.POST, httpEntity, String.class);
}
}
return result;
}
/**
* 文件二进制
*/
public static byte[] download(String url, String json) {
//初始化header公共参数
HttpHeaders httpHeaders = initHeader();
HttpEntity<String> httpEntity = new HttpEntity<>(json, httpHeaders);
ResponseEntity<byte[]> result = null;
try {
result = restTemplate.exchange(url, HttpMethod.POST, httpEntity, byte[].class);
}
catch(Exception ex)
{
try {
result = restTemplate.exchange(url, HttpMethod.POST, httpEntity, byte[].class);
}
catch(Exception e)
{
result = restTemplate.exchange(url, HttpMethod.POST, httpEntity, byte[].class);
}
}
return result.getBody();
}
}
\ No newline at end of file
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论