第一步:配置yml
spring:
#天气接口配置
weather:
appid: 82573591
appsecret: oIVw8PWM
#每个天气接口的version值都不同,如要更换接口,请同步更换version值
#收费版 支持全球十万+ 城市天气
#version: day
#url: https://v0.tianqiapi.com
#免费版 支持国内--七日天气--查询
#version: v1
#url: https://tianqiapi.com/api
#免费版 支持国内天气查询
version: v6
url: https://tianqiapi.com/api
第二步:单独写一个WeatherConfig类
package com.techen.ami.config;
import com.alibaba.fastjson.JSONObject;
import com.techen.tap.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;
@Configuration
public class WeatherConfig {
@Value("${spring.weather.appid}")
public String appid;
@Value("${spring.wea

该博客介绍了使用Java实现天气接口的步骤,首先要配置yml,接着单独编写一个WeatherConfig类,最后编写Controller,还给出了具体城市名称大全的参考链接。
最低0.47元/天 解锁文章
578

被折叠的 条评论
为什么被折叠?



