⭐ 手把手:从和风天气控制台获取配置,并在 Java 中自动读取配置完成天气获取(最全教程)
通过本文你已经学会:✔ 如何从和风天气控制台获取KEY / Host / 城市查询接口✔ 如何写入 Spring Boot 配置(避免硬编码)✔ 如何使用自动绑定配置✔ 如何封装一个可复用的✔ Controller 一行代码即可获取天气数据✔ 处理乱码与时间格式问题控制台配置 → yml 配置 → @ConfigurationProperties 绑定 → Java 调用 → 返回天气数据如需进一
和风天气(QWeather)提供了非常完善的天气数据服务,但很多开发者刚开始不知道 控制台的配置信息如何与代码联动。本文将详细介绍:
-
和风天气控制台 → 如何创建项目并获取 KEY、Host
-
将这些配置信息写入 Spring Boot yml
-
封装一个 Java 客户端,通过配置自动请求天气数据
-
完整可运行示例
1. 控制台如何配置
登录控制台后依次操作:
✔ 1.1 创建项目
控制台 → 应用管理 → “创建应用”
填写:
-
应用名称
-
使用场景
-
访问方式(公网调用)
创建后会得到:
🔑 KEY(核心)
示例:
ff3c19e7ab2c4c78b7c1e3ddxxxxxxxx
✔ 1.2 获取 API Host(标准版一般固定)
API 访问 Host:
https://api.qweather.com
常用路径(v7 版本):
-
实时天气:
/v7/weather/now -
预报 3 天:
/v7/weather/3d -
空气质量:
/v7/air/now -
城市查询:
https://geoapi.qweather.com/v2/city/lookup
⚠ 注意:
-
天气类接口用
api.qweather.com -
城市查询用
geoapi.qweather.com
✔ 1.3 获取 location(必须)
location 可以是:
| 类型 | 示例 |
|---|---|
| 城市 AdCode | 101010100(北京) |
| 经纬度 | 116.41,39.92 |
| 通过城市查询接口获取 | lookup?location=广州 |
2. 🛠 将配置写入 Spring Boot yml
在 application.yml 中配置和风天气信息:
qweather:
key: ff3c19e7ab2c4c78b7c1e3ddxxxxxxx
host: https://api.qweather.com
geoHost: https://geoapi.qweather.com
api:
weatherNow: /v7/weather/now
weather3d: /v7/weather/3d
cityLookup: /v2/city/lookup
这样你后续修改 KEY / Host,都不需要改代码。
3. 💡 使用 @ConfigurationProperties 绑定配置(推荐)
创建配置类:
@Component
@ConfigurationProperties(prefix = "qweather")
@Data
public class QWeatherProperties {
private String key;
private String host;
private String geoHost;
private Api api;
@Data
public static class Api {
private String weatherNow;
private String weather3d;
private String cityLookup;
}
}
Spring 会自动帮助你把 yml 内容绑定到对象中。
4. 🚀 Java 代码从配置自动获取天气数据
封装 QWeatherClient:
@Component
public class QWeatherClient {
private final RestTemplate restTemplate = new RestTemplate();
@Autowired
private QWeatherProperties properties;
/**
* 实时天气
*/
public JSONObject getWeatherNow(String location) {
String url = properties.getHost()
+ properties.getApi().getWeatherNow()
+ "?location=" + location
+ "&key=" + properties.getKey();
String json = restTemplate.getForObject(url, String.class);
return JSON.parseObject(json);
}
/**
* 城市查询
*/
public JSONObject lookupCity(String cityName) {
String url = properties.getGeoHost()
+ properties.getApi().getCityLookup()
+ "?location=" + cityName
+ "&key=" + properties.getKey();
String json = restTemplate.getForObject(url, String.class);
return JSON.parseObject(json);
}
}
5. 📌 Controller 直接调用(即可运行)
@RestController
@RequestMapping("/weather")
public class WeatherController {
@Autowired
private QWeatherClient client;
@GetMapping("/now")
public JSONObject now(@RequestParam String location) {
return client.getWeatherNow(location);
}
@GetMapping("/city")
public JSONObject city(@RequestParam String name) {
return client.lookupCity(name);
}
}
示例请求:
http://localhost:8080/weather/now?location=101010100
http://localhost:8080/weather/city?name=广州
6. 📝 字符串乱码问题如何解决?
restTemplate.getMessageConverters()
.set(1, new StringHttpMessageConverter(StandardCharsets.UTF_8));
强制使用 UTF-8 即可。
7. 🕒 时间格式问题(2025-11-20T10:41+08:00)
如果你需要转换为:yyyy-MM-dd HH:mm:ss
public static String convertTime(String time) {
OffsetDateTime odt = OffsetDateTime.parse(time);
return odt.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
}
8. ⭐ 总结
通过本文你已经学会:
✔ 如何从和风天气控制台获取 KEY / Host / 城市查询接口
✔ 如何写入 Spring Boot 配置(避免硬编码)
✔ 如何使用 @ConfigurationProperties 自动绑定配置
✔ 如何封装一个可复用的 QWeatherClient
✔ Controller 一行代码即可获取天气数据
✔ 处理乱码与时间格式问题
整个流程非常清晰:
控制台配置 → yml 配置 → @ConfigurationProperties 绑定 → Java 调用 → 返回天气数据
更多推荐
所有评论(0)