利用HttpClient获取数据,写入数据库

要获取的数据存在数据中台中,利用HttpClient

首先用key和secret获取登录的token

利用Postman发送get请求

得到响应

{
    "code": 10000,
    "message": "ok",
    "description": "api请求成功",
    "uuid": "a6528ee73a584e84b9515a997e0306b2",
    "result": {
        "access_token": "40cb7abb-2684-43ab-8548-4b5b51d2c431",
        "token_type": "bearer",
        "refresh_token": "f874e723-7fab-4cd2-b17b-f30d50811cdf",
        "expires_in": 6054,
        "scope": "server",
        "license": "made by pangu"
    }
}

之后请求数据,把获得的token和类型放入请求头一起发送请求

public void useApi () throws Exception{
        CloseableHttpClient httpClient = HttpClients.createDefault();
        String url = "http://192.168.200.151:30870/open_api/customization/tdzgccxyqxkdmysb/full?page=1&size=100";
        // 创建请求对象
        HttpGet httpGet = new HttpGet(url);
        httpGet.addHeader("Authorization","Bearer 3dfdccc3-d6bc-4628-9eef-93eb27844adc");

        // 发送请求,接收响应结果
        CloseableHttpResponse response = httpClient.execute(httpGet);

        // 获取服务器状态返回码
        int statusCode = response.getStatusLine().getStatusCode();
        System.out.println("服务端返回的状态码为:" + statusCode);
        HttpEntity entity = response.getEntity();
        String body = EntityUtils.toString(entity);
        JSONObject jsonResponse = JSONObject.parseObject(body);
        JSONArray resultArray = jsonResponse.getJSONObject("result").getJSONArray("data");
        JSONArray newArray = new JSONArray();
        for (int i = 0; i < resultArray.size(); i++) {
            JSONObject originalObj = resultArray.getJSONObject(i);
            JSONObject newObj = new JSONObject();
            newObj.put("name", originalObj.getString("sjxkmc"));
            newObj.put("num_code", originalObj.getString("cxflsjxkdm"));
            newObj.put("str_code", originalObj.getString("yqflsjxkdm"));
            newObj.put("parent_id",originalObj.getString("cxflejxkdm"));
//            newObj.put("startTime", originalObj.getString("start_date"));
//            newObj.put("endTime", originalObj.getString("end_date"));
            // 根据实际情况设置其他属性值
            newArray.add(newObj);
        }
        List<TDzgcGclxdm> tDzgcGclxdms = BeanUtil.copyToList(newArray, TDzgcGclxdm.class);
        this.saveBatch(tDzgcGclxdms);
        // 关闭服务
        response.close();
        httpClient.close();
    }

响应回来的数据为

{
    "code": 10000,
    "message": "ok",
    "description": "api请求成功",
    "uuid": "24218a2ef73547cdb1ce3bf2927a4935",
    "result": {
        "page": 1,
        "per": 2,
        "total": 80,
        "max_page": 40,
        "data_struct": {
            "id": "ID",
            "cxflyjxkdm": "测项分类一级学科代码",
            "yqflyjxkdm": "仪器分类一级学科代码",
            "yjxkmc": "一级学科名称",
            "cxflejxkdm": "测项分类二级学科代码",
            "yqflejxkdm": "仪器分类二级学科代码",
            "ejxkmc": "二级学科名称",
            "cxflsjxkdm": "测项分类三级学科代码",
            "yqflsjxkdm": "仪器分类三级学科代码",
            "sjxkmc": "三级学科名称",
            "tstamp": "时间戳"
        },
        "data": [
            {
                "id": "1",
                "cxflyjxkdm": "1",
                "yqflyjxkdm": "Z",
                "yjxkmc": "地震动观测",
                "cxflejxkdm": "11",
                "yqflejxkdm": "ZD",
                "ejxkmc": "位移",
                "cxflsjxkdm": "111",
                "yqflsjxkdm": "ZDS",
                "sjxkmc": "位移型短周期",
                "tstamp": "20240411074023"
            },
            {
                "id": "10",
                "cxflyjxkdm": "2",
                "yqflyjxkdm": "X",
                "yjxkmc": "地壳形变观测",
                "cxflejxkdm": "21",
                "yqflejxkdm": "XG",
                "ejxkmc": "重力",
                "cxflsjxkdm": "212",
                "yqflsjxkdm": "XGR",
                "sjxkmc": "连续重力观测",
                "tstamp": "20240411074023"
            }
        ],
        "encrypted_field": ""
    }
}

可以看出需要的是result中的data部分的数据,利用hutool中的JsonUtil将反序列化

 HttpEntity entity = response.getEntity();
        String body = EntityUtils.toString(entity);
        JSONObject jsonResponse = JSONObject.parseObject(body);
        JSONArray resultArray = jsonResponse.getJSONObject("result").getJSONArray("data");
        JSONArray newArray = new JSONArray();
        for (int i = 0; i < resultArray.size(); i++) {
            JSONObject originalObj = resultArray.getJSONObject(i);
            JSONObject newObj = new JSONObject();
            newObj.put("name", originalObj.getString("sjxkmc"));
            newObj.put("num_code", originalObj.getString("cxflsjxkdm"));
            newObj.put("str_code", originalObj.getString("yqflsjxkdm"));
            newObj.put("parent_id",originalObj.getString("cxflejxkdm"));
//            newObj.put("startTime", originalObj.getString("start_date"));
//            newObj.put("endTime", originalObj.getString("end_date"));
            // 根据实际情况设置其他属性值
            newArray.add(newObj);
        }
        List<TDzgcGclxdm> tDzgcGclxdms = BeanUtil.copyToList(newArray, TDzgcGclxdm.class);
        this.saveBatch(tDzgcGclxdms);

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值