提交 c574d9c0 authored 作者: inroi's avatar inroi

微调

上级 992b5782
...@@ -4,6 +4,7 @@ import com.core.config.ThreadLocalConfig; ...@@ -4,6 +4,7 @@ import com.core.config.ThreadLocalConfig;
import com.core.utils.StringUtil; import com.core.utils.StringUtil;
import com.system.constans.KingDeeConstants; import com.system.constans.KingDeeConstants;
import com.system.serializer.SerializerProxy; import com.system.serializer.SerializerProxy;
import com.system.utils.HttpUtil;
import com.system.utils.JsonUtil; import com.system.utils.JsonUtil;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
...@@ -51,7 +52,7 @@ public class KingDeeApiClient { ...@@ -51,7 +52,7 @@ public class KingDeeApiClient {
System.out.println("------------>" + url); System.out.println("------------>" + url);
long start = System.currentTimeMillis(); long start = System.currentTimeMillis();
ResponseEntity<String> responseEntity = KingDeeHttpApi.httpPost(url, header, JsonUtil.toString(request)); ResponseEntity<String> responseEntity = HttpUtil.httpPost(url, header, JsonUtil.toString(request));
long costTime = System.currentTimeMillis() - start; long costTime = System.currentTimeMillis() - start;
Map<String, Object> map = ThreadLocalConfig.get(); Map<String, Object> map = ThreadLocalConfig.get();
...@@ -67,7 +68,7 @@ public class KingDeeApiClient { ...@@ -67,7 +68,7 @@ public class KingDeeApiClient {
request.put("parameters", new Object[]{dbId, userName, password, lcid}); request.put("parameters", new Object[]{dbId, userName, password, lcid});
String url = serverUrl + "Kingdee.BOS.WebApi.ServicesStub.AuthService.ValidateUser" + HTTP_SUFFIX; String url = serverUrl + "Kingdee.BOS.WebApi.ServicesStub.AuthService.ValidateUser" + HTTP_SUFFIX;
ResponseEntity<String> responseEntity = KingDeeHttpApi.httpPost(url, JsonUtil.toString(request)); ResponseEntity<String> responseEntity = HttpUtil.httpPost(url, JsonUtil.toString(request));
System.out.println(responseEntity.getBody()); System.out.println(responseEntity.getBody());
if (responseEntity.getStatusCode() == HttpStatus.OK) { if (responseEntity.getStatusCode() == HttpStatus.OK) {
......
package com.system.handler;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.web.client.ResponseErrorHandler;
import javax.annotation.Nonnull;
/**
* @author Inori
*/
public class FacePlusThrowErrorHandler implements ResponseErrorHandler {
@Override
public boolean hasError(@Nonnull ClientHttpResponse response) {
return false;
}
@Override
public void handleError(@Nonnull ClientHttpResponse response) {
}
}
package com.system.api; package com.system.utils;
import com.system.handler.FacePlusThrowErrorHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpEntity; import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders; import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod; import org.springframework.http.HttpMethod;
...@@ -14,7 +17,9 @@ import java.util.Set; ...@@ -14,7 +17,9 @@ import java.util.Set;
/** /**
* @author Inori * @author Inori
*/ */
public class KingDeeHttpApi { public class HttpUtil {
private static final Logger log = LoggerFactory.getLogger(HttpUtil.class);
/** /**
* 连接超时时间 * 连接超时时间
...@@ -38,27 +43,7 @@ public class KingDeeHttpApi { ...@@ -38,27 +43,7 @@ public class KingDeeHttpApi {
requestFactory.setConnectTimeout(CONN_TIMEOUT); requestFactory.setConnectTimeout(CONN_TIMEOUT);
requestFactory.setReadTimeout(READ_TIMEOUT); requestFactory.setReadTimeout(READ_TIMEOUT);
restTemplate = new RestTemplate(requestFactory); restTemplate = new RestTemplate(requestFactory);
} restTemplate.setErrorHandler(new FacePlusThrowErrorHandler());
/**
* 发送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);
} }
/** /**
...@@ -86,6 +71,81 @@ public class KingDeeHttpApi { ...@@ -86,6 +71,81 @@ public class KingDeeHttpApi {
} }
/** /**
* 发送Get请求
*/
public static ResponseEntity<String> httpGet(String url) {
//初始化header公共参数
HttpHeaders httpHeaders = initHeader();
//发送请求
HttpEntity<String> httpEntity = new HttpEntity<>(null, httpHeaders);
return restTemplate.exchange(url, HttpMethod.GET, httpEntity, String.class);
}
/**
* 发送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);
log.info("【Get请求】Url: {}", url);
return restTemplate.exchange(url, HttpMethod.GET, httpEntity, String.class);
}
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);
//发送请求
log.info("【Get请求】Url: {}", url);
return httpGet(url, headers);
}
/**
* 发送Post请求
*/
public static ResponseEntity<String> httpPost(String url, String json) {
//初始化header公共参数
HttpHeaders httpHeaders = initHeader();
//发送请求
log.info("【Post请求】Url: {}", url);
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);
//发送请求
log.info("【Post请求】Url: {}", url);
return toPost(url, httpHeaders, json);
}
/**
* 发送请求 * 发送请求
*/ */
private static ResponseEntity<String> toPost(String url, HttpHeaders httpHeaders, String json) { private static ResponseEntity<String> toPost(String url, HttpHeaders httpHeaders, String json) {
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论