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>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.8</version>
</dependency>
<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
forest:
bean-id: config0
backend: okhttp3
max-connections: 1000
max-route-connections: 500
timeout: 8000
connect-timeout: 8000
retry-count: 6
ssl-protocol: SSLv3
logEnabled: 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;
@GetMapping("/index")
public String index() {
return "您好,欢迎访问【" + applicationName + "】";
}
@GetMapping("/hello")
public String hello(@RequestParam(required = false) String msg) throws InterruptedException {
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;
}
并提前写好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 {
public static String getCellValue(Cell cell) {
String cellValue = "";
if (null != cell) {
switch (cell.getCellType()) {
case HSSFCell.CELL_TYPE_NUMERIC:
if (0 == cell.getCellType()) {
if (HSSFDateUtil.isCellDateFormatted(cell)) {
Date date = cell.getDateCellValue();
DateFormat formater = new SimpleDateFormat("yyyy/MM/dd");
cellValue = formater.format(date);
} else {
DecimalFormat df = new DecimalFormat("####.####");
cellValue = df.format(cell.getNumericCellValue());
}
}
break;
case HSSFCell.CELL_TYPE_STRING:
cellValue = cell.getStringCellValue();
break;
case HSSFCell.CELL_TYPE_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");
cellValue = formater.format(date);
} else {
FormulaEvaluator evaluator = cell.getSheet().getWorkbook().getCreationHelper()
.createFormulaEvaluator();
evaluator.evaluateFormulaCell(cell);
DecimalFormat df = new DecimalFormat("####.####");
cellValue = df.format(cell.getNumericCellValue());
}
}
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();
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);
if(n==2){
key = key+cellValue;
}else if(n==3){
value = value+cellValue+",";
}else if(n==4){
value = value+cellValue;
n=0;
}
}
if(k>2) hashMap.put(key.trim(),value.trim());
}
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(){
readExcelToMap();
for (Map.Entry<String,String> m : hashMap.entrySet()){
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");
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(使用前将#替换为@)