java通过httpClient从接口请求数据入库以及自动生成实体工具类

java可以通过Apache HttpClient请求接口数据
Apache HttpClient maven jar包引入:

        
            org.apache.httpcomponents
            httpcore
            4.4.6
        
            org.apache.httpcomponents
            httpclient
            4.5.2
        

请求接口方法工具类

package com.test.app.utils;

import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

/**
 * @description TODO
 * @Author zhaohy
 **/
public class HttpUtil {

    public static String post(JSONObject json, String url) throws Exception{
        CloseableHttpClient httpclient = HttpClientBuilder.create().build();
        HttpPost post = new HttpPost(url);
        CloseableHttpResponse  response = null;
        InputStream in = null;
        BufferedReader br = null;
        String result = "";
        try {
            StringEntity s = new StringEntity(json.toString(),"utf-8");
            s.setContentEncoding("UTF-8");
            /*发送json数据需要设置contentType*/
            s.setContentType("application/json");
            post.setEntity(s);
            post.setHeader("Content-Type","application/json;charset=utf-8");
            response = httpclient.execute(post);
            in = response.getEntity().getContent();
            br = new BufferedReader(new InputStreamReader(in, "utf-8"));
            StringBuilder strber= new StringBuilder();
            String line = null;
            while((line = br.readLine())!=null){
                strber.append(line+'\n');
            }
            result = strber.toString();
            if(response.getStatusLine().getStatusCode()!=HttpStatus.SC_OK){
                if(StringUtils.isBlank(result)) result = "服务器异常";
                throw new Exception(result);
            }
           // System.out.println("返回数据="+result);
        } catch (Exception e) {
            //System.err.println("调用接口出错::::::::::::"+e.getMessage());
            throw new Exception(e.getMessage());
        } finally {
            response.close();
            httpclient.close();
            br.close();
            in.close();
        }
        return result;
    }

public static String get(JSONObject paramsObj, String url, Map headerMap) throws Exception {
        CloseableHttpClient httpclient = HttpClientBuilder.create().build();
        CloseableHttpResponse  response = null;
        InputStream in = null;
        BufferedReader br = null;
        String result = "";
        try {
            StringBuffer param = new StringBuffer();
            int i = 0;
            
            Set> entries = paramsObj.entrySet();
            for (Entry entry:entries){
                if (i == 0)
                    param.append("?");
                else
                    param.append("&");
                param.append(entry.getKey()).append("=").append(entry.getValue());
                i++;
            }
            
            url += param;
            HttpGet post = new HttpGet(url);
            post.setHeader("Content-Type","application/json;charset=utf-8");
            
            Set> headerEntries = headerMap.entrySet();
            for (Entry headerEntry:headerEntries){
                post.setHeader(headerEntry.getKey(), headerEntry.getValue());
            }
            
            response = httpclient.execute(post);
            in = response.getEntity().getContent();
            br = new BufferedReader(new InputStreamReader(in, "utf-8"));
            StringBuilder strber= new StringBuilder();
            String line = null;
            while((line = br.readLine())!=null){
                strber.append(line+'\n');
            }
            result = strber.toString();
            if(response.getStatusLine().getStatusCode()!=HttpStatus.SC_OK){
                if(StringUtils.isBlank(result)) result = "服务器异常";
                throw new Exception(result);
            }
            //System.out.println("返回数据="+result);
        } catch (Exception e) {
           // System.err.println("调用接口出错::::::::::::"+e.getMessage());
            throw new Exception(e.getMessage());
        } finally {
            response.close();
            httpclient.close();
            br.close();
            in.close();
        }
        return result;
    }
}

上面的post方法 丢一个JSONObject类型的json参数进去作为请求api的参数,url是api请求地址 返回String类型的json数据。

调用实例:

      JSONObject params = new JSONObject();
      params.put("area_type","region");
      params.put("area_codes",new JSONArray());
      params.put("linkage", 1);
      
      String resultJson = HttpUtil.post(params, regionProvinceCityApiUrl);

如此就可以成功返回数据并对返回的json做进一步处理了。

有的时候入库数据字段比较多 实体类如果手动创建会写一大堆的字段,这里介绍一个根据数据库表自动生成实体类的工具类,当然mybatis有自带类似功能的插件,但是这个工具类是通过jdbc连接数据库,理论上不挑框架。

package com.test.app.utils;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @description 实体类代码生成器
 **/
public class ModelCodeGenerator {
    //基本数据配置
    private String packageOutPath = "com.test.app.model";// 指定实体生成所在包的路径
    private String authorName = "zhaohy";// 作者名字
    private String tablename = "FDM_INDRS_MONTH_KPI_REPORT_ORI";// 表名
    private String modelName = "MonthKpi";// 实体类名
    private String[] colnames; // 列名数组
    private String[] colTypes; // 列名类型数组
    private String version = "V1.0"; // 版本
    private int[] colSizes; // 列名大小数组
    private boolean f_util = false; // 是否需要导入包java.util.*
    private boolean f_sql = false; // 是否需要导入包java.sql.*
    private boolean f_lang = false; // 是否需要导入包java.sql.*
    private String defaultPath = "/src/main/java/";
    // 数据库连接
    private static final String URL = "jdbc:oracle:thin:@XXXX:1521:DBNT";
    private static final String NAME = "XXXX";
    private static final String PASS = "XXXX";
    private static final String DRIVER = "oracle.jdbc.driver.OracleDriver";

