SpringBoot整合forest(调用彩云API获取所有城市的实时天气)

2023-05-16

Forest简介:

Forest是一个高层的、极简的轻量级HTTP调用API框架。相比于直接使用Httpclient您不再用写一大堆重复的代码了,而是像调用本地方法一样去发送HTTP请求。

环境配置:

因为本项目想要将调用得到的数据存进数据库,所以我在创建springBoot项目的时候勾选了以下的模块,具体的mybatis配置请看这篇博客
在这里插入图片描述
除了上面的环境,还需要添加本篇博客的主题forest,由于后面设计操作excel,所以还需要poi的相关jar,在pom中添加下面的依赖:

 		<dependency>
            <groupId>com.dtflys.forest</groupId>
            <artifactId>spring-boot-starter-forest</artifactId>
            <version>1.4.0</version>
        </dependency>

        <!-- Excel中表格POI插件 -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.8</version>
        </dependency>
        <!-- poi插件 XSSFWorkbook和SXSSHWorkbook -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.8</version>
        </dependency>

plugins添加下面的插件:

            <!--添加配置跳过测试-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.22.1</version>
                <configuration>
                    <skipTests>true</skipTests>
                </configuration>
            </plugin>
            <!--添加配置跳过测试-->

application.yml的配置:

spring:
  application:
    name: demo
  datasource:
    username: root
    password: root
    url: jdbc:mysql://127.0.0.1:3306/bigdata?serverTimezone=Asia/Shanghai
    driver-class-name: com.mysql.cj.jdbc.Driver


server:
  port: 8888
  servlet:
    context-path: /demo  #  添加项目名为 demo



# ========================== ↓↓↓↓↓↓ forest配置 ↓↓↓↓↓↓ ==========================
forest:
  bean-id: config0 # 在spring上下文中bean的id, 默认值为forestConfiguration
  backend: okhttp3 # 后端HTTP API: okhttp3 【支持`okhttp3`/`httpclient`】
  max-connections: 1000 # 连接池最大连接数,默认值为500
  max-route-connections: 500 # 每个路由的最大连接数,默认值为500
  timeout: 8000 # 请求超时时间,单位为毫秒, 默认值为3000
  connect-timeout: 8000 # 连接超时时间,单位为毫秒, 默认值为2000
  retry-count: 6 # 请求失败后重试次数,默认为0次不重试
  ssl-protocol: SSLv3 # 单向验证的HTTPS的默认SSL协议,默认为SSLv3
  logEnabled: true # 打开或关闭日志,默认为true

至此,环境就已经配置完毕!

简单的入门demo

  • 书写一个返回json字符串的controller层,向外界暴露接口。
package org.lzl.forest.api;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class IndexController {
    @Value(value = "${spring.application.name}")
    private String applicationName;     //demo

    @GetMapping("/index")
    public String index() {
        return "您好,欢迎访问【" + applicationName + "】";
    }

    @GetMapping("/hello")
    public String hello(@RequestParam(required = false) String msg) throws InterruptedException {
        // 模拟业务耗时处理流程
//        Thread.sleep(2 * 1000L);
        return "hello: " + msg;
    }
}

  • 书写一个接口MyClient,在@Get的括号中书写请求controller层的接口。
package org.lzl.forest.rpc.client;

import com.dtflys.forest.annotation.DataVariable;
import com.dtflys.forest.annotation.Get;
import java.util.Map;

public interface MyClient {
 	/**
     * 本地测试接口
     */
    @Get(url = "http://127.0.0.1:8888/demo/index")
    String index();

    @Get(url = "http://127.0.0.1:8888/demo/hello?msg=${msg}")
    String hello(@DataVariable("msg") String msg);
}    
  • 在springboot的启动类上加上@ForestScan注解,扫描上面写的接口的包
    在这里插入图片描述
  • 测试 注意在运行前要提前运行springboot的启动类。
@RunWith(SpringRunner.class)
@SpringBootTest
class ForestApplicationTests {

    @Autowired
    private MyClient myClient;
	
    @Test
    public void testForest() throws Exception {
        // 调用接口
        String index = myClient.index();
        System.out.println(index);
        String hello = myClient.hello("测试...");
        System.out.println(hello);
    }
}

运行结果:
在这里插入图片描述

正式进入本篇博客的主题

彩云api其实也就是像上面我们自己写的controller,那样像外界暴露一个端口,返回一个json字符串。想要使用彩云就需要先去注册:注册地址

测试接口

注册好了之后,根据里面的api文档,在MyClient接口中加入下面的方法:

在这里插入图片描述
先测试这个接口能否调用成功:

    @Test
    public void testWeather() throws Exception{
        Map weather = myClient.getWeather("121.475070,31.223570");
        System.out.println(weather.toString());
    }

