Spring Cloud学习笔记10——天气预报系统微服务(4)天气预报微服务
开发环境
JDK8+
Gradle4+
Spring Boot Web Starter
Spring Boot Thymeleaf Starter 2.0.0.M4
Thymeleaf 3.0.7.RELEASE
Bootstrap 4.1.3
创建项目
新建项目文件夹:
将micro-weather-report
项目中的源码文件复制粘贴到新项目文件夹中:
修改源码
修改build.gradle
配置,删除HttpClient
、redis
、quartz
的依赖:
//依赖关系
dependencies {
//该依赖用于编译阶段
compile('org.springframework.boot:spring-boot-starter-web')
//添加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
包下的WeatherReportServiceImpl
类:
package com.study.spring.cloud.weather.service;
import com.study.spring.cloud.weather.vo.Forecast;
import com.study.spring.cloud.weather.vo.Weather;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
@Service
public class WeatherReportServiceImpl implements WeatherReportService {
@Override
public Weather getDataByCityId(String cityId) {
//TODO 改为由天气数据API微服务来提供数据
Weather data=new Weather();
data.setAqi("81");
data.setCity("上海");
data.setGanmao("容易感冒!多穿衣");
data.setWendu("16");
List<Forecast> forecastList=new ArrayList<>();
Forecast forecast=new Forecast();
forecast.setDate("13日星期一");
forecast.setFengli("<![CDATA[<3级]]>");
forecast.setFengxiang("西南风");
forecast.setHigh("18");
forecast.setLow("12");
forecast.setType("阴");
forecastList.add(forecast);
forecast=new Forecast();
forecast.setDate("14日星期二");
forecast.setFengli("<![CDATA[<3级]]>");
forecast.setFengxiang("西南风");
forecast.setHigh("18");
forecast.setLow("12");
forecast.setType("阴");
forecastList.add(forecast);
forecast=new Forecast();
forecast.setDate("15日星期三");
forecast.setFengli("<![CDATA[<3级]]>");
forecast.setFengxiang("西南风");
forecast.setHigh("18");
forecast.setLow("12");
forecast.setType("阴");
forecastList.add(forecast);
forecast=new Forecast();
forecast.setDate("16日星期四");
forecast.setFengli("<![CDATA[<3级]]>");
forecast.setFengxiang("西南风");
forecast.setHigh("18");
forecast.setLow("12");
forecast.setType("阴");
forecastList.add(forecast);
forecast=new Forecast();
forecast.setDate("17日星期五");
forecast.setFengli("<![CDATA[<3级]]>");
forecast.setFengxiang("西南风");
forecast.setHigh("18");
forecast.setLow("12");
forecast.setType("阴");
forecastList.add(forecast);
data.setForecast(forecastList);
return data;
}
}
修改com.study.spring.cloud.weather.controller
包下的WeatherReportController
类:
package com.study.spring.cloud.weather.controller;
import com.study.spring.cloud.weather.service.WeatherReportService;
import com.study.spring.cloud.weather.vo.City;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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;
import java.util.ArrayList;
import java.util.List;
@RestController
@RequestMapping("/report")
public class WeatherReportController {
//在应用中添加日志
private final static Logger logger=LoggerFactory.getLogger(WeatherReportController.class);
@Autowired
private WeatherReportService weatherReportService;
@GetMapping("/cityId/{cityId}")
//@PathVariable:标识从路径中获取参数
public ModelAndView getReportByCityId(@PathVariable("cityId") String cityId,Model model) throws Exception {
//获取城市列表
//TODO 改为由城市数据API微服务来提供数据
List<City> cityList=null;
try {
//TODO 改为由城市数据API微服务来提供数据
cityList = new ArrayList<>();
City city=new City();
city.setCityId("101020100");
city.setCityName("上海");
cityList.add(city);
} catch (Exception e) {
logger.error("Exception!",e);
}
model.addAttribute("title", "天气预报");
model.addAttribute("cityId", cityId);
model.addAttribute("cityList", cityList);
model.addAttribute("report", weatherReportService.getDataByCityId(cityId));
return new ModelAndView("weather/report","reportModel",model);
}
}
修改com.study.spring.cloud.weather.vo
包下的City
类,去掉其中xml
相关解析代码:
package com.study.spring.cloud.weather.vo;
public class City {
private String cityId;
private String cityName;
private String cityCode;
private String province;
public String getCityId() {
return cityId;
}
public void setCityId(String cityId) {
this.cityId = cityId;
}
public String getCityName() {
return cityName;
}
public void setCityName(String cityName) {
this.cityName = cityName;
}
public String getCityCode() {
return cityCode;
}
public void setCityCode(String cityCode) {
this.cityCode = cityCode;
}
public String getProvince() {
return province;
}
public void setProvince(String province) {
this.province = province;
}
}
此时src
目录结构如下:
运行
注意一定要先运行Redis
运行应用:
运行结果如下:
访问http://localhost:8080/report/cityId/101020100
页面: