Java 天气接口 天气查询
生活随笔
收集整理的這篇文章主要介紹了
Java 天气接口 天气查询
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
第一步:配置yml
spring:#天氣接口配置weather:appid: 82573591appsecret: oIVw8PWM#每個天氣接口的version值都不同,如要更換接口,請同步更換version值#收費版 支持全球十萬+ 城市天氣#version: day#url: https://v0.tianqiapi.com#免費版 支持國內--七日天氣--查詢#version: v1#url: https://tianqiapi.com/api#免費版 支持國內天氣查詢version: v6url: 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.weather.appsecret}")public String appsecret;@Value("${spring.weather.version}")public String version;@Value("${spring.weather.url}")public String url;@Autowiredpublic RestTemplate restTemplate;//天氣查詢條件: 城市名稱 坐標 如: query=北京 query=36.68,116.99String query = "北京";//設備ip 默認值47.95.242.134String ip = "47.95.242.134";//語言 zh en 默認zhString language = "zh";//溫度單位 m--攝氏度 f--華氏度 默認攝氏度String unit = "m";public String getUrl(HttpServletRequest request){beforeSetting(request);//return url + "/?version=" + version + "&unit=" + unit + "&language=" + language + "&query=" + query + "&appid=" + appid + "&appsecret=" + appsecret;return url + "/?version=" + version + "&city=" + query + "&appid=" + appid + "&appsecret=" + appsecret;}//按坐標獲取天氣urlpublic String getUrlByCoordinate(Map paraMap , HttpServletRequest request){String coordinate = "";if(paraMap.containsKey("longitude")){String value = paraMap.get("longitude").toString();if(!StringUtil.isEmpty(value)){coordinate += value + ",";}}if(paraMap.containsKey("latitude")){String value = paraMap.get("latitude").toString();if(!StringUtil.isEmpty(value)){coordinate += value ;}}if(!StringUtil.isEmpty(coordinate)){query = coordinate;}return getUrl(request);}//按城市獲取天氣urlpublic String getUrlByCity(Map paraMap , HttpServletRequest request){String city = "";if(paraMap.containsKey("city")){String value = paraMap.get("city").toString();if(!StringUtil.isEmpty(value)){city += value ;}}if(!StringUtil.isEmpty(city)){query = city;}return getUrl(request);}//設置語言類型和溫度單位public void beforeSetting(HttpServletRequest request){String lang = request.getHeader("lang");if("en".equals(language)){language = lang;unit = "f";}else{language = lang;unit = "m";}}//由后臺發送請求,接收返回的天氣參數 依照城市public Map getWeatherByCity(HttpServletRequest request , Map paraMap){String url = this.getUrlByCity( paraMap , request);JSONObject jsonObj = restTemplate.getForObject(url , JSONObject.class);Map resultMap = (Map<String , Object>) jsonObj;return resultMap;}//由后臺發送請求,接收返回的天氣參數 依照經緯度public Map getUrlByCoordinate(HttpServletRequest request , Map paraMap){String url = this.getUrlByCoordinate( paraMap , request);JSONObject jsonObj = restTemplate.getForObject(url , JSONObject.class);Map resultMap = (Map<String , Object>) jsonObj;return resultMap;} }第三步 , 編寫Controller.
@Autowiredprotected WeatherConfig weatherConfig;@GetMapping(value = "/weather")public ResponseResult<Object> weather(HttpServletRequest request){//paraMap由get請求傳入, 參數名: city 參數值:城市名稱(不帶 省 市 后綴)如:上海Map paraMap = new HashMap();paraMap.put("city" , "shanghai");paraMap = weatherConfig.getWeatherByCity(request , paraMap);return ResponseResult.ok(paraMap);}具體城市名稱大全 請參考:
https://gitee.com/wangjins/weather_api/blob/master/city.sql#
?
總結
以上是生活随笔為你收集整理的Java 天气接口 天气查询的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: php导航条css代码生成器,怎么使用c
- 下一篇: 通过生命周期管理来做热数据到冷数据的迁移