亚洲视频二区_亚洲欧洲日本天天堂在线观看_日韩一区二区在线观看_中文字幕不卡一区

公告:魔扣目錄網(wǎng)為廣大站長(zhǎng)提供免費(fèi)收錄網(wǎng)站服務(wù),提交前請(qǐng)做好本站友鏈:【 網(wǎng)站目錄:http://www.430618.com 】, 免友鏈快審服務(wù)(50元/站),

點(diǎn)擊這里在線咨詢客服
新站提交
  • 網(wǎng)站:51998
  • 待審:31
  • 小程序:12
  • 文章:1030137
  • 會(huì)員:747

從一個(gè)天氣預(yù)報(bào)系統(tǒng)講起

本節(jié)通過Spring Boot技術(shù)快速實(shí)現(xiàn)一個(gè)天氣預(yù)報(bào)系統(tǒng)。

通過這個(gè)系統(tǒng),一方面可以了解Spring Boot的全面用法,為后續(xù)創(chuàng)建微服務(wù)應(yīng)用打下基礎(chǔ);另一方面,該系統(tǒng)會(huì)作為本節(jié)進(jìn)行微服務(wù)架構(gòu)改造的非常好的起點(diǎn)。

下面以前面創(chuàng)建的hello-world應(yīng)用作為基礎(chǔ)進(jìn)行改造,成為新的應(yīng)用micro-weather-basic。

大牛教大家如何用SpringBoot技術(shù)快速實(shí)現(xiàn)天氣預(yù)報(bào)系統(tǒng)

 

開發(fā)環(huán)境

為了演示本例,需要采用如下開發(fā)環(huán)境。

. JDK8。

.Gradle 4.0。

. Spring Boot Web Starter 2.0.0.M4。

Apache HttpClient 4.5.3。

數(shù)據(jù)來源

天氣的數(shù)據(jù)是天氣預(yù)報(bào)的實(shí)現(xiàn)基礎(chǔ)。本應(yīng)用與實(shí)際的天氣數(shù)據(jù)無關(guān),理論上可以兼容多種數(shù)據(jù)來源。但為求簡(jiǎn)單,我們?cè)诰W(wǎng)上找了一個(gè)免費(fèi)、可用的天氣數(shù)據(jù)接口。

  • ·天氣數(shù)據(jù)來源為中華萬年歷。例如以下兩種方式。

通過城市名稱獲得天氣數(shù)據(jù): http://wthrcdn.etouch.cn/weather_mini?city=深圳。

通過城市ID獲得天氣數(shù)據(jù): http://wthrcdn.etouch.cn/weather_mini?citykey=101280601。

  • ·城市ID列表。每個(gè)城市都有一個(gè)唯一的ID作為標(biāo)識(shí),見https:/waylau.com/data/citylist.xml。

調(diào)用天氣服務(wù)接口示例,這里以“深圳”城市為例,可看到如下天氣數(shù)據(jù)返回。

{
"data":{
"yesterday":{
"date" :"1日星期五",
"high" :"高溫33℃",
"fx":"無持續(xù)風(fēng)向",
"low" :"低溫26℃",
"fl":"<![CDATA[<3級(jí)]]>",
"type":"多云"
},
"city":"深圳",
"aqi" : "72",
"forecast":[
"date":"2日星期六",
"high":"高溫32℃",
"fengli":"<![CDATA[<3級(jí)]1>",
"low" :"低溫26℃",
"fengxiang":"無持續(xù)風(fēng)向",
"type" :"陣雨"
},
"date":"3日星期天",
"high":"高溫 29℃",
"fengli":"<![CDATA[5-6級(jí)]1>",
"low" :"低溫26℃",
"fengxiang":"無持續(xù)風(fēng)向",
"type":"大雨"
"date":"4日星期一",
"high":"高溫29℃",
"fengli":"<![CDATA[3-4級(jí)]1>",
"low":"低溫26℃",
"fengxiang" :"西南風(fēng)",
"type":"暴雨"
},
"date":"5日星期二",
"high":"高溫31℃",
"fengli":"<![CDATA[<3級(jí)]]>",
"low":"低溫27℃",
"fengxiang":"無持續(xù)風(fēng)向",
"type":"陣雨"
"date" :"6日星期三",
"high":"高溫32℃",
"fengli":"<![CDATA[<3級(jí)]l>",
"low":"低溫27℃",
"fengxiang" :"無持續(xù)風(fēng)向",
"type":"陣雨"
 }
"ganmao":"風(fēng)較大,陰冷潮濕,較易發(fā)生感冒,體質(zhì)較弱的朋友請(qǐng)注意適當(dāng)防護(hù)。
" wendu":"29"
},
"status": 1000,
"desc":"OK"}

