SpringCloud 是一套分布式的微服务解决方案,里面包含的一些组件统统都是基于springboot构建的 所以说SpringCloud是有很多SpringBoot应用所组成的。springBoot是整个SpringCloud的基石。
天气预报系统:
① 环境搭建
IntelliJ IDEA 开发工具 , gradle4+ , 打击一个springBoot的Web项目
②数据来源
天气预报系统的数据来源:
eg:http://wthrcdn.etouch.cn/weather_mini?city=深圳
http://wthrcdn.etouch.cn/weather_mini?citykey=101280601
http://mobile.weather.com.cn/js/citylist.xml
{"data":{"yesterday":{"date":"27日星期二","high":"高温 22℃","fx":"无持续风向","low":"低温 16℃","fl":"<![CDATA[<3级]]>","type":"多云"},"city":"深圳","aqi":"51","forecast":[{"date":"28日星期三","high":"高温 25℃","fengli":"<![CDATA[<3级]]>","low":"低温 19℃","fengxiang":"无持续风向","type":"多云"},{"date":"1日星期四","high":"高温 24℃","fengli":"<![CDATA[<3级]]>","low":"低温 18℃","fengxiang":"无持续风向","type":"多云"},{"date":"2日星期五","high":"高温 24℃","fengli":"<![CDATA[<3级]]>","low":"低温 19℃","fengxiang":"无持续风向","type":"阵雨"},{"date":"3日星期六","high":"高温 25℃","fengli":"<![CDATA[<3级]]>","low":"低温 21℃","fengxiang":"无持续风向","type":"多云"},{"date":"4日星期天","high":"高温 25℃","fengli":"<![CDATA[<3级]]>","low":"低温 21℃","fengxiang":"无持续风向","type":"阵雨"}],"ganmao":"各项气象条件适宜,无明显降温过程,发生感冒机率较低。","wendu":"26"},"status":1000,"desc":"OK"}
③需要的依赖:
搭建一个SpringBoot 的gradle项目 结果如下:
④根据天气预报信息返回的数据创建四个类: 每个类都实现Serlalizable接口,同时提供Set get方法。
Weather类:
private static final long serialVersionUID = 1L; private String city; private String aqi; private String ganmao; private String wendu; private List<Forecast> forecast; private Yesterday yesterday;
Forecast类
private static final long serialVersionUID = 1L; private String date; private String high; private String fengli; private String low; private String fengxiang; private String type;
Yesterday类
private static final long serialVersionUID = 1L; private String date; private String high; private String low; private String fx; private String fl; private String type;
WeatherResponse类
private static final long serialVersionUID = 1L; private Weather data; private Integer status; private String desc;
⑤创建Service类 先创建一个接口 WeatherDataService 接着写他的实现类
public interface WeatherDataService { /** * 根据cityName 获取到该城市的天气情况 * @param cityName * @return */ public WeatherResponse getDataByCityName(String cityName); /** * 根据cityKey 获取到该城市的天气情况 * @param cityKey * @return */ public WeatherResponse getDataByCityKey(String cityKey);}
@Servicepublic class WeatherDataServiceImpl implements WeatherDataService { private static final String WEATHER_URL = "http://wthrcdn.etouch.cn/weather_mini?"; @Autowired private RestTemplate restTemplate; @Override public WeatherResponse getDataByCityName(String cityName) { String url = WEATHER_URL + "city=" + cityName; return getDataByUrl(url); } @Override public WeatherResponse getDataByCityKey(String cityKey) { String url = WEATHER_URL + "citykey=" + cityKey; return getDataByUrl(url); } /** * 根据传入的url返回WeatherResponse对象 * * @param url * @return */ private WeatherResponse getDataByUrl(String url) { //获取到url返回的内容 String类型 ResponseEntity<String> responseEntity = restTemplate.getForEntity(url, String.class); int statusCode = responseEntity.getStatusCodeValue(); String responseBody = null; if(statusCode == 200){ responseBody = responseEntity.getBody(); } //将json数据放到对象中 ObjectMapper mapper = new ObjectMapper(); WeatherResponse ws = null; try { ws = mapper.readValue(responseBody,WeatherResponse.class); } catch (IOException e) { e.printStackTrace(); } return ws; }}
@Configurationpublic class RestConfiguration { @Bean public RestTemplate restTemplate(){ StringHttpMessageConverter m = new StringHttpMessageConverter(Charset.forName("UTF-8")); RestTemplateBuilder restTemplateBuilder = new RestTemplateBuilder(); return restTemplateBuilder.additionalMessageConverters(m).build(); }}
重点内容 实现的内容时在service中发送url请求 然后将返回的结果封装成一个WeatherResponse实例
采用RestTemplate 来发送请求
restTemplate.getForEntity(url, String.class); 发送url之后返回一个ResponseEntity实体,
responseEntity.getStatusCodeValue();可获取到状态码 当为200时表明成功。 最后将String类型的json数据转为对象
使用的时jackson的转换
ObjectMapper mapper = new ObjectMapper();
ws = mapper.readValue(responseBody,WeatherResponse.class);
可能会出现返回的结果为乱码的现象:
处理方法:
StringHttpMessageConverter m = new StringHttpMessageConverter(Charset.forName("UTF-8")); RestTemplateBuilder restTemplateBuilder = new RestTemplateBuilder(); return restTemplateBuilder.additionalMessageConverters(m).build();
RestTempalate 字符串类型默认StringHttpMessageConverter做转码,而StringHttpMessageConverter的默认编码集是ISO8859-1,
⑥ 创建Controller类 浏览器访问测试
@RestController@RequestMapping("/weather")public class WeatherController { @Autowired private WeatherDataService weatherDataService; @GetMapping("/cityName/{cityName}") public WeatherResponse getWeatherByCityName(@PathVariable("cityName")String cityName){ return weatherDataService.getDataByCityName(cityName); } @GetMapping("/cityId/{cityId}") public WeatherResponse getWeatherByCityId(@PathVariable("cityId")String cityId){ return weatherDataService.getDataByCityKey(cityId); }}
浏览器访问:http://localhost:8080/weather/cityName/北京