天气预报微服务(springBoot-report)
项目结构图:
pom文件:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.csq.study</groupId>
<artifactId>springBoot-report</artifactId>
<version>0.0.1-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
</parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Camden.SR6</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-test -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-thymeleaf -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
</dependencies>
</project>
controller层:
package com.csq.study.springboot.controller;
import java.util.ArrayList;
import java.util.List;
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 com.csq.study.springboot.service.WeatherReportService;
import com.csq.study.springboot.vo.City;
@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 {
//获取城市列表
List<City> cityList=null;
try {
//TODO 后续改为由城市数据API微服务来提供数据
cityList = new ArrayList<City>();
City city=new 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("report","reportModel",model);
}
}
service层以及实现类:
package com.csq.study.springboot.service;
import com.csq.study.springboot.vo.Weather;
public interface WeatherReportService {
//根据城市id查询天气信息
Weather getDataByCityId(String cityId);
}
package com.csq.study.springboot.service;
import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Service;
import com.csq.study.springboot.vo.Forecast;
import com.csq.study.springboot.vo.Weather;
@Service
public class WeatherReportImpl 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 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;
}
}
启动类:
package com.csq.study.springboot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
@SpringBootApplication
@EnableScheduling
public class Aplication {
public static void main(String[] args) {
SpringApplication.run(Aplication.class, args);
}
}
vo类(省略get/set方法):
city类
public class City{
private String cityId;
private String cityName;
private String cityCode;
private String province;
Forecast 类:
public class Forecast implements Serializable{
/**
* @Fields serialVersionUID : TODO
*/
private static final long serialVersionUID = 1L;
private String date;//琪日期
private String high;//最高温度
private String fengxiang;//风向
private String low;//最低温度
private String fengli;//风力
private String type;//天气类型
Weather 类:
public class Weather implements Serializable {
/**
* @Fields serialVersionUID : TODO
*/
private static final long serialVersionUID = 1L;
private String city;
private String aqi;//
private String wendu;
private String ganmao;
private Yesterday yesterday;
private List<Forecast> forecast;
WeatherResponse 类:
public class WeatherResponse implements Serializable{
/**
* @Fields serialVersionUID : TODO
*/
private static final long serialVersionUID = 1L;
private Weather data; //消息数据
private String status; //消息状态
private String desc;//消息描述
Yesterday 类:
public class Yesterday implements Serializable{
/**
* @Fields serialVersionUID : TODO
*/
private static final long serialVersionUID = 1L;
private String date;
private String high;
private String fx;
private String low;
private String fl;
private String type;
配置文件:
js文件:
/**
* report页面下拉框事件
*/
$(function(){
$("#selectCityId").change(function () {
var cityId=$("#selectCityId").val();
var url='/report/cityId/'+cityId;
window.location.href=url;
})
});
report.html:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"/>
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous"/>
<title>天气预报</title>
</head>
<body>
<div class="container">
<div class="row">
<h3 th:text="${reportModel.title}">天气</h3>
<select class="custom-select" id="selectCityId">
<option th:each="city : ${reportModel.cityList}"
th:value="${city.cityId}" th:text="${city.cityName}"
th:selected="${city.cityId eq reportModel.cityId}"></option>
</select>
</div>
<div class="row">
<h1 class="text-success" th:text="${reportModel.report.city}">城市名称</h1>
</div>
<div class="row">
<p>
空气质量指数:<span th:text="${reportModel.report.aqi}"></span>
</p>
</div>
<div class="row">
<p>
当前温度:<span th:text="${reportModel.report.wendu}"></span>
</p>
</div>
<div class="row">
<p>
温馨提示:<span th:text="${reportModel.report.ganmao}"></span>
</p>
</div>
<div class="row">
<div class="card border-info" th:each="forecast : ${reportModel.report.forecast}">
<div class="card-body text-info">
<p class="card-text" th:text="${forecast.date}">日期</p>
<p class="card-text" th:text="${forecast.type}">天气类型</p>
<p class="card-text" th:text="${forecast.high}">最高温度</p>
<p class="card-text" th:text="${forecast.low}">最低温度</p>
<p class="card-text" th:text="${forecast.fengxiang}">风向</p>
</div>
</div>
</div>
</div>
<!-- jQuery first, then Popper.js, then Bootstrap JS -->
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>
<!-- Optional JavaScript -->
<script type="text/javascript" th:src="@{/js/weather/report.js}"></script>
</body>
</html>
启动之后访问:http://localhost:8085/report/cityId/101020100/ 可以看到如下图示即成功