远程调用RestTemplate

本文介绍了一个使用Java的RestTemplate进行远程调用的例子。该示例展示了如何构造请求体、设置HTTP头以及发送POST请求到指定URL,并解析响应。

远程调用RestTemplate

 

private void sendMeessageToMC(JSONObject objs) throws Exception{
        Map<String ,Object> map=new HashMap<String,Object>();
        Map<String,String> contentMap = new HashMap<String,String>();
        
        contentMap.put("orderId", objs.getString("procBizId"));
        contentMap.put("city", objs.getString("cityName"));
        contentMap.put("communityName", objs.getString("communityName"));
        //put内容
        map.put("contents", contentMap);
        map.put("templateName", "inquiryTemplate");
        map.put("delayTime", "");
        map.put("users", "");
        map.put("roles", "");
        map.put("contacts", "");
        map.put("orgCode", "");
        CommonSendClient.sendHttp(map, kehujiaConfig.getSendQueueUrl());
    }

 

    @SuppressWarnings({ "rawtypes", "unused" })
    public static LinkedHashMap sendHttp(Map body, String url) throws Exception {
        CloseableHttpClient httpClient = CommonSendClient
                .acceptsUntrustedCertsHttpClient();
        HttpComponentsClientHttpRequestFactory clientHttpRequestFactory = new HttpComponentsClientHttpRequestFactory(
                httpClient);
        // String workFlowUrl =
        // "https://asset-test.kehujiabj.com:8843/message.mq.front/springtest/test3";
        RestTemplate restTemplate = new RestTemplate(clientHttpRequestFactory);
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.set("Accept-Charset", "UTF-8");
        Object data = JSON.toJSON(body);
        HttpEntity entity = new HttpEntity<Object>(data, headers);
        ResponseEntity<Map> response = restTemplate.exchange(url,
                HttpMethod.POST, entity, Map.class);
        LinkedHashMap responseMap = (LinkedHashMap) response.getBody();
        return responseMap;
    }
 

 

转载于:https://my.oschina.net/u/3229047/blog/856471

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值