package com.weather.service.impl;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.weather.service.WeatherDataService;
import com.weather.vo.WeatherResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
@Slf4j
@Service
public class WeatherDataServiceImpl implements WeatherDataService {
@Autowired
private RestTemplate restTemplate;
@Autowired
private StringRedisTemplate redisTemplate;
private static final long TIME_OUT = 1800L; //redis超时时间为30分钟
private static final String WEATHER_URI = "http://wthrcdn.etouch.cn/weather_mini?";
@Override
public WeatherResponse getDataByCityId(final String cityId) {
String uri = WEATHER_URI + "citykey=" + cityId;
return this.doGetWeather(uri);
}
@Override
public WeatherResponse getDataByCityName(final String cityName) {
String uri = WEATHER_URI + "city=" + cityName;
return this.doGetWeather(uri);
}
private WeatherResponse doGetWeather(String uri) {
ObjectMapper mapper = new ObjectMapper();
WeatherResponse resp = null;
String strBody = null;
// 先从缓存中查询数据
if (redisTemplate.hasKey(uri)) {
strBody = redisTemplate.opsForValue().get(uri);
} else {
// 缓存中没有就调用接口查询天气数据
ResponseEntity<String> respString = restTemplate.getForEntity(uri, String.class);
if (respString.getStatusCodeValue() == 200) {
strBody = respString.getBody();
}
// 把查询出来的数据写入缓存中
redisTemplate.opsForValue().set(uri,strBody,TIME_OUT,TimeUnit.SECONDS);
}
try {
resp = mapper.readValue(strBody, WeatherResponse.class);
} catch (IOException e) {
//e.printStackTrace();
log.error("Error!",e);
}
return resp;
}
/**
* 根据城市id来同步天气数据
*/
@Override
public void syncDataByCityId(String cityId) {
String uri = WEATHER_URI + "citykey=" + cityId;
this.saveWeatherDate(uri);
}
/**
* 把天气数据放入缓存中
*/
private void saveWeatherDate(String uri) {
String strBody = null;
ResponseEntity<String> respString = restTemplate.getForEntity(uri, String.class);
if (respString.getStatusCodeValue() == 200) {
strBody = respString.getBody();
}
// 把查询出来的数据写入缓存中
redisTemplate.opsForValue().set(uri,strBody,TIME_OUT,TimeUnit.SECONDS);
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
毕设项目:基于springboot的天气预报系统.zip
共25个文件
java:19个
xml:2个
gitignore:1个
需积分: 5 0 下载量 126 浏览量
2023-08-07
21:46:17
上传
评论
收藏 44KB ZIP 举报
温馨提示
基于java、jsp、servlet、mysql、springboot等技术构建的系统,适合毕设项目、课设作业。资源中的源码都是经过本地编译过可运行的,下载后按照文档配置好环境就可以运行。资源项目的难度比较适中,内容都是经过助教老师审定过的,应该能够满足学习、使用需求,如果有需要的话可以放心下载使用。
资源推荐
资源详情
资源评论
收起资源包目录
毕设项目:基于springboot的天气预报系统.zip (25个子文件)
today_0801
pom.xml 2KB
src
main
resources
citylist.xml 155KB
application.properties 46B
templates
weather
report.html 3KB
static
js
weather
report.js 230B
java
com
weather
DemoApplication.java 312B
controller
WeatherReportController.java 1KB
WeatherController.java 992B
utils
XmlBuilder.java 687B
service
CityDataService.java 173B
WeatherDataService.java 273B
WeatherReportService.java 155B
impl
CityDataServiceImpl.java 1KB
WeatherDataServiceImpl.java 3KB
WeatherReportServiceImpl.java 721B
job
WeatherDataSyncJob.java 1KB
vo
Yeaterday.java 364B
Weather.java 414B
City.java 595B
Forecast.java 374B
CityList.java 277B
WeatherResponse.java 300B
config
QuartzConfig.java 877B
RestConfig.java 531B
.gitignore 308B
共 25 条
- 1
资源评论
独处东汉
- 粉丝: 366
- 资源: 822
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功