http接口的调用于被调用的简单实现

本文介绍了一个用于批量发送短信的RESTful API接口实现,并展示了如何通过HTTP客户端调用远程服务更新机器人设置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

接口别人来调用
@RequestMapping(value="/sendMsg", method= RequestMethod.POST, consumes = CONSUMES)
public void sendMsg(@RequestBody Map<String,String> form,HttpServletResponse response){
  JSONObject json = new JSONObject();
  String phones = form.get("phone");
  String content = form.get("content");
   Msg msg = new Msg();
   String[] phoneArr = phones.split(",");
   for(int i=0;i<phoneArr.length;i++){
      String phone = phoneArr[i];
      msg.setContent(content);
      msg.setSendTime(new Date());
      msg.setPhone(phone);
      int num = msgServcie.insertMsg(msg);   
   }
      json.put("resultCode",CODE_003);
      json.put("msg",CODE_000_MSG);
      outputJson(response,json);
      return;
   
  
}
private void outputJson(HttpServletResponse response, JSONObject json) {
  response.setCharacterEncoding("UTF-8");
  response.setContentType("text/javascript;charset=UTF-8");
  response.setHeader("Cache-Control", "no-store, max-age=0, no-cache, must-revalidate");
  response.addHeader("Cache-Control", "post-check=0, pre-check=0");
  response.setHeader("Pragma", "no-cache");
  try {
    PrintWriter out = response.getWriter();
    try {
      out.write(json.toString());
    } finally {
      out.close();
    }
  } catch (IOException e) {
    e.printStackTrace();
  }
}
//调用别人接口
    
    @RequestMapping("/updateInterface.do")
    @ResponseBody
    public void updateInterface(HttpServletResponse response, HttpSession httpSession, String dialogueSign,String termSign) {
         CloseableHttpClient client = HttpClients.createDefault();
         String sn = "A10164900046";
         String account = httpSession.getAttribute("account").toString();
         HttpPost httpPost = new HttpPost(UtilConstants.ROBOT_INTERFACE_URL);
         List<NameValuePair> formparams = new ArrayList<NameValuePair>();
         formparams.add(new BasicNameValuePair("account", account));
         formparams.add(new BasicNameValuePair("sn", sn));
         UrlEncodedFormEntity uefEntity = null;
         try {
             uefEntity = new UrlEncodedFormEntity(formparams);
         } catch (UnsupportedEncodingException e) {
             e.printStackTrace();
         }
         httpPost.setEntity(uefEntity);
         CloseableHttpResponse closeresponse = null;
         try {
             closeresponse = client.execute(httpPost);
         } catch (IOException e) {
             e.printStackTrace();
         }
         HttpEntity entity = closeresponse.getEntity();
         String result = null;
         try {
             result = EntityUtils.toString(entity, "UTF-8");
         } catch (org.apache.http.ParseException | IOException e) {
             e.printStackTrace();
         }
         Map<String, String> rsMap = new HashMap<String, String>();
         Map map = JSON.parseObject(result);
         String rs = (String) map.get("result");
         String ms = (String) map.get("msg");
         if (rs.equals("0")) {
             RobotUpdate robotUp = new RobotUpdate();
             robotUp.setUser(account);
             robotUp.setSn(sn);
             if (dialogueSign!=null && dialogueSign.equals("1")) {
                 robotUp.setDialogueSign("0");
             }
             if (termSign!=null && termSign.equals("1")) {
                 robotUp.setTermSign("0");
             }
             robotUpdateService.updateRobot(robotUp);
             rsMap.put("code", "0");
         } else {
             if (ms.equals("get parameter error,no user or sn")) {
                 rsMap.put("msg", "-1");
             }
             if (ms.equals("Open mysql error!")) {
                 rsMap.put("msg", "-2");
             }
             if (ms.equals("Get user version error!")) {
                 rsMap.put("msg", "-3");
             }
             if (ms.equals("Reade or write questions and answers to sqlite error!")) {
                 rsMap.put("msg", "-4");
             }
             if (ms.equals("Read or write user professional vocabulary error!")) {
                 rsMap.put("msg", "-5");
             }
             if (ms.equals("ead or write config message error!")) {
                 rsMap.put("msg", " -6");
             }
             if (ms.equals("Database encrypt error!")) {
                 rsMap.put("msg", "-7");
             }
             if (ms.equals("Patch error!")) {
                 rsMap.put("msg", "-8");
             }
             if (ms.equals("Write patch message error!")) {
                 rsMap.put("msg", "-8");
             }
         }
         JSONObject jsonObject = JSONObject.fromObject(rsMap);
         try {
             response.getWriter().print(jsonObject.toString());
         } catch (IOException e) {
             e.printStackTrace();
         }
    }
}
maven 依赖
<dependency>
             <groupId>org.apache.httpcomponents</groupId>
             <artifactId>httpclient</artifactId>
             <version>4.5</version>
         </dependency>
         <dependency>
             <groupId>org.apache.httpcomponents</groupId>
             <artifactId>httpcore</artifactId>
             <version>4.4.1</version>
         </dependency>
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值