• 通过RestTemplate类实现跨平台请求


    //向对应业务平台发起消息通知
            try {
                TBpMonitorAnchor monitorAnchor = monitorAnchorService.selectByPrimaryKey(anchorId);
                if (monitorAnchor != null) {
                    TBpBusinessPlatformInfo businessPlatformInfo = businessPlatformInfoService.selectByPrimaryKey(monitorAnchor.getPlatformId());
    //获取到回调地址 String callbackUrl
    = businessPlatformInfo.getCallbackUrl(); RestTemplate restTemplate = new RestTemplate(); //封装请求参数 Map<String, Object> requestBody = new HashMap<String, Object>(16); Map<String, Object> map = new HashMap<>(16); map.put("anchorArchivesCode", monitorAnchor.getAnchorArchivesCode()); map.put("anchorStatus", "-1");
    //将参数转化为字节类型(注意了类型为UTF-8)
    byte[] bytes = JSON.toJSONString(map).getBytes(StandardCharsets.UTF_8); //通过Base64对参数进行编码,对应的获取到参数是也要对其使用Base64解码,类型为编码类型(UTF-8) requestBody.put("data", Base64.getEncoder().encodeToString(bytes)); requestBody.put("type", "monitorAnchorStatus"); HttpHeaders requestHeaders = new HttpHeaders();
    //这三个一般为固定写法 requestHeaders.setContentType(MediaType.APPLICATION_JSON); requestHeaders.set(HttpHeaders.ACCEPT_CHARSET, StandardCharsets.UTF_8.toString()); requestHeaders.set(HttpHeaders.ACCEPT_ENCODING, StandardCharsets.UTF_8.toString()); HttpEntity
    <Map<String, Object>> httpEntity = new HttpEntity<>(requestBody, requestHeaders); //发送post请求 restTemplate.postForObject(callbackUrl, httpEntity, HttpDataResult.class); } } catch (Exception e) { log.error("发送消息拆除通知失败!"); e.printStackTrace(); }
  • 相关阅读:
    Node
    HTTP权威指南
    一些乱七八糟的思考积累
    有限状态机
    奇怪问题引用到其它文件夹项目文件方法
    dijkstra+堆优化
    高精度
    BUAA 1301 最短路
    阿里云服务器ECS(Centos8)下安装和配置python3.8
    Cheat Sheet
  • 原文地址:https://www.cnblogs.com/panbingqi/p/14074051.html
Copyright © 2020-2023  润新知