Skip to content

Commit 402ba4d

Browse files
author
xuzheng
committed
add query weather schedule
1 parent ba1949d commit 402ba4d

File tree

10 files changed

+406
-3
lines changed

10 files changed

+406
-3
lines changed

pom.xml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@
3737
<optional>true</optional>
3838
</dependency>
3939

40+
<dependency>
41+
<groupId>org.springframework.boot</groupId>
42+
<artifactId>spring-boot-starter-mail</artifactId>
43+
</dependency>
44+
4045
<!--swagger ui-->
4146
<dependency>
4247
<groupId>io.springfox</groupId>
@@ -60,6 +65,19 @@
6065
<!--<artifactId>mysql-connector-java</artifactId>-->
6166
<!--<version>5.1.41</version>-->
6267
<!--</dependency>-->
68+
<!-- httpclient -->
69+
<dependency>
70+
<groupId>org.apache.httpcomponents</groupId>
71+
<artifactId>httpclient</artifactId>
72+
<version>4.5.6</version>
73+
</dependency>
74+
75+
<dependency>
76+
<groupId>com.google.code.gson</groupId>
77+
<artifactId>gson</artifactId>
78+
<version>2.8.5</version>
79+
</dependency>
80+
6381
</dependencies>
6482

6583
<build>

readme.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# spring boot 注解配置详解
2+
3+
## bean 的分类标识
4+
15
# swagger ui 配置详解
26

37
## @Api
@@ -66,3 +70,4 @@
6670
- example 属性的示例值
6771
- required 是否为必须值
6872

73+
http://api.help.bj.cn/api/

src/main/java/com/azxx/demon/Starter.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import org.springframework.boot.SpringApplication;
55
import org.springframework.boot.autoconfigure.SpringBootApplication;
66
import org.springframework.context.annotation.ComponentScan;
7+
import org.springframework.scheduling.annotation.EnableScheduling;
78

