首页 > 其他分享 >SpringBoot集成高德天气API获取天气数据

SpringBoot集成高德天气API获取天气数据

时间:2024-09-14 10:52:08浏览次数:3  
标签:weatherForecast String 天气 private API import com public SpringBoot

一、前言

现在我们的日常开发中获取天气信息已经成为一项很重要的功能,也是一些门户网站必不可少的展示。所以我们需要学习如何获取实时天气数据,我们利用SpringBoot集成高德天气API可以很方便的实现获取天气实时数据。

二、前期准备

1.账号申请

天气文档:https://lbs.amap.com/api/webservice/guide/api/weatherinfo

整体流程可参考:https://lbs.amap.com/api/webservice/guide/create-project/get-key

2.注册账号

注册地址:https://console.amap.com/dev/id/phone

3.创建应用

地址:https://console.amap.com/dev/key/app

4.申请key

SpringBoot集成高德天气API获取天气数据_高德地图

5.请勾选web服务

SpringBoot集成高德天气API获取天气数据_SpringBoot_02

三、项目实战

1.填写相关配置和创建配置类

#高德天气请求路径
amap-weather-config.weatherurl=https://restapi.amap.com/v3/weather/weatherInfo
#高德地图申请的key
amap-weather-config.key=xxxxx
package com.example.dataproject.config;

import lombok.Getter;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

/**
 * @author qx
 * @date 2024/9/13
 * @des 高德天气属性配置
 */
@Configuration
@ConfigurationProperties(prefix = "amap-weather-config")
@Getter
@Setter
public class AmapWeatherConfig {

    /**
     * 请求路径
     */
    private String weatherurl;

    /**
     * key
     */
    private String key;
}

2.配置RestTemplate

package com.example.dataproject;


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;


@SpringBootApplication
public class DataProjectApplication {

    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }

    public static void main(String[] args) {
        SpringApplication.run(DataProjectApplication.class, args);
    }

}

3.创建相关实体类

实时天气

package com.example.dataproject.domain;

import lombok.Data;

/**
 * 实况天气
 *
 * @author qiangesoft
 * @date 2023-08-10
 */
@Data
public class Live {

    /**
     * 省份名
     */
    private String province;

    /**
     * 城市名
     */
    private String city;

    /**
     * 区域编码
     */
    private String adcode;

    /**
     * 天气现象(汉字描述)
     */
    private String weather;

    /**
     * 实时气温,单位:摄氏度
     */
    private String temperature;

    /**
     * 风向描述
     */
    private String winddirection;

    /**
     * 风力级别,单位:级
     */
    private String windpower;

    /**
     * 空气湿度
     */
    private String humidity;

    /**
     * 数据发布的时间
     */
    private String reporttime;

}

预报天气

package com.example.dataproject.domain;


import lombok.Data;

import java.util.List;

/**
 * 预报天气
 *
 * @author qiangesoft
 * @date 2023-08-10
 */
@Data
public class Forecast {

    /**
     * 省份名
     */
    private String province;

    /**
     * 城市名
     */
    private String city;

    /**
     * 区域编码
     */
    private String adcode;

    /**
     * 数据发布的时间
     */
    private String reporttime;

    /**
     * 天气
     */
    private List<Cast> casts;

    /**
     * 详细天气信息
     */
    @Data
    public static class Cast {

        /**
         * 日期
         */
        private String date;

        /**
         * 星期几
         */
        private String week;

        /**
         * 白天天气现象
         */
        private String dayweather;

        /**
         * 晚上天气现象
         */
        private String nightweather;

        /**
         * 白天温度
         */
        private String daytemp;

        /**
         * 晚上温度
         */
        private String nighttemp;

        /**
         * 白天风向
         */
        private String daywind;

        /**
         * 晚上风向
         */
        private String nightwind;

        /**
         * 白天风力
         */
        private String daypower;

        /**
         * 晚上风力
         */
        private String nightpower;
    }
}

高德天气数据结果实体

package com.example.dataproject.domain;


import lombok.Data;

import java.util.List;

/**
 * 高德数据结果
 *
 * @author qiangesoft
 * @date 2023-07-18
 */
@Data
public class GaoDeResult {
    /**
     * 返回结果状态值
     */
    private String status;
    /**
     * 返回状态说明
     */
    private String info;
    /**
     * 状态码
     */
    private String infocode;
    /**
     * 查询个数
     */
    private String count;
    /**
     * 实况天气数据信息
     */
    private List<Live> lives;
    /**
     * 预报天气信息数据
     */
    private List<Forecast> forecasts;

}

天气常量类

package com.example.dataproject.common;


/**
 * 天气常量
 *
 * @author qiangesoft
 * @date 2023-08-10
 */
public class WeatherConstant {

    /**
     * 成功标志
     */
    public static final String SUCCESS = "1";


    /**
     * key
     */
    public static final String KEY = "key";

    /**
     * 输入城市的adcode
     */
    public static final String CITY = "city";

    /**
     * 可选值:base/all
     * base:返回实况天气
     * all:返回预报天气
     */
    public static final String EXTENSIONS = "extensions";

