天气免费API接口,天气API接口请求讲解及源码下载

soゝso 2019-12-16 19:07:41 44421

本站一直提供了免费的天气API接口供大家使用,目前日调用量平均为400万次每天左右。天气API介绍请看:https://www.sojson.com/blog/305.html ,由于一直有人在天气API接口使用上有疑问,我开发了一个标准调用天气API接口的Demo,面向对象的Demo,最简单的调用。

天气API接口目前调用情况

截止当天的18点之前的数据,下面看2个图。


详细数据统计图


所以还请大家在接入的时候,做好缓存策略,根据自身的业务做最大的缓存力度,能2个小时就别1个小时。您每一次请求我都要付费。免费不容易,且行且珍惜。

天气请求代码

本demo实例用最简单的代码实现。

天气接口Controller

/**
 * 天气数据
 * @param id
 * @return
 */
@RequestMapping(value = "city/{id:1[0-9]{8}}", method = RequestMethod.GET)
public WeatherDto loadApi(@PathVariable("id") String id){
    String vliCode = String.format(",%s,", id);
    if(!cityCode.contains(vliCode)){
        throw new RuntimeException("no_city_id");
    }
    return weatherManager.getById(id);

}

天气接口Manager(数据请求)

//请求连接地址
final static String SOJSON_WEATHER_URL = "http://t.weather.sojson.com/api/weather/city/{1}";

/**
 * 获取数据
 * @param id
 * @return
 */
@Cacheable(cacheNames = "weather_cache", key = "#id")// 从缓存获取,key为ID,缓存具体看 ehcache.xml 配置文件
public WeatherDto getById(String id) {
    log.info("WeatherManager#getById: id={}", id);

    try {
        RestTemplate restTemplate = new RestTemplate();
        WeatherDto dto = restTemplate.getForObject(SOJSON_WEATHER_URL , WeatherDto.class,id);

        if(dto != null && dto.isSuccess()){
            return dto;
        }else{
            log.error("获取天气数据返回错误:{}", dto);
        }
    } catch (RestClientException e) {
        log.error("获取天气数据返回错误,出现异常.", e);
    }
    return null;
}

就完成了。

天气接口返回

