创建项目
新建项目文件夹:
将hello-world
项目中的源码文件复制粘贴到新项目文件夹中:
修改源码
到https://mvnrepository.com上搜索HttpClient
,点击Apache HttpClient
:
选择需要的版本进入,点击Gradle
:
修改build.gradle
配置,加入HttpClient
的依赖:
//依赖关系
dependencies {
//该依赖用于编译阶段
compile('org.springframework.boot:spring-boot-starter-web')
//HttpClient
compile('org.apache.httpcomponents:httpclient:4.5.6')
//该依赖用于测试阶段
testCompile('org.springframework.boot:spring-boot-starter-test')
}
数据来源
- http://wthrcdn.etouch.cn/weather_mini?city=上海
- http://wthrcdn.etouch.cn/weather_mini?citykey=101020100
- http://mobile.weather.com.cn/js/citylist.xml
网页回应如下:
{
"data":
{
"yesterday":
{
"date":"6日星期二",
"high":"高温 21℃",
"fx":"西风",
"low":"低温 17℃",
"fl":"<![CDATA[<3级]]>",
"type":"小雨"
},
"city":"上海",
"aqi":"38",
"forecast":
[
{
"date":"7日星期三",
"high":"高温 19℃",
"fengli":"<![CDATA[<3级]]>",
"low":"低温 13℃",
"fengxiang":"西北风",
"type":"小雨"
},
{
"date":"8日星期四",
"high":"高温 15℃",
"fengli":"<![CDATA[3-4级]]>",
"low":"低温 10℃",
"fengxiang":"西北风",
"type":"小雨"
},
{
"date":"9日星期五",
"high":"高温 17℃",
"fengli":"<![CDATA[<3级]]>",
"low":"低温 12℃",
"fengxiang":"东风",
"type":"晴"
},
{
"date":"10日星期六",
"high":"高温 19℃",
"fengli":"<![CDATA[<3级]]>",
"low":"低温 14℃",
"fengxiang":"东南风",
"type":"小雨"
},
{
"date":"11日星期天",
"high":"高温 19℃",
"fengli":"<![CDATA[<3级]]>",
"low":"低温 14℃",
"fengxiang":"东北风",
"type":"小雨"
}
],
"ganmao":"天冷空气湿度大,易发生感冒,请注意适当增加衣服,加强自我防护避免感冒。",
"wendu":"17"
},
"status":1000,
"desc":"OK"
}
VO
新建包:
根据数据来源显示的回应字段新建四个类:Weather
、Yesterday
、Forecast
、WeatherResponse
:
package com.study.spring.cloud.weather.vo;
import java.io.Serializable;
import java.util.List;
//实现序列化接口
public class Weather implements Serializable {
//生成序列化ID
private static final long serialVersionUID = 1L;
private Yesterday yesterday;
private String city;
private String aqi;
private List<Forecast> forecast;
private String ganmao;
private String wendu;
public Yesterday getYesterday() {
return yesterday;
}
public void setYesterday(Yesterday yesterday) {
this.yesterday = yesterday;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getAqi() {
return aqi;
}
public void setAqi(String aqi) {
this.aqi = aqi;
}
public List<Forecast> getForecast() {
return forecast;
}
public void setForecast(List<Forecast> forecast) {
this.forecast = forecast;
}
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;
}
}
package com.study.spring.cloud.weather.vo;
import java.io.Serializable;
//实现序列化接口
public class Yesterday implements Serializable {
//生成序列化ID
private static final long serialVersionUID = 1L;
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;
}
}
package com.study.spring.cloud.weather.vo;
import java.io.Serializable;
//实现序列化接口
public class Forecast implements Serializable {
//生成序列化ID
private static final long serialVersionUID = 1L;
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;
}
}
package com.study.spring.cloud.weather.vo;
import java.io.Serializable;
//实现序列化接口
public class WeatherResponse implements Serializable {
//生成序列化ID
private static final long serialVersionUID = 1L;
private Weather data;
private Integer status;
private String desc;
public Weather getData() {
return data;
}
public void setData(Weather data) {
this.data = data;
}
public Integer getStatus() {
return status;
}
public void setStatus(Integer status) {
this.status = status;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
}
Service
新建包:
新建接口:
package com.study.spring.cloud.weather.service;
import com.study.spring.cloud.weather.vo.WeatherResponse;
public interface WeatherDataService {
//根据城市ID查询天气数据
WeatherResponse getDataByCityId(String cityId);
//根据城市名称查询天气数据
WeatherResponse getDataByCityName(String cityName);
}
新建实现类:
package com.study.spring.cloud.weather.service;
import java.io.IOException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.study.spring.cloud.weather.vo.WeatherResponse;
@Service
public class WeatherDataServiceImpl implements WeatherDataService {
private static final String WEATHER_URI="http://wthrcdn.etouch.cn/weather_mini?";
@Autowired
//对rest客户端的封装
private RestTemplate restTemplate;
@Override
public WeatherResponse getDataByCityId(String cityId) {
String uri=WEATHER_URI + "citykey=" + cityId;
return this.doGetWeather(uri);
}
@Override
public WeatherResponse getDataByCityName(String cityName) {
String uri=WEATHER_URI + "city=" + cityName;
return this.doGetWeather(uri);
}
private WeatherResponse doGetWeather(String uri) {
//得到json字符串
ResponseEntity<String> respString = restTemplate.getForEntity(uri, String.class);
//将json字符串转为想要的WeatherResponse类型
ObjectMapper mapper=new ObjectMapper();
WeatherResponse resp=null;
String strBody=null;
//判断ResponseEntity的状态码是否为200,为200时取出strBody
if(respString.getStatusCodeValue()==200) {
strBody=respString.getBody();
}
try {
/*
* strBody:要解析的参数内容,从respString获取
* WeatherResponse.class:要转成的对象类型
*/
resp=mapper.readValue(strBody,WeatherResponse.class);
}catch(IOException e) {
e.printStackTrace();
}
return resp;
}
}
Controller
新建类:
package com.study.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.study.spring.cloud.weather.service.WeatherDataService;
import com.study.spring.cloud.weather.vo.WeatherResponse;
@RestController
@RequestMapping("/weather")
public class WeatherController {
@Autowired
private WeatherDataService weatherDataService;
@GetMapping("/cityId/{cityId}")
//@PathVariable:标识从路径中获取参数
public WeatherResponse getWeatherByCityId(@PathVariable("cityId") String cityId) {
return weatherDataService.getDataByCityId(cityId);
}
@GetMapping("/cityName/{cityName}")
//@PathVariable:标识从路径中获取参数
public WeatherResponse getWeatherByCityName(@PathVariable("cityName") String cityName) {
return weatherDataService.getDataByCityName(cityName);
}
}
config
新建包:
新建类:
package com.study.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;
@Configuration
//rest配置类,配置restTemplate
public class RestConfiguration {
@Autowired
private RestTemplateBuilder builder;
@Bean
public RestTemplate restTemplate() {
return builder.build();
}
}
运行
运行结果如下:
访问http://localhost:8080/weather/cityId/101020100
页面:
访问http://localhost:8080/weather/cityName/上海
页面: