Spring Cloud学习笔记10——天气预报系统微服务(4)天气预报微服务

本文介绍如何使用SpringBoot、Thymeleaf和Bootstrap搭建一个简单的天气预报应用,包括开发环境配置、项目创建、源码修改及运行步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

开发环境

  • 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配置,删除HttpClientredisquartz的依赖:

//依赖关系
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页面:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值