    /**
     * 可选值:JSON,XML
     */
    public static final String OUTPUT = "output";
}

4.天气服务层

package com.example.dataproject.service;

import com.example.dataproject.common.WeatherConstant;
import com.example.dataproject.config.AmapWeatherConfig;
import com.example.dataproject.domain.*;
import com.example.dataproject.enums.WeatherType;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author qx
 * @date 2024/9/12
 * @des 高德天气服务层
 */
@Service
@Slf4j
public class WeatherService {

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private AmapWeatherConfig amapWeatherConfig;


    /**
     * 实时天气
     *
     * @param adcode
     * @return
     */
    public Weather getData(String adcode) {
        String sendUrl = amapWeatherConfig.getWeatherurl() +
                "?key=" + amapWeatherConfig.getKey() +
                "&city=" + adcode +
                "&extensions=base";
        ResponseEntity<GaoDeResult> responseEntity = restTemplate.getForEntity(sendUrl, GaoDeResult.class);
        // 请求异常,可能由于网络等原因
        HttpStatus statusCode = responseEntity.getStatusCode();
        if (!HttpStatus.OK.equals(statusCode)) {
            log.info("Request for Gaode interface error");
            return null;
        }
        // 请求失败
        GaoDeResult gaoDeResult = responseEntity.getBody();
        String status = gaoDeResult.getStatus();
        if (!status.equals(WeatherConstant.SUCCESS)) {
            log.info("Request for Gaode interface failed");
            return null;
        }

        List<Live> lives = gaoDeResult.getLives();
        if (CollectionUtils.isEmpty(lives)) {
            return null;
        }
        // 实况天气
        Live live = lives.get(0);
        Weather weather = new Weather();
        weather.setProvince(live.getProvince());
        weather.setCity(live.getCity());
        weather.setAdcode(live.getAdcode());
        weather.setWeather(live.getWeather());
        weather.setWeatherImg(WeatherType.getCodeByDes(live.getWeather()));
        weather.setTemperature(live.getTemperature());
        weather.setWindDirection(live.getWinddirection());
        weather.setWindPower(live.getWindpower());
        weather.setHumidity(live.getHumidity());
        weather.setReportTime(live.getReporttime());
        return weather;
    }

    /**
     * 预报天气
     *
     * @param adcode
     * @return
     */
    public List<WeatherForecast> getForecastData(String adcode) {
        String sendUrl = amapWeatherConfig.getWeatherurl() +
                "?key=" + amapWeatherConfig.getKey() +
                "&city=" + adcode +
                "&extensions=all";
        ResponseEntity<GaoDeResult> responseEntity = restTemplate.getForEntity(sendUrl, GaoDeResult.class);
        // 请求异常,可能由于网络等原因
        HttpStatus statusCode = responseEntity.getStatusCode();
        if (!HttpStatus.OK.equals(statusCode)) {
            log.info("Request for Gaode interface error");
            return Collections.emptyList();
        }

        // 请求失败
        GaoDeResult gaoDeResult = responseEntity.getBody();
        String status = gaoDeResult.getStatus();
        if (!status.equals(WeatherConstant.SUCCESS)) {
            log.info("Request for Gaode interface failed");
            return Collections.emptyList();
        }

        List<Forecast> forecasts = gaoDeResult.getForecasts();
        if (CollectionUtils.isEmpty(forecasts)) {
            return Collections.emptyList();
        }

        // 预报天气
        Forecast forecast = forecasts.get(0);
        List<WeatherForecast> weatherForecastList = new ArrayList<>();
        List<Forecast.Cast> casts = forecast.getCasts();
        for (Forecast.Cast cast : casts) {
            WeatherForecast weatherForecast = new WeatherForecast();
            weatherForecast.setProvince(forecast.getProvince());
            weatherForecast.setCity(forecast.getCity());
            weatherForecast.setAdcode(forecast.getAdcode());
            weatherForecast.setDate(cast.getDate());
            weatherForecast.setWeek(cast.getWeek());
            weatherForecast.setDayWeather(cast.getDayweather());
            weatherForecast.setDayWeatherImg(WeatherType.getCodeByDes(cast.getDayweather()));
            weatherForecast.setNightWeather(cast.getNightweather());
            weatherForecast.setNightWeatherImg(WeatherType.getCodeByDes(cast.getNightweather()));
            weatherForecast.setDayTemp(cast.getDaytemp());
            weatherForecast.setNightTemp(cast.getNighttemp());
            weatherForecast.setDayWind(cast.getDaywind());
            weatherForecast.setNightWind(cast.getNightwind());
            weatherForecast.setDayPower(cast.getDaypower());
            weatherForecast.setNightPower(cast.getNightpower());
            weatherForecast.setReportTime(forecast.getReporttime());
            weatherForecastList.add(weatherForecast);
        }
        return weatherForecastList;
    }
}

5.创建控制层

package com.example.dataproject.controller;

import com.example.dataproject.domain.*;
import com.example.dataproject.service.WeatherService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author qx
 * @date 2024/9/12
 * @des
 */
@RestController
@Slf4j
public class WeatherController {

