|
@@ -172,7 +172,8 @@ public class RestTemplateUtils {
|
|
|
* @param <T>
|
|
|
* @return
|
|
|
*/
|
|
|
- public <T> T post(String url, HttpHeaders httpHeaders, Object reqParam, Class<T> clazz, IRestFallback<?> restFallback) {
|
|
|
+ public <T> T post(String url, HttpHeaders httpHeaders, Object reqParam, Class<T> clazz,
|
|
|
+ IRestFallback<?> restFallback) {
|
|
|
String newUrl = getUrl(url);
|
|
|
Transaction t = Cat.getProducer().newTransaction(CatMsgConstants.THIRD_PARTY, newUrl);
|
|
|
logger.debug(" URL : " + url);
|
|
@@ -196,12 +197,12 @@ public class RestTemplateUtils {
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
if (!(e instanceof CoolingException)) {
|
|
|
- logger.warn("第三方调用异常:", e);
|
|
|
+ logger.warn("第三方调用异常:" + e.getMessage());
|
|
|
}
|
|
|
t.setStatus(e.getClass().getSimpleName());
|
|
|
if (restFallback != null) {
|
|
|
logger.debug("触发异常回调 : " + restFallback.toString());
|
|
|
- return (T) restFallback.post(url, httpHeaders, reqParam, clazz, e);
|
|
|
+ return (T)restFallback.post(url, httpHeaders, reqParam, clazz, e);
|
|
|
}
|
|
|
} finally {
|
|
|
t.complete();
|
|
@@ -237,13 +238,15 @@ public class RestTemplateUtils {
|
|
|
return exchange(url, HttpMethod.PUT, reqParam, clazz, httpHeaders, null);
|
|
|
}
|
|
|
|
|
|
- private <T> void requestAfter(String url, HttpHeaders httpHeaders, Object reqParam, Class<T> clazz, IRestFallback<?> restFallback) throws Exception {
|
|
|
+ private <T> void requestAfter(String url, HttpHeaders httpHeaders, Object reqParam, Class<T> clazz,
|
|
|
+ IRestFallback<?> restFallback) throws Exception {
|
|
|
if (restFallback != null) {
|
|
|
restFallback.requestAfter(url, httpHeaders, reqParam, clazz);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- private <T> void requestBefore(String url, HttpHeaders httpHeaders, Object reqParam, Class<T> clazz, IRestFallback<?> restFallback) throws Exception {
|
|
|
+ private <T> void requestBefore(String url, HttpHeaders httpHeaders, Object reqParam, Class<T> clazz,
|
|
|
+ IRestFallback<?> restFallback) throws Exception {
|
|
|
if (restFallback != null) {
|
|
|
restFallback.requestBefore(url, httpHeaders, reqParam, clazz);
|
|
|
}
|
|
@@ -285,16 +288,16 @@ public class RestTemplateUtils {
|
|
|
responseData = restTemplate.getForObject(url, clazz);
|
|
|
t.setStatus(Transaction.SUCCESS);
|
|
|
if (responseData != null) {
|
|
|
- logResponse((T) responseData);
|
|
|
+ logResponse((T)responseData);
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
if (!(e instanceof CoolingException)) {
|
|
|
- logger.warn("第三方调用异常:", e);
|
|
|
+ logger.warn("第三方调用异常:"+e.getMessage());
|
|
|
}
|
|
|
t.setStatus(e.getClass().getSimpleName());
|
|
|
if (restFallback != null) {
|
|
|
logger.debug("触发失败回调 : " + restFallback.toString());
|
|
|
- return (T) restFallback.get(url, clazz, e);
|
|
|
+ return (T)restFallback.get(url, clazz, e);
|
|
|
}
|
|
|
} finally {
|
|
|
t.complete();
|
|
@@ -339,7 +342,8 @@ public class RestTemplateUtils {
|
|
|
return exchange(url, HttpMethod.GET, body, clazz, httpHeaders, restFallback);
|
|
|
}
|
|
|
|
|
|
- private <T> T exchange(String url, HttpMethod httpMethod, Object body, Class<T> clazz, HttpHeaders httpHeaders, IRestFallback restFallback) {
|
|
|
+ private <T> T exchange(String url, HttpMethod httpMethod, Object body, Class<T> clazz, HttpHeaders httpHeaders,
|
|
|
+ IRestFallback restFallback) {
|
|
|
String newUrl = getUrl(url);
|
|
|
Transaction t = Cat.getProducer().newTransaction(CatMsgConstants.THIRD_PARTY, newUrl);
|
|
|
logger.debug(" URL : " + url);
|
|
@@ -360,16 +364,16 @@ public class RestTemplateUtils {
|
|
|
responseData = result.getBody();
|
|
|
t.setStatus(Transaction.SUCCESS);
|
|
|
if (responseData != null) {
|
|
|
- logResponse((T) responseData);
|
|
|
+ logResponse((T)responseData);
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
if (!(e instanceof CoolingException)) {
|
|
|
- logger.warn("第三方调用异常:", e);
|
|
|
+ logger.warn("第三方调用异常:"+ e.getMessage());
|
|
|
}
|
|
|
t.setStatus(e.getClass().getSimpleName());
|
|
|
if (restFallback != null) {
|
|
|
logger.debug("触发失败回调 : " + restFallback.toString());
|
|
|
- return (T) restFallback.get(url, clazz, e);
|
|
|
+ return (T)restFallback.get(url, clazz, e);
|
|
|
}
|
|
|
} finally {
|
|
|
t.complete();
|
|
@@ -407,7 +411,7 @@ public class RestTemplateUtils {
|
|
|
}
|
|
|
t.setStatus(Transaction.SUCCESS);
|
|
|
} catch (Exception e) {
|
|
|
- logger.warn("------ 第三方接口调用失败 : ", e);
|
|
|
+ logger.warn("------ 第三方接口调用失败 : "+ e.getMessage());
|
|
|
t.setStatus(e.getClass().getSimpleName());
|
|
|
} finally {
|
|
|
t.complete();
|
|
@@ -450,16 +454,12 @@ public class RestTemplateUtils {
|
|
|
}
|
|
|
|
|
|
public static void main(String[] args) {
|
|
|
- String param = "{\n" +
|
|
|
- "\t\"pageno\":\"1\",\n" +
|
|
|
- "\t\"pagesize\":\"10\",\n" +
|
|
|
- "\t\"houseid\":\"102\",\n" +
|
|
|
- "\t\"time\":\"2017-04-13\"\n" +
|
|
|
- "}";
|
|
|
+ String param = "{\n" + "\t\"pageno\":\"1\",\n" + "\t\"pagesize\":\"10\",\n" + "\t\"houseid\":\"102\",\n"
|
|
|
+ + "\t\"time\":\"2017-04-13\"\n" + "}";
|
|
|
JSONObject jsonObject = JSON.parseObject(param);
|
|
|
RestTemplateUtils restTemplateUtils = new RestTemplateUtils();
|
|
|
-// JSONModel post = restTemplateUtils.post("http://localhost:9005/skyforest/dd/pageList", jsonObject, JSONModel.class);
|
|
|
-// System.out.println(JSON.toJSONString(post));
|
|
|
+ // JSONModel post = restTemplateUtils.post("http://localhost:9005/skyforest/dd/pageList", jsonObject, JSONModel.class);
|
|
|
+ // System.out.println(JSON.toJSONString(post));
|
|
|
}
|
|
|
|
|
|
}
|