{
	"message": "success感谢又拍云(upyun.com)提供CDN赞助",
	"status": 200,
	"date": "20191216",
	"time": "2019-12-16 16:29:43",
	"cityInfo": {
		"city": "北京市",
		"citykey": "101010100",
		"parent": "北京",
		"updateTime": "16:18"
	},
	"data": {
		"shidu": "89%",
		"pm25": 79.0,
		"pm10": 0.0,
		"quality": "轻度污染",
		"wendu": "0",
		"ganmao": "儿童、老年人及心脏、呼吸系统疾病患者人群应减少长时间或高强度户外锻炼",
		"forecast": [{
			"date": "16",
			"high": "高温 2℃",
			"low": "低温 -3℃",
			"ymd": "2019-12-16",
			"week": "星期一",
			"sunrise": "07:28",
			"sunset": "16:50",
			"aqi": 97,
			"fx": "西风",
			"fl": "<3级",
			"type": "小雪",
			"notice": "小雪虽美,赏雪别着凉"
		}, {
			"date": "17",
			"high": "高温 5℃",
			"low": "低温 -7℃",
			"ymd": "2019-12-17",
			"week": "星期二",
			"sunrise": "07:29",
			"sunset": "16:51",
			"aqi": 30,
			"fx": "北风",
			"fl": "3-4级",
			"type": "晴",
			"notice": "愿你拥有比阳光明媚的心情"
		}, {
			"date": "18",
			"high": "高温 2℃",
			"low": "低温 -7℃",
			"ymd": "2019-12-18",
			"week": "星期三",
			"sunrise": "07:30",
			"sunset": "16:51",
			"aqi": 50,
			"fx": "南风",
			"fl": "<3级",
			"type": "晴",
			"notice": "愿你拥有比阳光明媚的心情"
		}, {
			"date": "19",
			"high": "高温 4℃",
			"low": "低温 -8℃",
			"ymd": "2019-12-19",
			"week": "星期四",
			"sunrise": "07:30",
			"sunset": "16:51",
			"aqi": 50,
			"fx": "北风",
			"fl": "3-4级",
			"type": "晴",
			"notice": "愿你拥有比阳光明媚的心情"
		}, {
			"date": "20",
			"high": "高温 0℃",
			"low": "低温 -7℃",
			"ymd": "2019-12-20",
			"week": "星期五",
			"sunrise": "07:31",
			"sunset": "16:52",
			"aqi": 30,
			"fx": "南风",
			"fl": "<3级",
			"type": "晴",
			"notice": "愿你拥有比阳光明媚的心情"
		}, {
			"date": "21",
			"high": "高温 2℃",
			"low": "低温 -7℃",
			"ymd": "2019-12-21",
			"week": "星期六",
			"sunrise": "07:32",
			"sunset": "16:52",
			"aqi": 66,
			"fx": "北风",
			"fl": "<3级",
			"type": "晴",
			"notice": "愿你拥有比阳光明媚的心情"
		}, {
			"date": "22",
			"high": "高温 3℃",
			"low": "低温 -6℃",
			"ymd": "2019-12-22",
			"week": "星期日",
			"sunrise": "07:32",
			"sunset": "16:52",
			"fx": "西风",
			"fl": "<3级",
			"type": "多云",
			"notice": "阴晴之间,谨防紫外线侵扰"
		}, {
			"date": "23",
			"high": "高温 3℃",
			"low": "低温 -4℃",
			"ymd": "2019-12-23",
			"week": "星期一",
			"sunrise": "07:33",
			"sunset": "16:53",
			"fx": "西北风",
			"fl": "<3级",
			"type": "多云",
			"notice": "阴晴之间,谨防紫外线侵扰"
		}, {
			"date": "24",
			"high": "高温 3℃",
			"low": "低温 -4℃",
			"ymd": "2019-12-24",
			"week": "星期二",
			"sunrise": "07:33",
			"sunset": "16:53",
			"fx": "东北风",
			"fl": "<3级",
			"type": "多云",
			"notice": "阴晴之间,谨防紫外线侵扰"
		}, {
			"date": "25",
			"high": "高温 3℃",
			"low": "低温 -3℃",
			"ymd": "2019-12-25",
			"week": "星期三",
			"sunrise": "07:34",
			"sunset": "16:54",
			"fx": "西北风",
			"fl": "<3级",
			"type": "多云",
			"notice": "阴晴之间,谨防紫外线侵扰"
		}, {
			"date": "26",
			"high": "高温 4℃",
			"low": "低温 -5℃",
			"ymd": "2019-12-26",
			"week": "星期四",
			"sunrise": "07:34",
			"sunset": "16:55",
			"fx": "北风",
			"fl": "<3级",
			"type": "多云",
			"notice": "阴晴之间,谨防紫外线侵扰"
		}, {
			"date": "27",
			"high": "高温 -2℃",
			"low": "低温 -7℃",
			"ymd": "2019-12-27",
			"week": "星期五",
			"sunrise": "07:34",
			"sunset": "16:55",
			"fx": "东风",
			"fl": "<3级",
			"type": "阴",
			"notice": "不要被阴云遮挡住好心情"
		}, {
			"date": "28",
			"high": "高温 -1℃",
			"low": "低温 -8℃",
			"ymd": "2019-12-28",
			"week": "星期六",
			"sunrise": "07:35",
			"sunset": "16:56",
			"fx": "东南风",
			"fl": "<3级",
			"type": "晴",
			"notice": "愿你拥有比阳光明媚的心情"
		}, {
			"date": "29",
			"high": "高温 -3℃",
			"low": "低温 -8℃",
			"ymd": "2019-12-29",
			"week": "星期日",
			"sunrise": "07:35",
			"sunset": "16:57",
			"fx": "东南风",
			"fl": "<3级",
			"type": "多云",
			"notice": "阴晴之间,谨防紫外线侵扰"
		}, {
			"date": "30",
			"high": "高温 0℃",
			"low": "低温 -8℃",
			"ymd": "2019-12-30",
			"week": "星期一",
			"sunrise": "07:35",
			"sunset": "16:57",
			"fx": "东风",
			"fl": "<3级",
			"type": "多云",
			"notice": "阴晴之间,谨防紫外线侵扰"
		}],
		"yesterday": {
			"date": "15",
			"high": "高温 3℃",
			"low": "低温 -2℃",
			"ymd": "2019-12-15",
			"week": "星期日",
			"sunrise": "07:28",
			"sunset": "16:50",
			"aqi": 110,
			"fx": "西南风",
			"fl": "<3级",
			"type": "多云",
			"notice": "阴晴之间,谨防紫外线侵扰"
		}
	},
	"success": true
}

