Spring Cloud学习笔记5——天气预报系统(4)为天气预报制作
开发环境
JDK8+
Gradle4+
Redis 3.2.100
Apache HttpClient 4.5.3
Spring Boot Web Starter
Spring Boot Data Redis Starter
Spring Boot Quartz Starter
Quartz Scheduler
Spring Boot Thymeleaf Starter 2.0.0.M4
Thymeleaf 3.0.7.RELEASE
Bootstrap 4.0.0-beta.2
天气预报服务的功能
- 按照不同的城市来进行查询
- 查询近几天的天气信息
- 界面简洁、优雅
天气预报服务的API
获取到该城市ID的天气预报信息:GET/report/cityId/{cityId}
新建项目
复制之前的micro-weather-quartz
项目,将副本改名为micro-weather-report
修改源码
修改build.gradle
配置,加入thymeleaf
的依赖:
//依赖关系
dependencies {
//该依赖用于编译阶段
compile('org.springframework.boot:spring-boot-starter-web')
//HttpClient
compile('org.apache.httpcomponents:httpclient:4.5.6')
//Redis
compile('org.springframework.boot:spring-boot-starter-data-redis')
//Quartz
compile('org.springframework.boot:spring-boot-starter-quartz')
//添加Spring Boot Thymeleaf Starter的依赖
compile('org.springframework.boot:spring-boot-starter-thymeleaf')
//该依赖用于测试阶段
testCompile('org.springframework.boot:spring-boot-starter-test')
}
在com.study.spring.cloud.weather.service
包下新建接口WeatherReportService
:
package com.study.spring.cloud.weather.service;
import com.study.spring.cloud.weather.vo.Weather;
public interface WeatherReportService {
//根据城市id查询天气信息
Weather getDataByCityId(String cityId);
}
在com.study.spring.cloud.weather.service
包下新建类WeatherReportServiceImpl
:
package com.study.spring.cloud.weather.service;
import com.study.spring.cloud.weather.vo.Weather;
import com.study.spring.cloud.weather.vo.WeatherResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class WeatherReportServiceImpl implements WeatherReportService {
@Autowired
private WeatherDataService weatherDataService;
@Override
public Weather getDataByCityId(String cityId) {
WeatherResponse resp=weatherDataService.getDataByCityId(cityId);
return resp.getData();
}
}
在com.study.spring.cloud.weather.controller
包下新建类WeatherReportController
:
package com.study.spring.cloud.weather.controller;
import com.study.spring.cloud.weather.service.CityDataService;
import com.study.spring.cloud.weather.service.WeatherReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
@RestController
@RequestMapping("/report")
public class WeatherReportController {
@Autowired
private CityDataService cityDataService;
@Autowired
private WeatherReportService weatherReportService;
@GetMapping("/cityId/{cityId}")
//@PathVariable:标识从路径中获取参数
public ModelAndView getReportByCityId(@PathVariable("cityId") String cityId,Model model) throws Exception {
model.addAttribute("title", "天气预报");
model.addAttribute("cityId", cityId);
model.addAttribute("cityList", cityDataService.listCity());
model.addAttribute("report", weatherReportService.getDataByCityId(cityId));
return new ModelAndView("weather/report","reportModel",model);
}
}
修改application.properties
配置:
#热部署静态文件
spring.thymeleaf.cache=false
在resources
的templates
目录下创建weather
目录,在weather
目录下新建前端页面report.html
:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>天气预报</title>
</head>
<body>
<h3 th:text="${reportModel.title}">天气</h3>
<select>
<option th:each="city : ${reportModel.cityList}"
th:value="${city.cityId}" th:text="${city.cityName}"
th:selected="${city.cityId eq reportModel.cityId}"></option>
</select>
<h1 th:text="${reportModel.report.city}">城市名称</h1>
<p>
空气质量指数:<span th:text="${reportModel.report.aqi}"></span>
</p>
<p>
当前温度:<span th:text="${reportModel.report.wendu}"></span>
</p>
<p>
温馨提示:<span th:text="${reportModel.report.ganmao}"></span>
</p>
<div th:each="forecast : ${reportModel.report.forecast}">
<div>
<p th:text="${forecast.date}">日期</p>
<p th:text="${forecast.type}">天气类型</p>
<p th:text="${forecast.high}">最高温度</p>
<p th:text="${forecast.low}">最低温度</p>
<p th:text="${forecast.fengxiang}">风向</p>
</div>
</div>
</body>
</html>