版权声明:此博客为个人博客,不涉及商业用途,仅提供学习参考,内容均来自个人原创以及互联网转载和摘录。 --------------------- 本文来自 路西法Lucifer 的CSDN 博客 ,全文地址请点击: https://blog.csdn.net/qq_37495786/article/details/82826561
项目结构:
通过城市的名字获取天气数据:
http://wthrcdn.etouch.cn/weather_mini?city=北京
通过城市id获得天气数据(可以自己在百度搜索城市的id)
http://wthrcdn.etouch.cn/weather_mini?citykey=101010100
1.通过Gradle导入相关jar包:
ps:
推荐两个我常用的中央仓库:
2.这里我用的是谷歌浏览器的插件PostMan,
根据返回给我们的数据参数去创建实体类。
3.创建实体类。
1)Weather:
package com.lucifer.demo.pojo;
import java.io.Serializable;
import java.util.List;
/**
* @author: Lucifer
* @create: 2018-09-23 23:00
* @description: 天气信息
**/
public class Weather implements Serializable {
/*
http://wthrcdn.etouch.cn/weather_mini?city=北京
{
"data":{
"yesterday":{
"date":"22日星期六", "high":"高温 24℃", "fx":"西北风", "low":"低温 13℃", "fl":"<![CDATA[3-4级]]>", "type":"晴"
},"city":"北京", "aqi":"35", "forecast":[{
"date":"23日星期天", "high":"高温 24℃", "fengli":"<![CDATA[4-5级]]>", "low":"低温 12℃", "fengxiang":"北风", "type":"晴"
},{
"date":"24日星期一", "high":"高温 23℃", "fengli":"<![CDATA[<3级]]>", "low":"低温 11℃", "fengxiang":"无持续风向", "type":
"晴"
},{
"date":"25日星期二", "high":"高温 23℃", "fengli":"<![CDATA[<3级]]>", "low":"低温 13℃", "fengxiang":"南风", "type":"多云"
},{
"date":"26日星期三", "high":"高温 22℃", "fengli":"<![CDATA[<3级]]>", "low":"低温 14℃", "fengxiang":"南风", "type":"多云"
},{
"date":"27日星期四", "high":"高温 22℃", "fengli":"<![CDATA[3-4级]]>", "low":"低温 14℃", "fengxiang":"南风", "type":"阴"
}],"ganmao":"各项气象条件适宜,无明显降温过程,发生感冒机率较低。", "wendu":"14"
},"status":1000, "desc":"OK"
}*/
private String city;
private Yesterday yesterday;
private String aqi;
private String ganmao;
private String wendu;
private List<Forecast> forecast;
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public Yesterday getYesterday() {
return yesterday;
}
public void setYesterday(Yesterday yesterday) {
this.yesterday = yesterday;
}
public String getAqi() {
return aqi;
}
public void setAqi(String aqi) {
this.aqi = aqi;
}
public String getGanmao() {
return ganmao;
}
public void setGanmao(String ganmao) {
this.ganmao = ganmao;
}
public String getWendu() {
return wendu;
}
public void setWendu(String wendu) {
this.wendu = wendu;
}
public List<Forecast> getForecast() {
return forecast;
}
public void setForecast(List<Forecast> forecast) {
this.forecast = forecast;
}
}
2)Yesterday:
package com.lucifer.demo.pojo;
import java.io.Serializable;
/**
* 昨日天气
*
* @author: Lucifer
* @create: 2018-09-23 23:10
* @description:
**/
public class Yesterday implements Serializable {
/* "yesterday":{
"date":"22日星期六", "high":"高温 24℃", "fx":"西北风", "low":"低温 13℃", "fl":"<![CDATA[3-4级]]>", "type":"晴"
},*/
private String date;
private String high;
private String fx;
private String low;
private String fl;
private String type;
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getHigh() {
return high;
}
public void setHigh(String high) {
this.high = high;
}
public String getFx() {
return fx;
}
public void setFx(String fx) {
this.fx = fx;
}
public String getLow() {
return low;
}
public void setLow(String low) {
this.low = low;
}
public String getFl() {
return fl;
}
public void setFl(String fl) {
this.fl = fl;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
}
3)Forecast:
package com.lucifer.demo.pojo;
import java.io.Serializable;
/**
* 未来天气
*
* @author: Lucifer
* @create: 2018-09-23 23:10
* @description:
**/
public class Forecast implements Serializable {
/* "forecast":[{
"date":"23日星期天", "high":"高温 24℃", "fengli":"<![CDATA[4-5级]]>", "low":"低温 12℃", "fengxiang":"北风", "type":"晴"
},{
"date":"24日星期一", "high":"高温 23℃", "fengli":"<![CDATA[<3级]]>", "low":"低温 11℃", "fengxiang":"无持续风向", "type":
"晴"
},{
"date":"25日星期二", "high":"高温 23℃", "fengli":"<![CDATA[<3级]]>", "low":"低温 13℃", "fengxiang":"南风", "type":"多云"
},{
"date":"26日星期三", "high":"高温 22℃", "fengli":"<![CDATA[<3级]]>", "low":"低温 14℃", "fengxiang":"南风", "type":"多云"
},{
"date":"27日星期四", "high":"高温 22℃", "fengli":"<![CDATA[3-4级]]>", "low":"低温 14℃", "fengxiang":"南风", "type":"阴"
}]*/
private String date;
private String high;
private String fengli;
private String low;
private String fengxiang;
private String type;
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getHigh() {
return high;
}
public void setHigh(String high) {
this.high = high;
}
public String getFengli() {
return fengli;
}
public void setFengli(String fengli) {
this.fengli = fengli;
}
public String getLow() {
return low;
}
public void setLow(String low) {
this.low = low;
}
public String getFengxiang() {
return fengxiang;
}
public void setFengxiang(String fengxiang) {
this.fengxiang = fengxiang;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
}
4.创建service接口:
WeatherDataService:
package com.lucifer.demo.Service;
import com.lucifer.demo.pojo.WeatherResponse;
/**
* 天气数据接口
*/
public interface WeatherDataService {
/**
* 根据城市ID查询天气数据
* @param CityId
* @return
*/
WeatherResponse getDataByCityId(String CityId);
/**
* 根据城市名称查询天气数据
* @param cityName
* @return
*/
WeatherResponse getDataByCityName(String cityName);
}
5.创建实现类:
package com.lucifer.demo.Service;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.lucifer.demo.pojo.WeatherResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
/**
* @author: Lucifer
* @create: 2018-09-23 23:24
* @description: 实现类
**/
@Service
public class WeatherDataServiceImpl implements WeatherDataService {
private static final String WEATHER_URI = "http://wthrcdn.etouch.cn/weather_mini?";
@Autowired
private RestTemplate restTemplate;
@Override
public WeatherResponse getDataByCityId(String cityId) {
String uri = WEATHER_URI + "citykey=" + cityId;
return this.getWeatherResponse(uri);
}
@Override
public WeatherResponse getDataByCityName(String cityName) {
String uri = WEATHER_URI + "city=" + cityName;
return this.getWeatherResponse(uri);
}
private WeatherResponse getWeatherResponse(String uri) {
ResponseEntity<String> respString = restTemplate.getForEntity(uri, String.class);
ObjectMapper objectMapper = new ObjectMapper();
WeatherResponse resp = null;
String strBody = null;
if (respString.getStatusCodeValue() == 200) {
strBody = respString.getBody();
}
try {
resp = objectMapper.readValue(strBody, WeatherResponse.class);
} catch (Exception e) {
e.printStackTrace();
}
return resp;
}
}
6.创建controller类:
package com.lucifer.demo.Controller;
import com.lucifer.demo.Service.WeatherDataService;
import com.lucifer.demo.pojo.WeatherResponse;
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;
/**
* @author: Lucifer
* @create: 2018-09-23 23:45
* @description:
**/
@RestController
@RequestMapping(value = "/weather")
public class WeatherController {
@Autowired
private WeatherDataService weatherDataService;
@GetMapping(value = "/cityId/{cityId}")
public WeatherResponse getWeatherByCityId(@PathVariable("cityId") String cityId){
return weatherDataService.getDataByCityId(cityId);
}
@GetMapping(value = "/cityName/{cityName}")
public WeatherResponse getWeatherByCityName(@PathVariable("cityName") String cityName){
return weatherDataService.getDataByCityName(cityName);
}
}
7.RestConfiguration 配置类:
package com.lucifer.demo.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;
/**
* @author: Lucifer
* @create: 2018-09-23 23:51
* @description:
**/
@Configuration
public class RestConfiguration {
@Autowired
private RestTemplateBuilder restTemplateBuilder;
@Bean
public RestTemplate restTemplate(){
return restTemplateBuilder.build();
}
}
8.通过localhost:8080/weather/cityName/北京