• 接口对接


    需求:两个项目需要数据交互,项目A 发送json格式数据到 项目B 中,项目B接收数据并反馈json格式到项目A。

    项目A:封装数据,发送请求:

    • 导入jar包 Http有关:

      

    • 编写代码:
                  //请求地址:
                  String url = "http://192.168.10.204:8081/account/coreinterface/company;
                  CloseableHttpClient httpClient = HttpClients.createDefaul t();
                  HttpPost httpPost = new HttpPost(url);
                  //httpPost.setHeader("data", data);
                  List<NameValuePair> params = new ArrayList<NameValuePair>();
         //添加需要发送到A项目的数据:
      params.add(new BasicNameValuePair("commNo", commNo)); params.add(new BasicNameValuePair("billFee", billFee.toString())); params.add(new BasicNameValuePair("insurerCode", insurerCode));
      //posp方式发送请求: httpPost.setEntity(
      new UrlEncodedFormEntity(params, "UTF-8"));
      //接收A项目反馈的数据: CloseableHttpResponse res
      = httpClient.execute(httpPost); String response = EntityUtils.toString(res.getEntity(),"UTF-8"); JSONObject jsStr = JSONObject.fromObject(response); //关闭 httpClient.close(); //接收反馈数据返回执行结果。 System.out.println("返回数据是:"+response); System.out.println("截取后得到结果="+jsStr.get("status")); if("0".equals(jsStr.get("status"))){ System.out.println("录入凭证成功!"); return true; }if("1".equals(jsStr.get("status"))){ System.out.println("录入凭证失败!"); return false; } } catch (IOException e) { e.printStackTrace(); } return false;

    项目B:接收数据,后反馈:

    • 编写代码:(servlet方式)
      protected void service(HttpServletRequest req, HttpServletResponse resp)
                  throws ServletException, IOException {
              String path=req.getRequestURI();
              System.out.println(path);
              CodeSystemServiceImp codeSystemServletImp=new CodeSystemServiceImp();
              //保险公司结算 发送财务
              if("/account/coreinterface/company".equals(path)){
                  System.out.println("保险公司结算单信息");
                  // 解码
                  String commNo="";
                  String billFee="";
                  String insurerCode="";
                  try {
      //解码:以防出现中文乱码: commNo
      =URLDecoder.decode(req.getParameter("commNo"), "UTF-8"); billFee=URLDecoder.decode(req.getParameter("billFee"), "UTF-8"); insurerCode=URLDecoder.decode(req.getParameter("insurerCode"), "UTF-8"); } catch (UnsupportedEncodingException e2) { e2.printStackTrace(); } // 生成当前系统年月六位数: Date udYears = new java.util.Date(); String yearmonth = new SimpleDateFormat("yyyyMM").format(udYears); String voucherNo=""//执行操作: try { codeSystemServletImp.insertvoucher(commNo, billFee,insurerCode); resp.setContentType("application/json;charset=utf-8");//json 的编码 String jsonStr = "{"status":"0","message":"录入凭证成功"}";//封装反馈数据为json字符串。 System.out.println("录入成功!"); resp.getWriter().write(jsonStr); } catch (Exception e) { resp.setContentType("application/json;charset=utf-8");//json 的编码 String jsonStr = "{"status":"1","message":"录入凭证失败"}"; try { resp.getWriter().write(jsonStr); } catch (IOException e1) { System.out.println("反馈数据失败!"); e1.printStackTrace(); } e.printStackTrace(); } } }
  • 相关阅读:
    《Maven实战》知识点记录总结
    Git实战
    IDEA下out目录与target目录的区别详解
    Linux下MySQL(8.0.18)的安装
    ElasticSearch学习笔记尚硅谷
    【安装教程】Python IDLE win10+macOS
    【摄影教程笔记】中传《视听语言》
    PyTorch图文安装教程(Win10),含遇到的问题及解决办法
    golangbitmap
    获取IP地址
  • 原文地址:https://www.cnblogs.com/forever2h/p/6835060.html
Copyright © 2020-2023  润新知