SpringBoot请求网页接口数据返回JSON,并存入本地数据库

  1. pom文件
    <dependency>
       <groupId>org.json</groupId>
       <artifactId>json</artifactId>
       <version>20160810</version>
    </dependency>
    
    <!--mysql 依赖-->
    <dependency>
       <groupId>mysql</groupId>
       <artifactId>mysql-connector-java</artifactId>
    
    </dependency>

     

  2. application.properties文件
    #本机调试
    spring.datasource.url = jdbc:mysql://localhost:3306/baidudata?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC
    spring.datasource.username = root
    spring.datasource.password = root
    spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
    #端口号
    server.port=18082
    
    #指明mybatis实体类包位置
    mybatis.type-aliases-package= bababi.springboot.demo.model
    #指明mappers位置,可以指明多个位置
    mybatis.mapper-locations=classpath:map/*.xml
    mybatis.configuration.call-setters-on-nulls=true

     

    1. GetJson文件
      public class GetJson {
          public JSONObject getHttpJson(String url, int comefrom) throws Exception {
              try {
                  URL realUrl = new URL(url);
                  HttpURLConnection connection = (HttpURLConnection) realUrl.openConnection();
                  connection.setRequestProperty("accept", "*/*");
                  connection.setRequestProperty("connection", "Keep-Alive");
                  connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
                  // 建立实际的连接
                  connection.connect();
                  //请求成功
                  if (connection.getResponseCode() == 200) {
                      InputStream is = connection.getInputStream();
                      ByteArrayOutputStream baos = new ByteArrayOutputStream();
                      //10MB的缓存
                      byte[] buffer = new byte[10485760];
                      int len = 0;
                      while ((len = is.read(buffer)) != -1) {
                          baos.write(buffer, 0, len);
                      }
                      String jsonString = baos.toString();
                      baos.close();
                      is.close();
                      //转换成json数据处理
                      // getHttpJson函数的后面的参数1,表示返回的是json数据,2表示http接口的数据在一个()中的数据
                      JSONObject jsonArray = getJsonString(jsonString, comefrom);
                      return jsonArray;
                  }
              } catch (MalformedURLException e) {
                  e.printStackTrace();
              } catch (IOException ex) {
                  ex.printStackTrace();
              }
              return null;
          }
      
          public JSONObject getJsonString(String str, int comefrom) throws Exception{
              JSONObject jo = null;
              if(comefrom==1){
                  return new JSONObject(str);
              }else if(comefrom==2){
                  int indexStart = 0;
                  //字符处理
                  for(int i=0;i<str.length();i++){
                      if(str.charAt(i)=='('){
                          indexStart = i;
                          break;
                      }
                  }
                  String strNew = "";
                  //分割字符串
                  for(int i=indexStart+1;i<str.length()-1;i++){
                      strNew += str.charAt(i);
                  }
                  return new JSONObject(strNew);
              }
              return jo;
          }
      
      }

      4.测试类

      @RunWith(SpringRunner.class)
      @SpringBootTest
      public class DemoApplicationTests {
         @Autowired
         private JokeMapper jokeMapper;
         @Test
         public void contextLoads() {
            String address="这里写你要请求的接口路径如www.baidu.com/getdata";
      
            try {
               JSONObject dayLine = new GetJson().getHttpJson(address,1);
               System.out.println(dayLine);
               //取得data的json数据
               JSONArray json= null;
               json = dayLine.getJSONArray("joke");
               List<Joke> list= JSON.parseArray(json.toString(),Joke.class);
               System.out.println(list);
               for (Joke btcoinEntity:list){  //遍历list
                  jokeMapper.insert(btcoinEntity);  //插入数据库
               }
            } catch (Exception e) {
               e.printStackTrace();
            }
         }

      5.这里还有dao层接口JokeMapper、以及JokeMapper.xml、Joke(实体类,需要跟你爬取数据字一一对应)没写,可以通过逆向工程生成,最后运行test 数据成功插入数据库。                ------------参考文章https://blog.csdn.net/rui15111/article/details/80974172

转载于:https://my.oschina.net/u/3836038/blog/2876872

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值