通過觀察以上數(shù)據(jù),來理解每個(gè)返回字段的含義。

  • “city”:城市名稱。
  • "aqi”:空氣指數(shù)。
  • “wendu”:實(shí)時(shí)溫度。
  • “date”:日期,包含未來5天。
  • “high”:最高溫度。
  • “low”:最低溫度。
  • “fengli”:風(fēng)力。
  • “fengxiang”:風(fēng)向。
  • “type”:天氣類型。

以上數(shù)據(jù)是需要的天氣數(shù)據(jù)的核心數(shù)據(jù),但是,同時(shí)也要關(guān)注下面兩個(gè)字段。

  • “status”:接口調(diào)用的返回狀態(tài),返回值“1000”,意味著數(shù)據(jù)接口正常。
  • ·“desc”:接口狀態(tài)的描述,“OK”代表接口正常。

重點(diǎn)關(guān)注返回值不是“1000”的情況,這說明這個(gè)接口調(diào)用異常。

初始化一個(gè)Spring Boot項(xiàng)目

初始化一個(gè)Spring Boot項(xiàng)目“micro-weather-basic”,該項(xiàng)目可以直接以之前的“hello-world"應(yīng)用作為基礎(chǔ)進(jìn)行修改。

添加Apache HttpClient的依賴,來作為Web請(qǐng)求的客戶端。完整的依賴情況如下。

//依賴關(guān)系
dependencies {
//該依賴用于編譯階段
compile('org.springframework.boot:spring-boot-starter-web')
/添加Apache HttpClient依賴
compile('org.apache.httpcomponents:httpclient:4.5.3')
//該依賴用于測(cè)試階段
testCompile('org.springframework.boot:spring-boot-starter-test')}
大牛教大家如何用SpringBoot技術(shù)快速實(shí)現(xiàn)天氣預(yù)報(bào)系統(tǒng)

 

創(chuàng)建天氣信息相關(guān)的值對(duì)象

創(chuàng)建com.waylau.spring.cloud.weather.vo包,用于存放相關(guān)值對(duì)象。這些對(duì)象都是POJO對(duì)象,沒有復(fù)雜的業(yè)務(wù)邏輯

創(chuàng)建天氣信息類 Weather:

public class Weather implements Serializable {
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;
1/省略getter/setter方法
}

昨日天氣信息類Yesterday :

public class Yesterday implements Serializable {
private static final long serialversionUID = 1L;
private string date;
private string high;
private String fx;
private String low;
private String fl;
private String type;
//省略getter/setter方法
}

未來天氣信息類Forecast:

public class Forecast implements Serializable 
private static final long serialVersionUID =1L;
private string date;
private string high;
private string fengxiang;
private string low;
private String fengli;
private String type;
//省略getter/setter方法
}

WeatherResponse作為整個(gè)消息的返回對(duì)象:

public class WeatherResponse implements Serializable{
private static final long serialversionUID =1L;
private Weather data;1/消息數(shù)據(jù)
private String status;//消息狀態(tài)
private string desc;/l消息描述
//省略getter/setter方法
}

服務(wù)接口及實(shí)現(xiàn)

創(chuàng)建com.waylau.spring.cloud.weather.service包,用于存放服務(wù)接口及其實(shí)現(xiàn)。

下面是定義服務(wù)的兩個(gè)接口方法,一個(gè)是根據(jù)城市的ID來查詢天氣數(shù)據(jù),另一個(gè)是根據(jù)城市名稱來查詢天氣數(shù)據(jù)。

package com.waylau.spring.cloud.weather.service;
import com.waylau.spring.cloud.weather.vo.WeatherResponse;
/*★
*天氣數(shù)據(jù)服務(wù).
大
csince 1.0.o 2017年10月18日
*  @author <a href="https://waylau.com">Way Lau</a>
*/
public interface weatherDataservice {
/**
根據(jù)城市ID來查詢天氣數(shù)據(jù)
*
*@param city工d
*return
*/
WeatherResponse getDataByCityId(String cityId);
/**
*根據(jù)城市名稱來查詢天氣數(shù)據(jù)
*
*@param cityId
*@return
*/
WeatherResponse getDataByCityName(String cityName);
}

其服務(wù)實(shí)現(xiàn)WeatherDataServiceImpl為:

