zoukankan      html  css  js  c++  java
  • 接口对接

    需求:两个项目需要数据交互,项目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(); } } }
  • 相关阅读:
    关于ie7下display:inline-block;不支持的解决方案。
    Unicode转义序列
    DOMContentLoaded与load的区别
    有关列分组,定义css样式无效的问题
    多行文字溢出[...]的实现(text-overflow: ellipsis)
    goahead cgi 及出现的问题解决
    Android面试题整理(1)
    回溯法 之 马周游(马跳日)问题
    软件工程总结
    [置顶] 每日震精图
  • 原文地址:https://www.cnblogs.com/forever2h/p/6835060.html
Copyright © 2011-2022 走看看