运行很成功:
在这里插入图片描述
根据返回的json字符串的详细信息,设计bean的属性,和数据库的字段:

public class Weather {
    private String cityName;//城市名字
    private String temperature;//温度
    private String cloudrate;//云量
    private String skycon;//
    private String visibility;//能见度
    private String dswrf;//
    private String pressure;//压力
    private String apparent_temperature;//表观温度
    private String humidity;//湿度
    
    //省略getter,setter 和 构造器
}

在这里插入图片描述
并提前写好mapper层;

package org.lzl.forest.mapper;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.lzl.forest.pojo.Weather;

@Mapper
public interface MyMapper {
    @Insert("insert into weather values(#{cityName},#{temperature},#{cloudrate},#{skycon},#{visibility},#{dswrf},#{pressure},#{apparent_temperature},#{humidity})")
    void insert(Weather weather);
}

由于彩云是根据经纬度来返回天气情况的,所以我们需要先下载城市和经纬度的对照表,下载地址,由于解压之后是个excel文件。
在这里插入图片描述

下面我们的工作就是读取excel的数据,将数据封装成hjava的map。map的key为城市名,map的value为(经度,维度)的字符串。
对excel的操作就需要用到poi的jar(前面已经引入了!)先写个PIO的帮助类:

package org.lzl.forest.util;

import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.FormulaEvaluator;

public class POIUtil {
    /**
     * 获取cell中的值并返回String类型
     *
     * @param cell
     * @return String类型的cell值
     */
    public static String getCellValue(Cell cell) {
        String cellValue = "";
        if (null != cell) {
            // 以下是判断数据的类型
            switch (cell.getCellType()) {
                case HSSFCell.CELL_TYPE_NUMERIC: // 数字
                    if (0 == cell.getCellType()) {// 判断单元格的类型是否则NUMERIC类型
                        if (HSSFDateUtil.isCellDateFormatted(cell)) {// 判断是否为日期类型
                            Date date = cell.getDateCellValue();
//                      DateFormat formater = new SimpleDateFormat("yyyy/MM/dd HH:mm");
                            DateFormat formater = new SimpleDateFormat("yyyy/MM/dd");
                            cellValue = formater.format(date);
                        } else {
                            // 有些数字过大,直接输出使用的是科学计数法: 2.67458622E8 要进行处理
                            DecimalFormat df = new DecimalFormat("####.####");
                            cellValue = df.format(cell.getNumericCellValue());
                            // cellValue = cell.getNumericCellValue() + "";
                        }
                    }
                    break;
                case HSSFCell.CELL_TYPE_STRING: // 字符串
                    cellValue = cell.getStringCellValue();
                    break;
                case HSSFCell.CELL_TYPE_BOOLEAN: // Boolean
                    cellValue = cell.getBooleanCellValue() + "";
                    break;
                case HSSFCell.CELL_TYPE_FORMULA: // 公式
                    try {
                        // 如果公式结果为字符串
                        cellValue = String.valueOf(cell.getStringCellValue());
                    } catch (IllegalStateException e) {
                        if (HSSFDateUtil.isCellDateFormatted(cell)) {// 判断是否为日期类型
                            Date date = cell.getDateCellValue();
//                      DateFormat formater = new SimpleDateFormat("yyyy/MM/dd HH:mm");
                            DateFormat formater = new SimpleDateFormat("yyyy/MM/dd");
                            cellValue = formater.format(date);
                        } else {
                            FormulaEvaluator evaluator = cell.getSheet().getWorkbook().getCreationHelper()
                                    .createFormulaEvaluator();
                            evaluator.evaluateFormulaCell(cell);
                            // 有些数字过大,直接输出使用的是科学计数法: 2.67458622E8 要进行处理
                            DecimalFormat df = new DecimalFormat("####.####");
                            cellValue = df.format(cell.getNumericCellValue());
//                          cellValue = cell.getNumericCellValue() + "";
                        }
                    }
//              //直接获取公式
//              cellValue = cell.getCellFormula() + "";
                    break;
                case HSSFCell.CELL_TYPE_BLANK: // 空值
                    cellValue = "";
                    break;
                case HSSFCell.CELL_TYPE_ERROR: // 故障
                    cellValue = "非法字符";
                    break;
                default:
                    cellValue = "未知类型";
                    break;
            }
        }
        return cellValue;
    }
}

然后在springboot提供的测试类中正式写代码,封装到map(见方法readExcelToMap),然后遍历map,挨个请求每个map键值对的value地址,将数据存进数据库(见方法insertWeatherIntoDb)

@RunWith(SpringRunner.class)
@SpringBootTest
class ForestApplicationTests {

