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);
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
【备注】 该项目是个人毕设项目,答辩评审分达到95分,代码都经过调试测试,确保可以运行!欢迎下载使用,可用于小白学习、进阶。 该资源主要针对计算机、通信、人工智能、自动化等相关专业的学生、老师或从业者下载使用,亦可作为期末课程设计、课程大作业、毕业设计等。 项目整体具有较高的学习借鉴价值!基础能力强的可以在此基础上修改调整,以实现不同的功能。 欢迎下载,欢迎沟通,互相学习,共同进步!提供答疑!
资源推荐
资源详情
资源评论
收起资源包目录
毕设源码-基于springboot的天气预报系统.zip (25个子文件)
project_code
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
资源评论
- qq_576624092024-02-03果断支持这个资源,资源解决了当前遇到的问题,给了新的灵感,感谢分享~
z同学的编程之路
- 粉丝: 2187
- 资源: 2132
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功