添加了查询天气实况的接口

This commit is contained in:
DESKTOP-G8BCEP0\HP 2021-07-26 17:35:08 +08:00
parent 6685e334ec
commit 2cebe20881
11 changed files with 441 additions and 0 deletions

View File

@ -29,6 +29,7 @@
<springfox-swagger2.version>2.10.5</springfox-swagger2.version> <springfox-swagger2.version>2.10.5</springfox-swagger2.version>
<poi.version>5.0.0</poi.version> <poi.version>5.0.0</poi.version>
<poi-ooxml.version>5.0.0</poi-ooxml.version> <poi-ooxml.version>5.0.0</poi-ooxml.version>
<httpclient.version>4.5.2</httpclient.version>
</properties> </properties>
<dependencies> <dependencies>
@ -177,6 +178,11 @@
<version>2.4</version> <version>2.4</version>
<classifier>jdk15</classifier> <classifier>jdk15</classifier>
</dependency> </dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>${httpclient.version}</version>
</dependency>
</dependencies> </dependencies>

View File

@ -2,6 +2,15 @@ package com.xkrs;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; 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 * @author XinYi Song
@ -13,4 +22,12 @@ public class FirePointApplication {
SpringApplication.run(FirePointApplication.class, args); 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;
}
} }

View File

@ -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();
}
}

View File

@ -50,6 +50,7 @@ class WebSecurityConfig extends WebSecurityConfigurerAdapter {
.antMatchers(HttpMethod.GET,"/api/user/booleanUserName").permitAll() .antMatchers(HttpMethod.GET,"/api/user/booleanUserName").permitAll()
.antMatchers(HttpMethod.GET,"/websocketTest").permitAll() .antMatchers(HttpMethod.GET,"/websocketTest").permitAll()
.antMatchers(HttpMethod.GET,"/selectCityName").permitAll() .antMatchers(HttpMethod.GET,"/selectCityName").permitAll()
.antMatchers(HttpMethod.GET,"/weather/cityName").permitAll()
// 所有其它请求需要身份认证 // 所有其它请求需要身份认证
.anyRequest().authenticated() .anyRequest().authenticated()
.and() .and()

View File

@ -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);
}
}

View File

@ -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":"<![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;
}
}

View File

@ -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":"<![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;
}
}

View File

@ -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 + '\'' +
'}';
}
}

View File

@ -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":"<![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;
}
}

View File

@ -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);
}

View File

@ -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<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;
}
}