Springboot通过http请求将返回的json数据解析生成SQL插入数据库(用于数据库同步)

一、需求

我们无法通过账号密码去访问客户的数据库,但是我们可以通过访问客户提供的接口去获取数据,因此,我们可以通过请求接口的方式,对返回的json数据进行解析,然后就能得到我们所需要的数据。

  1. 通过配置文件的方式配置所需要信息,增强灵活性

  2. 请求到的数据,如果在数据库存在就更新,不存在就插入

  3. 如果json中某个数据不存在,则自动补全,并且置为空值,存入数据库中

  4. 返回的数据如果存在时间戳,需要进行解析再存入到数据库中

  5. 使用http连接池,减低频繁进行连接断开的开销

二、思路

工具包:

Httpclient:发送http请求

jsonPath:用于解析字符串

image.png

三、HttpClient工具使用

引入maven依赖:

<!-- httpClient -->
<dependency>
	<groupId>org.apache.httpcomponents</groupId>
	<artifactId>httpclient</artifactId>
	<version>4.5.5</version>
</dependency>

新增一个HttpClientUtil工具类来使用:

public class HttpClientUtil {
    
    private static CloseableHttpClient httpClient = null;

    public static void init() {
        //1.创建连接池管理器,默认情况下,此实现将为每个给定路由创建不超过2个并发连接,并且总共不超过20个连接
        PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(60000,//1.1
                TimeUnit.MILLISECONDS);//tomcat服务器默认支持保持60s的链接,超过60s,会关闭客户端的链接
        connectionManager.setMaxTotal(1000);//设置连接器最多同时支持1000个链接
        connectionManager.setDefaultMaxPerRoute(50);//设置每个路由最多支持50个链接。注意这里路由是指IP+PORT或者指域名

        //2.创建httpclient对象
        httpClient = HttpClients.custom()
                .setConnectionManager(connectionManager)
                .disableAutomaticRetries()
                .build();
    }


    public static String doGet(String url) {
        //初始化连接池
        if(httpClient == null)
            init();
        String httpResponse = null;
        // 创建Get请求
        HttpGet httpGet = new HttpGet(url);
        // 响应模型
        CloseableHttpResponse response = null;
        try {
            // 配置信息
            RequestConfig requestConfig = RequestConfig.custom()
                    // 设置连接超时时间(单位毫秒)
                    .setConnectTimeout(5000)
                    // 设置请求超时时间(单位毫秒)
                    .setConnectionRequestTimeout(5000)
                    // socket读写超时时间(单位毫秒)
                    .setSocketTimeout(5000)
                    // 设置是否允许重定向(默认为true)
                    .setRedirectsEnabled(true).build();

            // 将上面的配置信息 运用到这个Get请求里
            httpGet.setConfig(requestConfig);


            // 由客户端执行(发送)Get请求
            response = httpClient.execute(httpGet);
            // 从响应模型中获取响应实体
            HttpEntity responseEntity = response.getEntity();
            //获取请求体
            httpResponse = EntityUtils.toString(responseEntity,"UTF-8");
        } catch (ParseException | IOException e) {
            e.printStackTrace();
        } finally {
            //回收连接
            if (null != response) {
                try {
                    EntityUtils.consume(response.getEntity());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return httpResponse;
    }

    public static String doPost(String url) throws IOException {
        if(httpClient == null)
            init();
        String httpResponse = null;
        // 获得Http客户端(可以理解为:你得先有一个浏览器;注意:实际上HttpClient与浏览器是不一样的)
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();

        // 创建Post请求
        HttpPost httpPost = new HttpPost(url);
        // 响应模型
        CloseableHttpResponse response = null;
        HttpEntity responseEntity = null;
        try {
            // 由客户端执行(发送)Post请求

            response = httpClient.execute(httpPost);
            // 从响应模型中获取响应实体
            responseEntity = response.getEntity();
            if (responseEntity != null) {
                //获取请求体
                httpResponse = EntityUtils.toString(responseEntity,"UTF-8");
            }
        } catch (ParseException | IOException e) {
            e.printStackTrace();
        } finally {
            //回收连接
            if (null != response) {
                try {
                    EntityUtils.consume(response.getEntity());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return httpResponse;
    }

    public static String doPostWithEntity(String url,String json) throws IOException {
        if(httpClient == null)
            init();
        String httpResponse = null;
        // 获得Http客户端(可以理解为:你得先有一个浏览器;注意:实际上HttpClient与浏览器是不一样的)
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();

        // 创建Post请求
        HttpPost httpPost = new HttpPost(url);
        // 响应模型
        CloseableHttpResponse response = null;
        HttpEntity responseEntity = null;
        try {
            StringEntity requestEntity = new StringEntity(json,"utf-8");
            requestEntity.setContentEncoding("UTF-8");
            httpPost.setHeader("Content-type", "application/json");
            httpPost.setEntity(requestEntity);

            // 由客户端执行(发送)Post请求
            response = httpClient.execute(httpPost);
            // 从响应模型中获取响应实体
            responseEntity = response.getEntity();
            if (responseEntity != null) {
                //获取请求体
                httpResponse = EntityUtils.toString(responseEntity,"UTF-8");
            }
        } catch (ParseException | IOException e) {
            e.printStackTrace();
        } finally {
            //回收连接
            if (null != response) {
                try {
                    EntityUtils.consume(response.getEntity());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return httpResponse;
    }
}

四、JsonPath工具使用

引入maven:

<!-- jsonPath json提取工具 -->
<dependency>
	<groupId>com.jayway.jsonpath</groupId>
	<artifactId>json-path</artifactId>
	<version>2.4.0</version>
</dependency>

新增一个通过jsonPath工具解析json后生成插入SQL的工具类JsonToSqlUtill


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值