From 2cebe208814108600b0909b9213c29be8db3d78b Mon Sep 17 00:00:00 2001
From: "DESKTOP-G8BCEP0\\HP" <2037158277@qq.com>
Date: Mon, 26 Jul 2021 17:35:08 +0800
Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BA=86=E6=9F=A5=E8=AF=A2?=
=?UTF-8?q?=E5=A4=A9=E6=B0=94=E5=AE=9E=E5=86=B5=E7=9A=84=E6=8E=A5=E5=8F=A3?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
pom.xml | 6 ++
.../java/com/xkrs/FirePointApplication.java | 17 ++++
.../xkrs/common/config/RestConfiguration.java | 24 +++++
.../xkrs/common/config/WebSecurityConfig.java | 1 +
.../weather/controller/WeatherController.java | 28 ++++++
.../com/xkrs/weather/entity/Forecast.java | 78 +++++++++++++++++
.../java/com/xkrs/weather/entity/Weather.java | 87 +++++++++++++++++++
.../xkrs/weather/entity/WeatherResponse.java | 55 ++++++++++++
.../com/xkrs/weather/entity/Yesterday.java | 68 +++++++++++++++
.../weather/service/WeatherDataService.java | 24 +++++
.../service/impl/WeatherDataServiceImpl.java | 53 +++++++++++
11 files changed, 441 insertions(+)
create mode 100644 src/main/java/com/xkrs/common/config/RestConfiguration.java
create mode 100644 src/main/java/com/xkrs/weather/controller/WeatherController.java
create mode 100644 src/main/java/com/xkrs/weather/entity/Forecast.java
create mode 100644 src/main/java/com/xkrs/weather/entity/Weather.java
create mode 100644 src/main/java/com/xkrs/weather/entity/WeatherResponse.java
create mode 100644 src/main/java/com/xkrs/weather/entity/Yesterday.java
create mode 100644 src/main/java/com/xkrs/weather/service/WeatherDataService.java
create mode 100644 src/main/java/com/xkrs/weather/service/impl/WeatherDataServiceImpl.java
diff --git a/pom.xml b/pom.xml
index 007bdca..b284715 100644
--- a/pom.xml
+++ b/pom.xml
@@ -29,6 +29,7 @@
2.10.5
5.0.0
5.0.0
+ 4.5.2
@@ -177,6 +178,11 @@
2.4
jdk15
+
+ org.apache.httpcomponents
+ httpclient
+ ${httpclient.version}
+
diff --git a/src/main/java/com/xkrs/FirePointApplication.java b/src/main/java/com/xkrs/FirePointApplication.java
index 8de176b..4e2f1cd 100644
--- a/src/main/java/com/xkrs/FirePointApplication.java
+++ b/src/main/java/com/xkrs/FirePointApplication.java
@@ -2,6 +2,15 @@ package com.xkrs;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.context.annotation.Bean;
+import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
+import org.springframework.http.client.OkHttp3ClientHttpRequestFactory;
+import org.springframework.http.converter.HttpMessageConverter;
+import org.springframework.http.converter.StringHttpMessageConverter;
+import org.springframework.web.client.RestTemplate;
+
+import java.nio.charset.StandardCharsets;
+import java.util.List;
/**
* @author XinYi Song
@@ -13,4 +22,12 @@ public class FirePointApplication {
SpringApplication.run(FirePointApplication.class, args);
}
+ @Bean
+ public RestTemplate restTemplate() {
+ //Apache Httpclient
+ RestTemplate restTemplate = new RestTemplate(new HttpComponentsClientHttpRequestFactory());
+ restTemplate.getMessageConverters().set(1, new StringHttpMessageConverter(StandardCharsets.UTF_8));
+ return restTemplate;
+ }
+
}
diff --git a/src/main/java/com/xkrs/common/config/RestConfiguration.java b/src/main/java/com/xkrs/common/config/RestConfiguration.java
new file mode 100644
index 0000000..ce45f96
--- /dev/null
+++ b/src/main/java/com/xkrs/common/config/RestConfiguration.java
@@ -0,0 +1,24 @@
+package com.xkrs.common.config;
+
+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;
+
+import javax.annotation.Resource;
+
+/**
+ * @author XinYi Song
+ */
+@Configuration
+public class RestConfiguration {
+
+ @Resource
+ private RestTemplateBuilder restTemplateBuilder;
+
+ @Bean
+ public RestTemplate restTemplate(){
+ return restTemplateBuilder.build();
+ }
+
+}
diff --git a/src/main/java/com/xkrs/common/config/WebSecurityConfig.java b/src/main/java/com/xkrs/common/config/WebSecurityConfig.java
index 614429c..6f4958e 100644
--- a/src/main/java/com/xkrs/common/config/WebSecurityConfig.java
+++ b/src/main/java/com/xkrs/common/config/WebSecurityConfig.java
@@ -50,6 +50,7 @@ class WebSecurityConfig extends WebSecurityConfigurerAdapter {
.antMatchers(HttpMethod.GET,"/api/user/booleanUserName").permitAll()
.antMatchers(HttpMethod.GET,"/websocketTest").permitAll()
.antMatchers(HttpMethod.GET,"/selectCityName").permitAll()
+ .antMatchers(HttpMethod.GET,"/weather/cityName").permitAll()
// 所有其它请求需要身份认证
.anyRequest().authenticated()
.and()
diff --git a/src/main/java/com/xkrs/weather/controller/WeatherController.java b/src/main/java/com/xkrs/weather/controller/WeatherController.java
new file mode 100644
index 0000000..01e40c1
--- /dev/null
+++ b/src/main/java/com/xkrs/weather/controller/WeatherController.java
@@ -0,0 +1,28 @@
+package com.xkrs.weather.controller;
+
+import com.xkrs.weather.entity.WeatherResponse;
+import com.xkrs.weather.service.WeatherDataService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.*;
+
+/**
+ * @author XinYi Song
+ */
+@RestController
+@RequestMapping(value = "/weather")
+public class WeatherController {
+
+ @Autowired
+ private WeatherDataService weatherDataService;
+
+ @GetMapping("/cityId")
+ public WeatherResponse getWeatherByCityId(@RequestParam("cityId") String cityId){
+ return weatherDataService.getDataByCityId(cityId);
+ }
+
+ @GetMapping("/cityName")
+ public WeatherResponse getWeatherByCityName(@RequestParam("cityName") String cityName){
+ return weatherDataService.getDataByCityName(cityName);
+ }
+
+}
diff --git a/src/main/java/com/xkrs/weather/entity/Forecast.java b/src/main/java/com/xkrs/weather/entity/Forecast.java
new file mode 100644
index 0000000..6587ea0
--- /dev/null
+++ b/src/main/java/com/xkrs/weather/entity/Forecast.java
@@ -0,0 +1,78 @@
+package com.xkrs.weather.entity;
+
+import java.io.Serializable;
+
+/**
+ * 未来天气
+ * @author XinYi Song
+ */
+public class Forecast implements Serializable {
+
+ /* "forecast":[{
+ "date":"23日星期天", "high":"高温 24℃", "fengli":"", "low":"低温 12℃", "fengxiang":"北风", "type":"晴"
+ },{
+ "date":"24日星期一", "high":"高温 23℃", "fengli":"", "low":"低温 11℃", "fengxiang":"无持续风向", "type":
+ "晴"
+ },{
+ "date":"25日星期二", "high":"高温 23℃", "fengli":"", "low":"低温 13℃", "fengxiang":"南风", "type":"多云"
+ },{
+ "date":"26日星期三", "high":"高温 22℃", "fengli":"", "low":"低温 14℃", "fengxiang":"南风", "type":"多云"
+ },{
+ "date":"27日星期四", "high":"高温 22℃", "fengli":"", "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;
+ }
+}
diff --git a/src/main/java/com/xkrs/weather/entity/Weather.java b/src/main/java/com/xkrs/weather/entity/Weather.java
new file mode 100644
index 0000000..55923ef
--- /dev/null
+++ b/src/main/java/com/xkrs/weather/entity/Weather.java
@@ -0,0 +1,87 @@
+package com.xkrs.weather.entity;
+
+import java.io.Serializable;
+import java.util.List;
+
+/**
+ * 天气信息
+ * @author XinYi Song
+ */
+public class Weather implements Serializable {
+
+/*
+ http://wthrcdn.etouch.cn/weather_mini?city=北京
+ {
+ "data":{
+ "yesterday":{
+ "date":"22日星期六", "high":"高温 24℃", "fx":"西北风", "low":"低温 13℃", "fl":"", "type":"晴"
+ },"city":"北京", "aqi":"35", "forecast":[{
+ "date":"23日星期天", "high":"高温 24℃", "fengli":"", "low":"低温 12℃", "fengxiang":"北风", "type":"晴"
+ },{
+ "date":"24日星期一", "high":"高温 23℃", "fengli":"", "low":"低温 11℃", "fengxiang":"无持续风向", "type":
+ "晴"
+ },{
+ "date":"25日星期二", "high":"高温 23℃", "fengli":"", "low":"低温 13℃", "fengxiang":"南风", "type":"多云"
+ },{
+ "date":"26日星期三", "high":"高温 22℃", "fengli":"", "low":"低温 14℃", "fengxiang":"南风", "type":"多云"
+ },{
+ "date":"27日星期四", "high":"高温 22℃", "fengli":"", "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;
+
+ 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 getForecast() {
+ return forecast;
+ }
+
+ public void setForecast(List forecast) {
+ this.forecast = forecast;
+ }
+}
diff --git a/src/main/java/com/xkrs/weather/entity/WeatherResponse.java b/src/main/java/com/xkrs/weather/entity/WeatherResponse.java
new file mode 100644
index 0000000..84a16ad
--- /dev/null
+++ b/src/main/java/com/xkrs/weather/entity/WeatherResponse.java
@@ -0,0 +1,55 @@
+package com.xkrs.weather.entity;
+
+import java.io.Serializable;
+
+/**
+ * @author XinYi Song
+ */
+public class WeatherResponse implements Serializable {
+ private static final long serialVersionUID = -8483256225271502962L;
+ private Weather data;
+ private Integer status;
+ private String desc;
+
+ public WeatherResponse() {
+ }
+
+ public WeatherResponse(Weather data, Integer status, String desc) {
+ this.data = data;
+ this.status = status;
+ this.desc = 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;
+ }
+
+ @Override
+ public String toString() {
+ return "WeatherResponse{" +
+ "data=" + data +
+ ", status=" + status +
+ ", desc='" + desc + '\'' +
+ '}';
+ }
+}
diff --git a/src/main/java/com/xkrs/weather/entity/Yesterday.java b/src/main/java/com/xkrs/weather/entity/Yesterday.java
new file mode 100644
index 0000000..472ba98
--- /dev/null
+++ b/src/main/java/com/xkrs/weather/entity/Yesterday.java
@@ -0,0 +1,68 @@
+package com.xkrs.weather.entity;
+
+import java.io.Serializable;
+
+/**
+ * 昨日天气
+ * @author XinYi Song
+ */
+public class Yesterday implements Serializable {
+ /* "yesterday":{
+ "date":"22日星期六", "high":"高温 24℃", "fx":"西北风", "low":"低温 13℃", "fl":"", "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;
+ }
+}
diff --git a/src/main/java/com/xkrs/weather/service/WeatherDataService.java b/src/main/java/com/xkrs/weather/service/WeatherDataService.java
new file mode 100644
index 0000000..8c382dd
--- /dev/null
+++ b/src/main/java/com/xkrs/weather/service/WeatherDataService.java
@@ -0,0 +1,24 @@
+package com.xkrs.weather.service;
+
+import com.xkrs.weather.entity.WeatherResponse;
+
+/**
+ * @author XinYi Song
+ */
+public interface WeatherDataService {
+
+ /**
+ * 根据城市ID查询天气数据
+ * @param CityId
+ * @return
+ */
+ WeatherResponse getDataByCityId(String CityId);
+
+ /**
+ * 根据城市名称查询天气数据
+ * @param cityName
+ * @return
+ */
+ WeatherResponse getDataByCityName(String cityName);
+
+}
diff --git a/src/main/java/com/xkrs/weather/service/impl/WeatherDataServiceImpl.java b/src/main/java/com/xkrs/weather/service/impl/WeatherDataServiceImpl.java
new file mode 100644
index 0000000..40ee22e
--- /dev/null
+++ b/src/main/java/com/xkrs/weather/service/impl/WeatherDataServiceImpl.java
@@ -0,0 +1,53 @@
+package com.xkrs.weather.service.impl;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.xkrs.weather.entity.WeatherResponse;
+import com.xkrs.weather.service.WeatherDataService;
+import org.springframework.http.ResponseEntity;
+import org.springframework.stereotype.Service;
+import org.springframework.web.client.RestTemplate;
+
+import javax.annotation.Resource;
+
+/**
+ * @author XinYi Song
+ */
+@Service
+public class WeatherDataServiceImpl implements WeatherDataService {
+
+ private static final String WEATHER_URI = "http://wthrcdn.etouch.cn/weather_mini?";
+
+ @Resource
+ 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 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;
+ }
+
+}