Commit 619e4fd3 by 袁津

[chg]优化日志打印:只有天气、aqi接口调用异常时,才打印日志

1 parent ad71b23a
...@@ -38,27 +38,33 @@ public class JsonService { ...@@ -38,27 +38,33 @@ public class JsonService {
// 调用weather接口 // 调用weather接口
Callable<Boolean> weatherCallable = () -> { Callable<Boolean> weatherCallable = () -> {
String host = jsonclass.getHost1(); String json = "";
String path = jsonclass.getPath1(); try {
String method = "POST"; String host = jsonclass.getHost1();
String appcode = jsonclass.getAppcode(); String path = jsonclass.getPath1();
Map<String, String> headers = new HashMap<>(); String method = "POST";
headers.put("Authorization", "APPCODE " + appcode); String appcode = jsonclass.getAppcode();
headers.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"); Map<String, String> headers = new HashMap<>();
Map<String, String> querys = new HashMap<>(); headers.put("Authorization", "APPCODE " + appcode);
Map<String, String> bodys = new HashMap<>(); headers.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
bodys.put("cityId", String.valueOf(cityId)); Map<String, String> querys = new HashMap<>();
HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys); Map<String, String> bodys = new HashMap<>();
String json = EntityUtils.toString(response.getEntity()); bodys.put("cityId", String.valueOf(cityId));
HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys);
System.out.println(sdf.format(new Date()) + "调用weather接口返回json:" + json); json = EntityUtils.toString(response.getEntity());
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); ObjectMapper mapper = new ObjectMapper();
mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd")); mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
WeatherMessage weatherMessage = mapper.readValue(json, WeatherMessage.class); mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd"));
if (null != weatherMessage) { WeatherMessage weatherMessage = mapper.readValue(json, WeatherMessage.class);
dataMap.put("WEATHER", weatherMessage); if (null != weatherMessage) {
return true; dataMap.put("WEATHER", weatherMessage);
return true;
}
} catch (Exception e) {
// 接口调用发生异常时,打印接口返回信息
System.out.println("调用weather接口发生异常,接口返回数据:" + json);
throw e;
} }
return false; return false;
}; };
...@@ -89,27 +95,34 @@ public class JsonService { ...@@ -89,27 +95,34 @@ public class JsonService {
// 调用aqi接口 // 调用aqi接口
Callable<Boolean> aqiCallable = () -> { Callable<Boolean> aqiCallable = () -> {
String host = jsonclass.getHost2(); String json = "";
String path = jsonclass.getPath2(); try {
String method = "POST"; String host = jsonclass.getHost2();
String appcode = jsonclass.getAppcode(); String path = jsonclass.getPath2();
Map<String, String> headers = new HashMap<>(); String method = "POST";
headers.put("Authorization", "APPCODE " + appcode); String appcode = jsonclass.getAppcode();
headers.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"); Map<String, String> headers = new HashMap<>();
Map<String, String> querys = new HashMap<>(); headers.put("Authorization", "APPCODE " + appcode);
Map<String, String> bodys = new HashMap<>(); headers.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
bodys.put("cityId", String.valueOf(cityId)); Map<String, String> querys = new HashMap<>();
HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys); Map<String, String> bodys = new HashMap<>();
String json = EntityUtils.toString(response.getEntity()); bodys.put("cityId", String.valueOf(cityId));
HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys);
System.out.println(sdf.format(new Date()) + "调用AQI接口返回json:" + json); json = EntityUtils.toString(response.getEntity());
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); System.out.println(sdf.format(new Date()) + "调用AQI接口返回json:" + json);
mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")); ObjectMapper mapper = new ObjectMapper();
WeatherMessage aqiMessage = mapper.readValue(json, WeatherMessage.class); mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
if (null != aqiMessage) { mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
dataMap.put("AQI", aqiMessage); WeatherMessage aqiMessage = mapper.readValue(json, WeatherMessage.class);
return true; if (null != aqiMessage) {
dataMap.put("AQI", aqiMessage);
return true;
}
} catch (Exception e) {
// 接口调用发生异常时,打印接口返回信息
System.out.println("调用AQI接口发生异常,接口返回数据:" + json);
throw e;
} }
return false; return false;
}; };
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!