Browse Source

气象监测页面报错处理

develop
zhaoxianglong 8 months ago
parent
commit
59a09b3b76
  1. 82
      zc-business/src/main/java/com/zc/business/controller/WeatherForecastController.java

82
zc-business/src/main/java/com/zc/business/controller/WeatherForecastController.java

@ -8,7 +8,6 @@ import com.ruoyi.common.core.redis.RedisCache;
import com.zc.business.domain.DcRegion; import com.zc.business.domain.DcRegion;
import com.zc.business.service.impl.DcRegionServiceImpl; import com.zc.business.service.impl.DcRegionServiceImpl;
import com.zc.common.core.httpclient.OkHttp; import com.zc.common.core.httpclient.OkHttp;
import com.zc.common.core.httpclient.exception.HttpException;
import io.swagger.annotations.Api; import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
import okhttp3.Response; import okhttp3.Response;
@ -17,7 +16,6 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.io.IOException;
import java.util.*; import java.util.*;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@ -53,7 +51,7 @@ public class WeatherForecastController extends BaseController {
* */ * */
@ApiOperation("天气实况查询") @ApiOperation("天气实况查询")
@PostMapping(value = "/weatherFacts") @PostMapping(value = "/weatherFacts")
public AjaxResult weatherFacts() throws HttpException, IOException { public AjaxResult weatherFacts() {
return getAjaxResult(WEATHERFACTSURI, WEATHERFACTSKEY, WEATHERFACTS); return getAjaxResult(WEATHERFACTSURI, WEATHERFACTSKEY, WEATHERFACTS);
} }
@ -62,7 +60,7 @@ public class WeatherForecastController extends BaseController {
* */ * */
@ApiOperation("气象预警查询") @ApiOperation("气象预警查询")
@PostMapping(value = "/meteorologicalEarlyWarning") @PostMapping(value = "/meteorologicalEarlyWarning")
public AjaxResult meteorologicalEarlyWarning() throws HttpException, IOException { public AjaxResult meteorologicalEarlyWarning() {
return getAjaxResult(METEOROLOGICALEARLYWARNINGURI, METEOROLOGICALEARLYWARNINGKEY, METEOROLOGICALEARLYWARNING); return getAjaxResult(METEOROLOGICALEARLYWARNINGURI, METEOROLOGICALEARLYWARNINGKEY, METEOROLOGICALEARLYWARNING);
} }
@ -71,7 +69,7 @@ public class WeatherForecastController extends BaseController {
* */ * */
@ApiOperation("气象预警数量查询") @ApiOperation("气象预警数量查询")
@PostMapping(value = "/queryTheNumberOfMeteorologicalWarning") @PostMapping(value = "/queryTheNumberOfMeteorologicalWarning")
public AjaxResult queryTheNumberOfMeteorologicalWarning() throws HttpException, IOException { public AjaxResult queryTheNumberOfMeteorologicalWarning() {
JSONObject jsonObject = new JSONObject(); JSONObject jsonObject = new JSONObject();
JSONObject cacheObject = redisCache.getCacheObject(METEOROLOGICALEARLYWARNING); JSONObject cacheObject = redisCache.getCacheObject(METEOROLOGICALEARLYWARNING);
@ -84,24 +82,28 @@ public class WeatherForecastController extends BaseController {
for (DcRegion dcRegion : list) { for (DcRegion dcRegion : list) {
OkHttp okHttp = new OkHttp(); try {
OkHttp okHttp = new OkHttp();
Response response // 请求响应 Response response // 请求响应
= okHttp = okHttp
.headers(new HashMap<>()) .headers(new HashMap<>())
.url(METEOROLOGICALEARLYWARNINGURI + dcRegion.getLongitude() + "," + dcRegion.getLatitude() + KEY + METEOROLOGICALEARLYWARNINGKEY) // 请求地址 .url(METEOROLOGICALEARLYWARNINGURI + dcRegion.getLongitude() + "," + dcRegion.getLatitude() + KEY + METEOROLOGICALEARLYWARNINGKEY) // 请求地址
.get(); // 请求方法 .get(); // 请求方法
if (response.body() != null) { if (response.body() != null) {
JSONObject jsonResult = JSONObject.parseObject(response.body().string()); JSONObject jsonResult = JSONObject.parseObject(response.body().string());
if (jsonResult.getInteger("code") == 200) { if (jsonResult.getInteger("code") == 200) {
jsonObject.put(METEOROLOGICALEARLYWARNING + dcRegion.getId(), extracted(jsonResult, "warning")); jsonObject.put(METEOROLOGICALEARLYWARNING + dcRegion.getId(), extracted(jsonResult, "warning"));
} else { } else {
return AjaxResult.error(jsonResult.getInteger("code"), "请求失败"); return AjaxResult.error(jsonResult.getInteger("code"), "请求失败");
}
} }
} catch (Exception e) {
return AjaxResult.error(500, "连接异常,请检查网络");
} }
} }
} }
@ -131,11 +133,11 @@ public class WeatherForecastController extends BaseController {
* */ * */
@ApiOperation("逐小时天气查询") @ApiOperation("逐小时天气查询")
@PostMapping(value = "/hourlyWeather") @PostMapping(value = "/hourlyWeather")
public AjaxResult hourlyWeather() throws HttpException, IOException { public AjaxResult hourlyWeather() {
return getAjaxResult(HOURLYWEATHERURI, HOURLYWEATHERKEY, HOURLYWEATHER); return getAjaxResult(HOURLYWEATHERURI, HOURLYWEATHERKEY, HOURLYWEATHER);
} }
private AjaxResult getAjaxResult(String uri, String accessKey, String redisKey) throws HttpException, IOException { private AjaxResult getAjaxResult(String uri, String accessKey, String redisKey) {
JSONObject cacheObject = redisCache.getCacheObject(redisKey); JSONObject cacheObject = redisCache.getCacheObject(redisKey);
@ -150,34 +152,42 @@ public class WeatherForecastController extends BaseController {
JSONObject jsonObject = new JSONObject(); JSONObject jsonObject = new JSONObject();
for (DcRegion dcRegion : list) { for (DcRegion dcRegion : list) {
OkHttp okHttp = new OkHttp();
Response response // 请求响应 try {
= okHttp
.headers(new HashMap<>()) OkHttp okHttp = new OkHttp();
.url(uri + dcRegion.getLongitude() + "," + dcRegion.getLatitude() + KEY + accessKey) // 请求地址
.get(); // 请求方法 Response response // 请求响应
= okHttp
.headers(new HashMap<>())
.url(uri + dcRegion.getLongitude() + "," + dcRegion.getLatitude() + KEY + accessKey) // 请求地址
.get(); // 请求方法
if (response.body() != null) { if (response.body() != null) {
JSONObject jsonResult = JSONObject.parseObject(response.body().string()); JSONObject jsonResult = JSONObject.parseObject(response.body().string());
if (jsonResult.getInteger("code") == 200) { if (jsonResult.getInteger("code") == 200) {
if (Objects.equals(redisKey, WEATHERFACTS)) { if (Objects.equals(redisKey, WEATHERFACTS)) {
jsonObject.put(redisKey + dcRegion.getId(), extracted(jsonResult, "now")); jsonObject.put(redisKey + dcRegion.getId(), extracted(jsonResult, "now"));
} else if (Objects.equals(redisKey, METEOROLOGICALEARLYWARNING)) { } else if (Objects.equals(redisKey, METEOROLOGICALEARLYWARNING)) {
jsonObject.put(redisKey + dcRegion.getId(), extracted(jsonResult, "warning")); jsonObject.put(redisKey + dcRegion.getId(), extracted(jsonResult, "warning"));
} else if (Objects.equals(redisKey, HOURLYWEATHER)) { } else if (Objects.equals(redisKey, HOURLYWEATHER)) {
jsonObject.put(redisKey + dcRegion.getId(), extracted(jsonResult, "hourly")); jsonObject.put(redisKey + dcRegion.getId(), extracted(jsonResult, "hourly"));
}
} else {
return AjaxResult.error(jsonResult.getInteger("code"), "请求失败");
} }
} else {
return AjaxResult.error(jsonResult.getInteger("code"), "请求失败");
} }
} catch (Exception e) {
return AjaxResult.error(500, "连接异常,请检查网络");
} }
} }

Loading…
Cancel
Save