Ehcache缓存配置

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns="http://www.ehcache.org/v3"
        xmlns:jsr107="http://www.ehcache.org/v3/jsr107"
        xsi:schemaLocation="
            http://www.ehcache.org/v3 http://www.ehcache.org/schema/ehcache-core-3.0.xsd
            http://www.ehcache.org/v3/jsr107 http://www.ehcache.org/schema/ehcache-107-ext-3.0.xsd">
    <service>
        <jsr107:defaults enable-statistics="true"/>
    </service>

    <!-- user 为该缓存名称 对应@Cacheable的属性cacheNames-->
    <cache alias="weather_cache" >
        <!-- 指定缓存 key 类型,对应@Cacheable的属性key -->
        <key-type>java.lang.String</key-type>
        <!-- 配置value类型 -->
        <value-type>com.sojson.weather.api.demo.dto.WeatherDto</value-type>
        <expiry>
            <!-- 缓存 ttl,单位为分钟,现在设置的是2个小时 -->
            <ttl unit="minutes">120</ttl>
        </expiry>
        <resources>
            <!-- 分配资源大小 -->
            <heap unit="entries">2000</heap>
            <offheap unit="MB">100</offheap>
        </resources>
    </cache>
</config>

这里配置了缓存ttl为120分钟,也就是2个小时,建议保持这个配置

天气接口源码下载

github:https://github.com/baichengzhou/weather.api

gitee:https://gitee.com/jsonla/weather.api 


版权所属:SO JSON在线解析

原文地址:https://www.sojson.com/blog/349.html

转载时必须以链接形式注明原始出处及本声明。

本文主题:

如果本文对你有帮助,那么请你赞助我,让我更有激情的写下去,帮助更多的人。

关于作者
一个低调而闷骚的男人。
相关文章
免费天气API,全国天气 JSON API接口,可以获取五天的天气预报
天气API,全国天气 JSON API接口,可以获取十五天的天气预报
天气预报 API,7天天气预报API,信息全,高精准天气API 接口说明
免费天气API天气JSON API,不限次数获取十五天的天气预报
Java API接口返回不是JSON的解决方案,SpringMVC返回JSON配置。
公安局网站备案信息查询API免费查询公安网备 API 代码说明
溯源接口
在线HTTP接口测试 - HTTP GET/POST模拟请求测试工具【更新说明】
又拍云(Upyun)CDN、云存储刷新链接缓存,API实例讲解
JDK1.7中新增自动释放资源接口AutoCloseable讲解
最新文章
使用七牛云存储实现图片API,自动删除图片方案合集 1205
神速ICP备案经验分享,ICP备案居然一天就通过了 2318
百度加强推送URL链接,百度SEO强行推送链接JavaScript代码案例讲解。 2932
SOJSON 拓展服务器被DDos攻击了一晚上,是如何解决的? 3358
湖南地区备案“新增网站需提交组网方案或解释说明”,关于备案做简单叙述 3427
企查查你是个什么企业,骗子的帮凶,诈骗的集中营,通过企查查骚扰企业电话不断,为所欲为的企查查 9730
Java爬取百度云观测对网站的检测数据,获取子域名及域名的安全信息 2573
【2020年】百度搜索词获取,获取百度搜索的关键词【真实有效】 7067
Nodejs环境安装,Nodejs环境安装一篇就够了 1815
iOS版淘宝打开提示“您使用的程序是内测版本,将于2020-03-28到期,到期将无法使用,请尽快下载最新版本” 2505
最热文章
苹果电脑Mac怎么恢复出厂系统?苹果系统怎么重装系统? 433471
我为什么要选择RabbitMQ ,RabbitMQ简介,各种MQ选型对比 418346
免费天气API,全国天气 JSON API接口,可以获取五天的天气预报 351109
最新MyEclipse8.5注册码,有效期到2020年 (已经更新) 313468
免费天气API,天气JSON API,不限次数获取十五天的天气预报 288851
Elasticsearch教程(四) elasticsearch head 插件安装和使用 221888
Jackson 时间格式化,时间注解 @JsonFormat 用法、时差问题说明 178689
谈谈斐讯路由器劫持,你用斐讯路由器,你需要知道的事情 136591
Elasticsearch教程(一),全程直播(小白级别) 119786
Elasticsearch教程(五) elasticsearch Mapping的创建 99018

骚码加入我们 / 千人QQ群:259217951

入群需要5元为的是没有垃圾广告,如果没有QQ钱包,可以加群主拉进。

二维码生成 来自 >> 二维码生成器

支付扫码

所有赞助/开支都讲公开明细,用于网站维护:赞助名单查看

查看我的收藏

正在加载... ...