Commit 619e4fd3 by 袁津

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

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