package com.waylau.spring.cloud.weather.service;
import JAVA.io.IOException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import com.fasterxml.jackson.databind.0bjectMApper;
import com.waylau.spring.cloud.weather.vo.WeatherResponse;
/**
*天氣數(shù)據(jù)服務(wù).
*
* @since 1.0.0 2017年10月18日
* @author <a href="https://waylau.com">Way Lau</a>
*/
@service
public class WeatherDataServiceImpl implements WeatherDataService {
Autowired
private RestTemplate restTemplate;
private final string WEATHER_API = "http://wthrcdn.etouch.cn/weath-
er_ mini";
@override
public WeatherResponse getDataByCityId(string cityId){
String uri = WEATHER_API + "?citykey=" + cityId;
return this.doGetweatherData(uri);
}
@override
public WeatherResponse getDataByCityName(String cityName){
String uri = WEATHER_API +"?city=" + cityName;
return this.doGetWeatherData (uri);
private WeatherResponse doGetWeatherData(String uri){
ResponseEntity<String> response = restTemplate.getForEntity(uri,
String.class);
String strBody = null;
if(response.getstatusCodevalue()==200){
strBody= response.getBody(;
}
objectMapper mapper = new objectMapper();
WeatherResponse weather = null;
try{
weather = mapper.readValue (strBody,WeatherResponse.class);
}catch (工OException e){
e.printStackTrace();
return weather;

其中:

. RestTemplate是一個(gè)REST客戶端,默認(rèn)采用Apache HttpClient來實(shí)現(xiàn);

·返回的天氣信息采用了Jackson來進(jìn)行反序列化,使其成為WeatherResponse對(duì)象。

控制器層

創(chuàng)建com.waylau.spring.cloud.weather.service包,用于存放控制器層代碼。控制器層暴露了RESTful API接口。

package com.waylau.spring.cloud.weather.controller;
import org.springframework.beans.factory.annotation.Autowired;
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 com.waylau.spring.cloud.weather.service.WeatherDataService;
import com.waylau.spring.cloud.weather.vo.WeatherResponse;
/★大
*天氣AP工.
*
* @since 1.0.0 2017年10月18日
* author <a href="https://waylau.com">Way Lau</a>
*/
@RestController
RequestMapping("/weather")
public class WeatherController {
@Autowired
private WeatherDataService weatherDataService;
@GetMapping("/cityId/{cityId}")
public WeatherResponse getReportByCityId(CPathVariable("cityId")
string cityId){
return weatherDataService.getDataByCityId(cityId);
}
GetMapping("/cityName/{cityName}")
public WeatherResponse getReportByCityName (CPathVariable ("cityName")
string cityName){
return weatherDataService.getDataByCityName(cityName);}

其中,@RestController會(huì)自動(dòng)將返回的數(shù)據(jù)進(jìn)行序列化,使其成為JSON數(shù)據(jù)格式。

配置類

創(chuàng)建com.waylau.spring.cloud.weather.config包,用于存放配置相關(guān)的代碼。創(chuàng)建RestConfiguration

類,該類是RestTemplate 的配置類。

package com.waylau.spring.cloud.weather.config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
/**
*REST 配置類.
*
*@since 1.0.02017年10月18日
* @author <a href="https://waylau.com" >Way Lau</a>
*/
Configuration
public class RestConfiguration {
Autowired
private RestTemplateBuilder builder;
@Bean
public RestTemplate restTemplate({
return builder .build();}
}

訪問API

運(yùn)行項(xiàng)目之后,訪問以下API來進(jìn)行測(cè)試。

  • . http://localhost:8080/weather/cityId/101280601。
  • http://localhost:8080/weather/cityName/惠州。

能看到如圖6-1所示的天氣API返回的數(shù)據(jù)。

大牛教大家如何用SpringBoot技術(shù)快速實(shí)現(xiàn)天氣預(yù)報(bào)系統(tǒng)

分享到:
標(biāo)簽:SpringBoot
用戶無頭像

網(wǎng)友整理

注冊(cè)時(shí)間:

網(wǎng)站:5 個(gè)   小程序:0 個(gè)  文章:12 篇

  • 51998

    網(wǎng)站

  • 12

    小程序

  • 1030137

    文章

  • 747

    會(huì)員

趕快注冊(cè)賬號(hào),推廣您的網(wǎng)站吧!
最新入駐小程序

數(shù)獨(dú)大挑戰(zhàn)2018-06-03

數(shù)獨(dú)一種數(shù)學(xué)游戲,玩家需要根據(jù)9

答題星2018-06-03

您可以通過答題星輕松地創(chuàng)建試卷

全階人生考試2018-06-03

各種考試題,題庫,初中,高中,大學(xué)四六

運(yùn)動(dòng)步數(shù)有氧達(dá)人2018-06-03

記錄運(yùn)動(dòng)步數(shù),積累氧氣值。還可偷

每日養(yǎng)生app2018-06-03

每日養(yǎng)生,天天健康

體育訓(xùn)練成績(jī)?cè)u(píng)定2018-06-03

通用課目體育訓(xùn)練成績(jī)?cè)u(píng)定