本文共 4356 字,大约阅读时间需要 14 分钟。
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.1.3
获取到该城市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;@Servicepublic 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
的static
目录下创建js
目录,再在js
目录下创建weather
目录,在weather
目录下新建report.js
文件:
/** * report页面下拉框事件 */$(function(){ $("#selectCityId").change(function () { var cityId=$("#selectCityId").val(); var url='/report/cityId/'+cityId; window.location.href=url; })});
在resources
的templates
目录下创建weather
目录,在weather
目录下新建前端页面report.html
:
天气预报 天气
城市名称
空气质量指数:
当前温度:
温馨提示:
日期
天气类型
最高温度
最低温度
风向
注意一定要先运行Redis
。
运行应用:
运行结果如下: 访问http://localhost:8080/report/cityId/101020100页面: 更改选择的城市,页面中的天气信息会随之改变:转载地址:http://igiwi.baihongyu.com/