|
@@ -2,8 +2,10 @@ package com.elab.spring.utils;
|
|
|
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
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.log.utils.EventProcessUtils;
|
|
|
+import com.elab.log.utils.CatMsgConstants;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.http.*;
|
|
@@ -68,20 +70,29 @@ public class RestTemplateUtils {
|
|
|
* @param <T>
|
|
|
* @return
|
|
|
*/
|
|
|
- public <T> T post(String url, Object reqParam, Class<T> clazz) {
|
|
|
- EventProcessUtils.writeThirdParty(url);
|
|
|
+ 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 : " + ObjectUtils.objectParseJsonStr(reqParam));
|
|
|
+ logger.info(" RequestData : " + logData(reqParam));
|
|
|
}
|
|
|
-
|
|
|
- //利用容器实现数据封装,发送
|
|
|
- HttpEntity<Object> entity = new HttpEntity<Object>(reqParam, headers);
|
|
|
- T o = restTemplate.postForObject(url, entity, clazz);
|
|
|
- if (o != null) {
|
|
|
- logger.info(" ResponseData : " + ObjectUtils.objectParseJsonStr(o));
|
|
|
+ T responseData = null;
|
|
|
+ try {
|
|
|
+ //利用容器实现数据封装,发送
|
|
|
+ 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));
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ logger.error("------ 第三方接口调用失败 : ", e);
|
|
|
+ t.setStatus(e.getClass().getSimpleName());
|
|
|
+ } finally {
|
|
|
+ t.complete();
|
|
|
}
|
|
|
- return o;
|
|
|
+ return responseData;
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -93,11 +104,24 @@ public class RestTemplateUtils {
|
|
|
* @return
|
|
|
*/
|
|
|
public <T> T get(String url, Class<T> clazz) {
|
|
|
- EventProcessUtils.writeThirdParty(url);
|
|
|
+ String newUrl = getUrl(url);
|
|
|
+ Transaction t = Cat.getProducer().newTransaction(CatMsgConstants.THIRD_PARTY, newUrl);
|
|
|
logger.info(" URL : " + url);
|
|
|
//利用容器实现数据封装,发送
|
|
|
- T o = restTemplate.getForObject(url, clazz);
|
|
|
- return o;
|
|
|
+ T responseData = null;
|
|
|
+ try {
|
|
|
+ responseData = restTemplate.getForObject(url, clazz);
|
|
|
+ t.setStatus(Transaction.SUCCESS);
|
|
|
+ if (responseData != null) {
|
|
|
+ logger.info(" ResponseData : " + logData(responseData));
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ logger.error("------ 第三方接口调用失败 : ", e);
|
|
|
+ t.setStatus(e.getClass().getSimpleName());
|
|
|
+ } finally {
|
|
|
+ t.complete();
|
|
|
+ }
|
|
|
+ return responseData;
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -112,13 +136,60 @@ public class RestTemplateUtils {
|
|
|
public <T> T postUpload(String url, Object reqParam, Class<T> clazz) {
|
|
|
logger.info(" URL : " + url);
|
|
|
logger.info(" RequestData : " + ObjectUtils.objectParseJsonStr(reqParam));
|
|
|
- //利用容器实现数据封装,发送
|
|
|
- HttpEntity<Object> entity = new HttpEntity<Object>(reqParam, new HttpHeaders());
|
|
|
- ResponseEntity<T> exchange = restTemplate.exchange(url, HttpMethod.POST, entity, clazz);
|
|
|
- if (exchange != null && exchange.getBody() != null) {
|
|
|
- logger.info(" ResponseData : " + ObjectUtils.objectParseJsonStr(exchange.getBody()));
|
|
|
+
|
|
|
+ String newUrl = getUrl(url);
|
|
|
+ Transaction t = Cat.getProducer().newTransaction(CatMsgConstants.THIRD_PARTY, newUrl);
|
|
|
+ T responseData = null;
|
|
|
+ try {
|
|
|
+ //利用容器实现数据封装,发送
|
|
|
+ HttpEntity<Object> entity = new HttpEntity<Object>(reqParam, new HttpHeaders());
|
|
|
+ ResponseEntity<T> exchange = restTemplate.exchange(url, HttpMethod.POST, entity, clazz);
|
|
|
+ responseData = exchange.getBody();
|
|
|
+ if (exchange != null && responseData != null) {
|
|
|
+ logger.info(" ResponseData : " + logData(responseData));
|
|
|
+ }
|
|
|
+ t.setStatus(Transaction.SUCCESS);
|
|
|
+ } catch (Exception e) {
|
|
|
+ logger.error("------ 第三方接口调用失败 : ", e);
|
|
|
+ t.setStatus(e.getClass().getSimpleName());
|
|
|
+ } finally {
|
|
|
+ t.complete();
|
|
|
+ }
|
|
|
+ return responseData;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取除开?号后面的URL
|
|
|
+ *
|
|
|
+ * @param url
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private String getUrl(String url) {
|
|
|
+ if (ObjectUtils.isNotEmpty(url)) {
|
|
|
+ String newUrl = url;
|
|
|
+ if (url.indexOf("?") > 0) {
|
|
|
+ newUrl = url.substring(0, url.indexOf("?"));
|
|
|
+ }
|
|
|
+ return newUrl;
|
|
|
+ }
|
|
|
+ return url;
|
|
|
+ }
|
|
|
+
|
|
|
+ private String logData(Object data) {
|
|
|
+ if (data == null) {
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+
|
|
|
+ if (data instanceof String) {
|
|
|
+ return data.toString();
|
|
|
+ }
|
|
|
+ String logData = "";
|
|
|
+ try {
|
|
|
+ logData = ObjectUtils.objectParseJsonStr(data);
|
|
|
+ } catch (Exception e) {
|
|
|
+ return data.toString();
|
|
|
}
|
|
|
- return (T) exchange;
|
|
|
+ return logData;
|
|
|
}
|
|
|
|
|
|
public static void main(String[] args) {
|