    static HashMap<String, String> hashMap = new HashMap();
    
     public static void readExcelToMap(){
        String filePath = "src/main/resources/static/adcode-release-2020-06-10.xlsx";
        InputStream fis = null;
        try {
            fis = new FileInputStream(filePath);
            Workbook workbook = null;
            if (filePath.endsWith(".xlsx")) {
                workbook = new XSSFWorkbook(fis);
            } else if (filePath.endsWith(".xls") || filePath.endsWith(".et")) {
                workbook = new HSSFWorkbook(fis);
            }
            fis.close();
            /* 读EXCEL文字内容 */
            // 获取第一个sheet表,也可使用sheet表名获取
            Sheet sheet = workbook.getSheetAt(0);
            // 获取行
            Iterator<Row> rows = sheet.rowIterator();
            Row row;
            Cell cell;
            int k = 0;
            while (rows.hasNext()) {
                k++;
                row = rows.next();
                // 获取单元格
                Iterator<Cell> cells = row.cellIterator();
                int n = 0 ;
                String key = "";
                String value = "";
                while (cells.hasNext()) {
                    n++;
                    cell = cells.next();
                    String cellValue = POIUtil.getCellValue(cell);
//                    System.out.print(cellValue + " ");
                    if(n==2){
//                        System.out.print(cellValue+ " ");
                        key = key+cellValue;
                    }else if(n==3){
//                        System.out.print(cellValue+",");
                        value = value+cellValue+",";
                    }else if(n==4){
//                        System.out.print(cellValue);
                        value = value+cellValue;
                        n=0;
                    }
                }
//                System.out.println(key+"-----"+value);
                if(k>2) hashMap.put(key.trim(),value.trim());
//                System.out.println();
            }

            System.out.println(hashMap.toString());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (null != fis) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }


    @Test
    public void insertWeatherIntoDb(){
//        HashMap<String, String> map = new HashMap();
//        map.put("北京市","116.407526,39.90403");
//        map.put("天津市","117.200983,39.084158");

        readExcelToMap();
        for (Map.Entry<String,String> m : hashMap.entrySet()){
//            System.out.println("Key = " + m.getKey() + ", Value = " + m.getValue());
            Map api = myClient.getWeather(m.getValue());
            Map result = (Map) api.get("result");
            Map realtime = (Map) result.get("realtime");
            System.out.println(realtime.toString());
            String temperature = "" + realtime.get("temperature");//将double转为String
            String cloudrate = "" + realtime.get("cloudrate");
            String skycon = "" + realtime.get("skycon");
            String visibility = "" + realtime.get("visibility");
            String dswrf = "" + realtime.get("dswrf");
            String pressure = "" + realtime.get("pressure");
            String apparent_temperature = "" + realtime.get("apparent_temperature");
            String humidity = "" + realtime.get("humidity");
            Weather weather = new Weather(m.getKey(),temperature,cloudrate,skycon,visibility,dswrf,pressure,apparent_temperature,humidity);
            myMapper.insert(weather);
            System.out.println("成功!!!!!!!!!");
        }

    }

    }

运行效果:
在这里插入图片描述
在这里插入图片描述

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

SpringBoot整合forest(调用彩云API获取所有城市的实时天气) 的相关文章

随机推荐

  • MATLAB Simmechanics/Simscape四旋翼无人机控制仿真(4) 串级姿态控制

    MATLAB Simmechanics Simscape四旋翼无人机控制仿真 xff08 4 xff09 串级姿态控制 MATLAB Simmechanics Simscape四旋翼无人机控制仿真 xff08 4 xff09 串级姿态控制1
  • 年假总述

    假期结束了 xff0c 虽然只有短短的五天 xff0c 但是这期间看到了听到了很多事情 xff0c 自己也想了很多 xff0c 以流水账的形式写一写 xff0c 也算是一种回忆吧 与姥爷聊教育 姥爷是高中的退休教师 xff0c 这次回去和姥
  • 烟台大学参加2011移动开发者大会和参观CSDN

    第一天 每年一度的中国移动开发者大会如期举行 xff0c 这次在潘永强老师的帮助下 xff0c 幸运的获得了此次大会的门票 xff0c 激动万分 xff0c 这是我们第一次北京之行 xff0c 带着无限的幻想和憧憬 xff0c 踏上了前往北
  • Linux的信号量

    信号量 xff08 semaphore xff09 与已经介绍过的 IPC 结构不同 xff0c 它是一个计数器 信号量用于实现进程间的互斥与同步 xff0c 而不是用于存储进程间通信数据 1 特点 1 信号量用于进程间同步 xff0c 若
  • 21 kubesphere安装部署