89
/**
910
* @SpringBootApplication 包含了三个特点:
@@ -13,18 +14,21 @@
1314
* @Configuration: allow to register extra beans in the context or import additional configuration classes
1415
*
1516
*/
17+
//@SpringBootApplication(exclude = {Starter.class,HelloWorld.class}) // 等同于 @EnableAutoConfiguration @ComponentScan @Configuration
1618
@SpringBootApplication // 等同于 @EnableAutoConfiguration @ComponentScan @Configuration
1719
@ComponentScan(basePackages = {
1820
"com.azxx.demon.configuration",
1921
"com.azxx.demon.controller",
20-
"com.azxx.demon.service"})
22+
"com.azxx.demon.service",
23+
"com.azxx.demon.schedule"})
24+
@EnableScheduling
2125
public class Starter {
2226

2327
public static void main(String[] args) {
2428

2529
SpringApplication app = new SpringApplication(Starter.class);
2630
app.setBannerMode(Banner.Mode.OFF);
2731
app.run(args);
28-
// SpringApplication.run(Application.class,args);
32+
// SpringApplication.run(Starter.class,args);
2933
}
3034
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package com.azxx.demon.modal;
2+
3+
import java.util.List;
4+
5+
public class TodayWeather {
6+
7+
8+
private Weather yesterday;
9+
private String life;
10+
private String temp;
11+
private String aqi;
12+
private String city;
13+
private List<Weather> forecast;
14+
15+
public Weather getYesterday() {
16+
return yesterday;
17+
}
18+
19+
public void setYesterday(Weather yesterday) {
20+
this.yesterday = yesterday;
21+
}
22+
23+
public String getLife() {
24+
return life;
25+
}
26+
27+
public void setLife(String life) {
28+
this.life = life;
29+
}
30+
31+
public String getTemp() {
32+
return temp;
33+
}
34+
35+
public void setTemp(String temp) {
36+
this.temp = temp;
37+
}
38+
39+
public String getAqi() {
40+
return aqi;
41+
}
42+
43+
public void setAqi(String aqi) {
44+
this.aqi = aqi;
45+
}
46+
47+
public String getCity() {
48+
return city;
49+
}
50+
51+
public void setCity(String city) {
52+
this.city = city;
53+
}
54+
55+
public List<Weather> getForecast() {
56+
return forecast;
57+
}
58+
59+
public void setForecast(List<Weather> forecast) {
60+
this.forecast = forecast;
61+
}
62+
63+
@Override
64+
public String toString() {
65+
return "TodayWeather{" +
66+
"yesterday=" + yesterday +
67+
", life='" + life + '\'' +
68+
", temp='" + temp + '\'' +
69+
", aqi='" + aqi + '\'' +
70+
", city='" + city + '\'' +
71+
", forecast=" + forecast +
72+
'}';
73+
}
74+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.azxx.demon.modal;
2+
3+
public class Weather {
4+
5+
private String date;
6+
private String temphigh;
7+
private String templow;
8+
private String wind;
9+
private String weather;
10+
11+
public String getDate() {
12+
return date;
13+
}
14+
15+
public void setDate(String date) {
16+
this.date = date;
17+
}
18+
19+
public String getTemphigh() {
20+
return temphigh;
21+
}
22+
23+
public void setTemphigh(String temphigh) {
24+
this.temphigh = temphigh;
25+
}
26+
27+
public String getTemplow() {
28+
return templow;
29+
}
30+
31+
public void setTemplow(String templow) {
32+
this.templow = templow;
33+
}
34+
35+
public String getWind() {
36+
return wind;
37+
}
38+
39+
public void setWind(String wind) {
40+
this.wind = wind;
41+
}
42+
43+
public String getWeather() {
44+
return weather;
45+
}
46+
47+
public void setWeather(String weather) {
48+
this.weather = weather;
49+
}
50+
51+
@Override
52+
public String toString() {
53+
return "Weather{" +
54+
"date='" + date + '\'' +
55+
", temphigh='" + temphigh + '\'' +
56+
", templow='" + templow + '\'' +
57+
", wind='" + wind + '\'' +
58+
", weather='" + weather + '\'' +
59+
'}';
60+
}
61+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.azxx.demon.schedule;
2+
3+
import com.azxx.demon.modal.TodayWeather;
4+
import com.azxx.demon.utils.HttpUtils;
5+
import com.google.gson.Gson;
6+
import com.google.gson.JsonObject;
7+
import com.google.gson.JsonParser;
8+
import org.slf4j.Logger;
9+
import org.slf4j.LoggerFactory;
10+
import org.springframework.beans.factory.annotation.Value;
11+
import org.springframework.scheduling.annotation.Scheduled;
12+
import org.springframework.stereotype.Component;
13+
14+
@Component
15+
public class WeatherJob {
16+
17+
private static Logger logger = LoggerFactory.getLogger(WeatherJob.class);
18+
19+
@Value("${weather.query.url}")
20+
private String url;
21+
22+
@Scheduled(cron = "0/5 * * * * *")
23+
public void queryWeather(){
24+
String result = HttpUtils.httpGet(url,null);
25+
logger.info(result);
26+
Gson gson = new Gson();
27+
JsonObject object = new JsonParser().parse(result).getAsJsonObject();
28+
JsonObject data = object.get("data").getAsJsonObject();
29+
logger.info(data.toString());
30+
TodayWeather weather = gson.fromJson(data,TodayWeather.class);
31+
logger.info(weather.toString());
32+
}
33+
}
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
package com.azxx.demon.utils;
2+
3+
import org.apache.http.HttpEntity;
4+
import org.apache.http.client.HttpClient;
5+
import org.apache.http.client.config.RequestConfig;
6+
import org.apache.http.client.entity.UrlEncodedFormEntity;
7+
import org.apache.http.client.methods.CloseableHttpResponse;
8+
import org.apache.http.client.methods.HttpGet;
9+
import org.apache.http.client.methods.HttpPost;
10+
import org.apache.http.client.methods.HttpUriRequest;
11+
import org.apache.http.impl.client.CloseableHttpClient;
12+
import org.apache.http.impl.client.HttpClients;
13+
import org.apache.http.message.BasicNameValuePair;
14+
import org.apache.http.util.EntityUtils;
15+
import org.slf4j.Logger;
16+
import org.slf4j.LoggerFactory;
17+
18+
import java.io.IOException;
19+
import java.util.ArrayList;
20+
import java.util.List;
21+
import java.util.Map;
22+
23+
public class HttpUtils {
24+
25+
private static Logger logger = LoggerFactory.getLogger(HttpUtils.class);
26+
27+
public static String httpGet(String url, Map<String, String> params) {
28+
29+
StringBuffer sb = new StringBuffer();
30+
if (params != null) {
31+
for (Map.Entry<String, String> entry : params.entrySet()) {
32+
String param = entry.getKey() + "=" + entry.getValue();
33+
sb.append(param + "&");
34+
}
35+
url = url+"?"+sb.toString();
36+
}
37+
//1.创建HttpClient
38+
CloseableHttpClient client = HttpClients.createDefault();
39+
//2.创建HttpGet
40+
HttpGet httpGet = new HttpGet(url );
41+
CloseableHttpResponse response = null;
42+
String content = null;
43+
44+
try {
45+
//3.执行Get请求
46+
response = client.execute(httpGet);
47+
//4.获取响应实体
48+
HttpEntity entity = response.getEntity();
49+
//获取返回状态码
50+
logger.info(String.valueOf(response.getStatusLine()));
51+
if (entity != null) {
52+
content = EntityUtils.toString(entity);
53+
logger.info(content);
54+
}
55+
56+
} catch (IOException e) {
57+
e.printStackTrace();
58+
} finally {
59+
try {
60+
response.close();
61+
client.close();
62+
} catch (IOException e) {
63+
e.printStackTrace();
64+
}
65+
}
66+
return content;
67+
}
68+
69+
public static String httpPost(String url, Map<String, String> params) {
70+
71+
//1.创建HttpClient
72+
CloseableHttpClient client = HttpClients.createDefault();
73+
//2.创建HttpGet
74+
HttpPost httpPost = new HttpPost(url);
75+
CloseableHttpResponse response = null;
76+
String content = null;
77+
//设置请求和传输超时时间
78+
RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(6000).setConnectTimeout(6000).build();
79+
httpPost.setConfig(requestConfig);
80+
httpPost.addHeader("User-Agent", "Mozilla/5.0");
81+
// 创建参数队列
82+
List formparams = new ArrayList();
83+
for (Map.Entry<String, String> entry : params.entrySet()) {
84+
formparams.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
85+
}
86+
87+
UrlEncodedFormEntity uefEntity;
88+
try {
89+
uefEntity = new UrlEncodedFormEntity(formparams, "UTF-8");
90+
httpPost.setEntity(uefEntity);
91+
//3.执行Get请求
92+
response = client.execute(httpPost);
93+
//4.获取响应实体
94+
HttpEntity entity = response.getEntity();
95+
//获取返回状态码
96+
logger.info(String.valueOf(response.getStatusLine()));
97+
if (entity != null) {
98+
content = EntityUtils.toString(entity);
99+
logger.info(content);
100+
}
101+
102+
} catch (IOException e) {
103+
e.printStackTrace();
104+
} finally {
105+
try {
106+
response.close();
107+
client.close();
108+
} catch (IOException e) {
109+
e.printStackTrace();
110+
}
111+
}
112+
return content;
113+
}
114+
}
Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,19 @@
11
server:
2-
port: 8088
2+
port: 8088
3+
4+
weather:
5+
query:
6+
url: http://api.help.bj.cn/apis/weather6d/?id=101280601
7+
8+
spring:
9+
properties:
10+
mail:
11+
smtp:
12+
auth :true
13+
starttls:
14+
enable: true
15+
required :true
16+
mail:
17+
host:
18+
username:
19+
password:

0 commit comments

Comments
 (0)