    /*
     * 构造函数
     */
    public ModelCodeGenerator() {
        // 创建连接
        Connection con;
        // 查要生成实体类的表
        String sql = "select * from " + tablename;
        PreparedStatement pStemt = null;
        try {
            try {
                Class.forName(DRIVER);
            } catch (ClassNotFoundException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            con = DriverManager.getConnection(URL, NAME, PASS);
            pStemt = con.prepareStatement(sql);
            ResultSet rs2 = pStemt.executeQuery();
            ResultSetMetaData rsmd = pStemt.getMetaData();
            int size = rsmd.getColumnCount(); // 统计列
            colnames = new String[size];
            colTypes = new String[size];
            colSizes = new int[size];
            for (int i = 0; i < size; i++) {

                colnames[i] = UnderlineToHump(rsmd.getColumnName(i + 1).toLowerCase());
                colTypes[i] = rsmd.getColumnTypeName(i + 1);
                //自动生成包配置

                // if (colTypes[i].equalsIgnoreCase("datetime")) {
                // f_util = true;
                // }
                if (colTypes[i].equalsIgnoreCase("image") || colTypes[i].equalsIgnoreCase("text")
                        || colTypes[i].equalsIgnoreCase("datetime") || colTypes[i].equalsIgnoreCase("time")
                        || colTypes[i].equalsIgnoreCase("date") || colTypes[i].equalsIgnoreCase("datetime2")) {
                    f_sql = true;
                }
                // if (colTypes[i].equalsIgnoreCase("int")) {
                // f_lang = true;
                // }
                colSizes[i] = rsmd.getColumnDisplaySize(i + 1);
            }

            String content = parse(colnames, colTypes, colSizes);

            try {
                File directory = new File("");

                String path = this.getClass().getResource("").getPath();

                System.out.println(path);

                String outputPath = directory.getAbsolutePath() + this.defaultPath
                        + this.packageOutPath.replace(".", "/") + "/" + initcap(modelName) + ".java";
                System.out.println("执行完毕,生成路径为:"+outputPath);
                FileWriter fw = new FileWriter(outputPath);
                PrintWriter pw = new PrintWriter(fw);
                pw.println(content);
                pw.flush();
                pw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {

        }
    }

    /**
     * 功能:生成实体类主体代码
     *
     * @param colnames
     * @param colTypes
     * @param colSizes
     * @return
     */
    private String parse(String[] colnames, String[] colTypes, int[] colSizes) {
        StringBuffer sb = new StringBuffer();
        // 生成package包路径
        sb.append("package " + this.packageOutPath + ";\r\n");
        // 判断是否导入工具包
        if (f_util) {
            sb.append("import java.util.Date;\r\n");
        }
        if (f_sql) {
            sb.append("import java.sql.*;\r\n");
        }
        if (f_lang) {
            sb.append("import java.lang.*;\r\n");
        }

        sb.append("\r\n");
        // 注释部分
        sb.append("   /**\r\n");
        sb.append("    * @文件名称:" + this.modelName + ".java\r\n");
        sb.append("    * @创建时间:" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + "\r\n");
        sb.append("    * @创  建  人:" + this.authorName + " \r\n");
        sb.append("    * @文件描述:" + modelName + " 实体类\r\n");
        sb.append("    * @文件版本:" + this.version + " \r\n");
        sb.append("    */ \r\n");
        // 实体部分
        sb.append("\r\n\r\npublic class " + initcap(modelName) + "{\r\n");
        processAllAttrs(sb);// 属性
        processAllMethod(sb);// get set方法
        sb.append("}\r\n");

        // System.out.println(sb.toString());
        return sb.toString();
    }

    /**
     * 功能:生成所有属性
     *
     * @param sb
     */
    private void processAllAttrs(StringBuffer sb) {

        for (int i = 0; i < colnames.length; i++) {
            sb.append("\tprivate " + sqlType2JavaType(colTypes[i]) + " " + colnames[i] + ";\r\n");
        }

    }

    /**
     * 功能:生成所有方法
     *
     * @param sb
     */
    private void processAllMethod(StringBuffer sb) {

        for (int i = 0; i < colnames.length; i++) {
            sb.append("\tpublic void set" + initcap(colnames[i]) + "(" + sqlType2JavaType(colTypes[i]) + " "
                    + colnames[i] + "){\r\n");
            sb.append("\tthis." + colnames[i] + "=" + colnames[i] + ";\r\n");
            sb.append("\t}\r\n");
            sb.append("\tpublic " + sqlType2JavaType(colTypes[i]) + " get" + initcap(colnames[i]) + "(){\r\n");
            sb.append("\t\treturn " + colnames[i] + ";\r\n");
            sb.append("\t}\r\n");
        }

    }

    /**
     * 功能:将输入字符串的首字母改成大写
     *
     * @param str
     * @return
     */
    private String initcap(String str) {

        char[] ch = str.toCharArray();
        if (ch[0] >= 'a' && ch[0] <= 'z') {
            ch[0] = (char) (ch[0] - 32);
        }

        return new String(ch);
    }

    /**
     * 功能:获得列的数据类型
     *
     * @param sqlType
     * @return
     */
    private String sqlType2JavaType(String sqlType) {

        if (sqlType.equalsIgnoreCase("binary_double")) {
            return "double";
        } else if (sqlType.equalsIgnoreCase("binary_float")) {
            return "float";
        } else if (sqlType.equalsIgnoreCase("blob")) {
            return "byte[]";
        } else if (sqlType.equalsIgnoreCase("blob")) {
            return "byte[]";
        } else if (sqlType.equalsIgnoreCase("char")
                || sqlType.equalsIgnoreCase("nvarchar2")
                || sqlType.equalsIgnoreCase("varchar2")) {
            return "String";
        } else if (sqlType.equalsIgnoreCase("date")
                || sqlType.equalsIgnoreCase("timestamp")
                || sqlType.equalsIgnoreCase("timestamp with local time zone")
                || sqlType.equalsIgnoreCase("timestamp with time zone")) {
            return "Date";
        } else if (sqlType.equalsIgnoreCase("number")) {
            return "Double";
        }
        return "String";
    }

    /**
     * 出口 TODO
     *
     * @param args
     */
    public static void main(String[] args) {

        new ModelCodeGenerator();
    }
    /***
     * 下划线命名转为驼峰命名
     *
     * @param para
     *        下划线命名的字符串
     */

    public static String UnderlineToHump(String para){
        StringBuilder result=new StringBuilder();
        String a[]=para.split("_");
        for(String s:a){
            if (!para.contains("_")) {
                result.append(s);
                continue;
            }
            if(result.length()==0){
                result.append(s.toLowerCase());
            }else{
                result.append(s.substring(0, 1).toUpperCase());
                result.append(s.substring(1).toLowerCase());
            }
        }
        return result.toString();
    }



    /***
     * 驼峰命名转为下划线命名
     *
     * @param para
     *        驼峰命名的字符串
     */

    public static String HumpToUnderline(String para){
        StringBuilder sb=new StringBuilder(para);
        int temp=0;//定位
        if (!para.contains("_")) {
            for(int i=0;i

在上面配置好基本配置,运行一下main方法就能在相应包下生成数据库中相应表对应的实体类

下面贴一个入库的代码实例:

public void provinceCityTask() {
          
          System.out.println("==================同步省份城市信息数据==================");
          
          JSONObject params = new JSONObject();
          params.put("area_type","province");
          params.put("area_codes",new JSONArray());
          params.put("linkage", 1);
          
          String resultJson = HttpUtil.post(params, regionProvinceCityApiUrl);
                 // "{\"provinces\":[{\"province_code\":\"20000\",\"province_name\":\"广东省\",\"citys\":[{\"city_code\":\"30000\",\"city_name\":\"广州市\"}]}]}";
          
          if(resultJson != null){
          JSONObject resultObj = JSONObject.parseObject(resultJson);
          JSONArray jsonArr = resultObj.getJSONArray("provinces");
          System.out.println("获取省份城市数据条数:"+jsonArr.size());
          
          List> regionCityList = new ArrayList>();
          Map regionCityMap = null;
          for(int i = 0,length = jsonArr.size(); i < length; i++){
          JSONObject jsonObj = jsonArr.getJSONObject(i);
          JSONArray citiesArr = jsonObj.getJSONArray("citys");
          
          for(int j = 0; j < citiesArr.size(); j++) {
          JSONObject citiesObj = citiesArr.getJSONObject(j);
          regionCityMap = new HashMap();
          regionCityMap.put("provinceCode",jsonObj.get("province_code"));
          regionCityMap.put("provinceName",jsonObj.get("province_name"));
          regionCityMap.put("cityCode", citiesObj.get("city_code"));
          regionCityMap.put("cityName", citiesObj.get("city_name"));
          regionCityList.add(regionCityMap);
          }
          }
          
          int num = synDataMapper.batchInsertProvinceCityData(regionCityList);
          System.out.println("新增省份城市:"+num+"条");
          
          }else{
          System.out.println("获取省份城市数据返回null!");
          System.out.println("params="+JSON.toJSONString(params));
          System.out.println("monthKpiUrl="+monthKpiUrl);
          }
          System.out.println("==================结束同步省份城市数据==================");
          
          }

xml:


      begin
        delete from DM_PROVINCE_CITY_ORI;
        INSERT ALL
        
            into DM_PROVINCE_CITY_ORI
            (
            
                
                    province_code,
                
                
                    province_name,
                
                
                    city_code,
                
                
                    city_name,
                
            
            )
            values
            (
            
                
                    #{item.provinceCode},
                
                
                    #{item.provinceName},
                
                
                    #{item.cityCode},
                
                
                    #{item.cityName},
                
            
            )
        
        SELECT 1 FROM DUAL;
     end;
    

你可能感兴趣的:(java通过httpClient从接口请求数据入库以及自动生成实体工具类)