    @Autowired
    private WeatherService weatherService;


    /**
     * 实时天气
     *
     * @param adcode 行政编码
     * @return
     */
    @GetMapping("/getWeather")
    public Weather getData(String adcode) {
        return weatherService.getData(adcode);
    }

    /**
     * 预告天气
     *
     * @param adcode 行政编码
     * @return
     */
    @GetMapping("/getForecatWeather")
    public List<WeatherForecast> getForecastData(String adcode) {
        return weatherService.getForecastData(adcode);
    }
}

6.启动程序进行测试

输入城市编码表进行测试,具体的城市编码表在高德地图上也提供了支持。

https://lbs.amap.com/api/webservice/download

SpringBoot集成高德天气API获取天气数据_SpringBoot_03

获取实时天气

SpringBoot集成高德天气API获取天气数据_SpringBoot_04

获取预报天气

SpringBoot集成高德天气API获取天气数据_SpringBoot_05


标签:weatherForecast,String,天气,private,API,import,com,public,SpringBoot
From: https://blog.51cto.com/u_13312531/12015257

相关文章

  • 基于SpringBoot的在线拍卖系统[源码+论文]
    摘要随着社会的发展,社会的各行各业都在利用信息化时代的优势。计算机的优势和普及使得各种信息系统的开发成为必需。在线拍卖系统,主要的模块包括管理员;首页、个人中心、用户管理、商品类型管理、拍卖商品管理、历史竞拍管理、竞拍订单管理、留言板管理、系统管理,用户;首页、......
  • springboot-实现csv文件导出功能
    excle文件导出,会遇到一个65535行限制的问题,就是导出的数据行数超过65535行就会导出失败,这个是excle本生的限制,这种情况下通常将导出的格式改成csv这样就可以跨过这个限制,同时生成的csv文件用office打开浏览效果与打开excle没有区别可以完美替代。1、依赖(比导出excle还少两个依赖)......
  • .NET 开源的功能强大的人脸识别 API
    前言人工智能时代,人脸识别技术已成为安全验证、身份识别和用户交互的关键工具。给大家推荐一款.NET开源提供了强大的人脸识别API,工具不仅易于集成,还具备高效处理能力。本文将介绍一款如何利用这些API,为我们的项目添加智能识别的亮点。项目介绍GitHub上拥有1.2k星标的C#......
  • springboot-实现excle文件导出功能
    后端直接生成一个excle文件,提供给前端进行下载1、依赖<!--excle操作--><dependency><groupId>cn.hutool</groupId><artifactId>hutool-all</artifactId><version>5.3.8</version><......
  • 课程答疑管理|基于springboot+vue的课程答疑系统(源码+数据库+文档)
    课程答疑管理目录基于springboot+vue的课程答疑管理系统一、前言二、系统设计三、系统功能设计 四、数据库设计 五、核心代码 六、论文参考七、最新计算机毕设选题推荐八、源码获取:博主介绍:✌️大厂码农|毕设布道师,阿里云开发社区乘风者计划专家博主,CSDN平台Jav......
  • fastapi 使用package 提供web 静态资源
    日常开发中对于静态资源的处理方法很多,但是如果静态资源也可以像包一样集成发布就会比较方便对于我们实际软件部署就会比较方便以下是关于fastapi通过package提供静态资源参考代码安装静态资源我们使用了bootstrap4,基于这个包提供app.pyfromfastapiimportFastAPIfrom......
  • 股票api接口程序化报备,程序化交易监管对个人量化交易者有何影响
    炒股自动化:申请官方API接口,散户也可以python炒股自动化(0),申请券商API接口python炒股自动化(1),量化交易接口区别Python炒股自动化(2):获取股票实时数据和历史数据Python炒股自动化(3):分析取回的实时数据和历史数据Python炒股自动化(4):通过接口向交易所发送订单Python炒股自动化(5):......
  • springboot jar包瘦身
    现在的项目结构是业务包引用comomn包,common包里又引用了很多其他的jar包,导致业务包打包出来动则就是一百甚至两百兆,现在要做到把公共的包放出来,放到一个单独的文件夹,业务包里只放业务代码。现在只需要修改mavenbuild节点如下:<build><finalName>pager</finalName>......
  • springboot+vue校园社团活动推荐系统【程序+论文+开题】计算机毕业设计
    系统程序文件列表开题报告内容研究背景随着高等教育的普及与校园文化生活的日益丰富,校园社团作为学生自主管理、自我提升的重要平台,其活动的多样性和参与度成为衡量校园文化活力的重要指标。然而,面对众多社团及其纷繁复杂的活动,学生们往往难以全面、及时地获取到感兴趣的活......
  • springboot+vue校园跑腿服务平台【程序+论文+开题】计算机毕业设计
    系统程序文件列表开题报告内容研究背景随着高校规模的不断扩大与学生生活节奏的加快,校园内对于便捷、高效生活服务的需求日益增长。传统的人工跑腿服务已难以满足学生群体对于时间管理和个性化服务的需求。同时,移动互联网技术的普及与智能手机的高度渗透,为构建校园跑腿服务......