    文章目录 一 kubersphere1 KuberSphere简介2 全栈的 Kubernetes 容器云 PaaS 解决方案3 为什么选择 KubeSphere xff1f 4 主要功能 二 kubespshe 安装 xff08 Kube
  • 关于AlreadyExistsError: Another metric with the same name already exists.的解决方案

    这项错误发生在我刚安装完tensorflow xff0c 想import keras包的时候发生了如下的错误 xff1a Tensorflow python framework errors impl AlreadyExistsError
  • uC/OSIII在Cortex-M3的任务切换和中断退出分析

    uC OSIII在任务中执行OSSched相关的函数和在中断退出后都会开始执行调度 xff0c 这是它的调度机制 而按uC OSIII书中所讲 xff0c 普通任务切换和从中断中退出后的任务切换应该是不同的函数 xff0c 因为普通任务切换
  • 每天一个adb命令:dumpsys命令详解

    dumpsys是一个能帮助我们对手机进行性能分析的命令 xff0c 它可以帮助我们获取电池 内存 cpu 磁盘 wifi等等信息 xff0c 具体能查询的信息可以通过命令 xff1a adb span class hljs built in
  • vs2017如何创建一个asax文件

    VS2017无法为网站创建Global asax文件 xff0c 导致出现错误WebForms UnobtrusiveValidationMode 需要 jquery ScriptResourceMapping 解决方案如下 xff1a 勾
  • Spring Security OAuth2.0认证授权

    文章目录 1 基本概念1 1 什么是认证1 2 什么是会话1 3什么是授权1 4授权的数据模型1 4 RBAC1 4 1 基于角色的访问控制 2 基于Session的认证方式3 整合案例3 1 SpringMVC 43 Servlet3 0
  • 浏览器不显示favicon.ico怎么办?

    原因1 xff1a 连接文件的路径不对 如上图路径的话href连接路径应该写成 xff1a href 61 34 img favicon ico 34 xff0c 具体如下 xff1a span class token operator l
  • VNCViewer连接树莓派失败、显示超时的部分原因

    刚入手树莓派 xff0c 在用VNCViewer这款软件实现树莓派的图形化桌面时遇到了一些坑 xff0c 在这里分享 xff0c 希望能对大家有所帮助 1 在文本框内输入IP地址之后一定要记得加上 端号 xff0c 如下图所示 这个端号在P
  • Kubernetes中文手册

    Kubernetes中文手册 https www kubernetes org cn kubernetes pod
  • JSP中文乱码问题终极解决方案

    在介绍方法之前我们首先应该清楚具体的问题有哪些 xff0c 笔者在本博客当中论述的 JSP 中文乱码问题有如下几个方面 xff1a 页面乱码 参数乱码 表单乱码 源文件乱码 下面来逐一解决其中的乱码问题 一 JSP 页面中文乱码 在 JSP
  • JS表白代码

    简单的JS弹窗表白代码 思路 xff1a 只有当用户输入1 xff08 表示喜欢你 xff09 才有进一步浏览的资格 如果用户输入2 xff08 不喜欢你 xff09 就会陷入死循环进行撒娇 xff0c 只有当用户输入1 xff0c 才可以
  • Quartz框架详解

    Quartz框架可以实现 异步定时任务 Quartz框架下载地址 注意1版本和2版本写法完全不一样 xff0c 本文采用的是2 x版本 下载完毕后进入进入lib文件夹 xff0c 然后将下面的几个jar引入项目 xff1a 基本实现步骤 x
  • 前端的端口问题

    本文 xff0c 将以通俗易懂的方式剖析 服务器 电脑 是怎么访问html文件 先说一下前置知识 xff1a 首先我们得知道一件事情 xff1a 电脑中每个运行的程序都对应着某个端口 xff0c 举个例子 xff1a 我们都知道mysql默
  • 湖北师范大学java习题汇编(超详细!已经进行了章节划分)

    表达式和流程控制语句 1 验证歌德巴赫猜想 一个充分大的偶数 xff08 大于或等于6 xff09 可以分解为两个素数之和 试编程序 xff0c 将 6至50之间全部偶数表示为两个素数之和 span class token keyword
  • OPENCV(五) 对给定的车牌进行字符分割

    下面有这样的一个车牌号 xff1a 现在的任务是将每一个字符区分开来 xff0c 并方框圈出来 完成这个功能需要以下的步骤 xff1a 1 灰度处理 span class token comment 读取图片 span image1 spa
  • SpringBoot整合forest(调用彩云API获取所有城市的实时天气)

    Forest简介 xff1a Forest是一个高层的 极简的轻量级HTTP调用API框架 相比于直接使用Httpclient您不再用写一大堆重复的代码了 xff0c 而是像调用本地方法一样去发送HTTP请求 环境配置 xff1a 因为本项