|
@@ -5,6 +5,7 @@ import com.alibaba.fastjson.JSONObject;
|
|
|
import com.dianping.cat.Cat;
|
|
|
import com.dianping.cat.message.Transaction;
|
|
|
import com.elab.core.utils.ObjectUtils;
|
|
|
+import com.elab.core.utils.StringUtils;
|
|
|
import com.elab.log.utils.CatMsgConstants;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
@@ -12,6 +13,8 @@ import org.springframework.http.*;
|
|
|
import org.springframework.http.client.SimpleClientHttpRequestFactory;
|
|
|
import org.springframework.web.client.RestTemplate;
|
|
|
|
|
|
+import java.io.InputStream;
|
|
|
+
|
|
|
/**
|
|
|
* http调用工具
|
|
|
*
|
|
@@ -70,12 +73,12 @@ public class RestTemplateUtils {
|
|
|
* @param <T>
|
|
|
* @return
|
|
|
*/
|
|
|
- public <T> T post(String url, Object reqParam, Class<T> clazz) {
|
|
|
+ public <T> T post(String url, Object reqParam, Class<T> clazz) {
|
|
|
String newUrl = getUrl(url);
|
|
|
Transaction t = Cat.getProducer().newTransaction(CatMsgConstants.THIRD_PARTY, newUrl);
|
|
|
logger.info(" URL : " + url);
|
|
|
if (reqParam != null) {
|
|
|
- logger.info(" RequestData : " + logData(reqParam));
|
|
|
+ logger.info(" RequestData : " + StringUtils.logOut(logData(reqParam)));
|
|
|
}
|
|
|
T responseData = null;
|
|
|
try {
|
|
@@ -83,9 +86,7 @@ public class RestTemplateUtils {
|
|
|
HttpEntity<Object> entity = new HttpEntity<Object>(reqParam, headers);
|
|
|
responseData = restTemplate.postForObject(url, entity, clazz);
|
|
|
t.setStatus(Transaction.SUCCESS);
|
|
|
- if (responseData != null) {
|
|
|
- logger.info(" ResponseData : " + logData(responseData));
|
|
|
- }
|
|
|
+ logResponse(responseData);
|
|
|
} catch (Exception e) {
|
|
|
logger.error("------ 第三方接口调用失败 : ", e);
|
|
|
t.setStatus(e.getClass().getSimpleName());
|
|
@@ -95,6 +96,20 @@ public class RestTemplateUtils {
|
|
|
return responseData;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 通用的返回日志
|
|
|
+ *
|
|
|
+ * @param responseData
|
|
|
+ * @param <T>
|
|
|
+ */
|
|
|
+ private <T> void logResponse(T responseData) {
|
|
|
+ if (dontCareType(responseData)) {
|
|
|
+ logger.info(" 不关心的类型 ... ");
|
|
|
+ } else {
|
|
|
+ logger.info(" ResponseData : " + StringUtils.logOut(logData(responseData)));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* get请求发送
|
|
|
*
|
|
@@ -113,7 +128,7 @@ public class RestTemplateUtils {
|
|
|
responseData = restTemplate.getForObject(url, clazz);
|
|
|
t.setStatus(Transaction.SUCCESS);
|
|
|
if (responseData != null) {
|
|
|
- logger.info(" ResponseData : " + logData(responseData));
|
|
|
+ logResponse((T) responseData);
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
logger.error("------ 第三方接口调用失败 : ", e);
|
|
@@ -124,6 +139,10 @@ public class RestTemplateUtils {
|
|
|
return responseData;
|
|
|
}
|
|
|
|
|
|
+ private <T> boolean dontCareType(T responseData) {
|
|
|
+ return responseData instanceof byte[] || responseData instanceof InputStream;
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 文件上传接口
|
|
|
*
|
|
@@ -146,7 +165,7 @@ public class RestTemplateUtils {
|
|
|
ResponseEntity<T> exchange = restTemplate.exchange(url, HttpMethod.POST, entity, clazz);
|
|
|
responseData = exchange.getBody();
|
|
|
if (exchange != null && responseData != null) {
|
|
|
- logger.info(" ResponseData : " + logData(responseData));
|
|
|
+ logger.info(" ResponseData : " + StringUtils.logOut(logData(responseData)));
|
|
|
}
|
|
|
t.setStatus(Transaction.SUCCESS);
|
|
|
} catch (Exception e) {
|