package com.ekingwin.bas.cloud.mobileErp.utils;

import java.util.HashMap;

import java.util.Map;

import org.springframework.http.HttpEntity;

import org.springframework.http.HttpHeaders;

import org.springframework.http.HttpMethod;

import org.springframework.http.MediaType;

import org.springframework.http.ResponseEntity;

import org.springframework.web.client.RestTemplate;

import com.alibaba.fastjson.JSON;

import com.alibaba.fastjson.JSONObject;

/**

* 向esb发送post请求工具类

* @author yang

*

*/

public class HttpRestUtil {

public static JSONObject  sendPost(Map map1,Map map2,String url) {

//设置头信息

HttpHeaders headers = new HttpHeaders();

headers.setContentType(MediaType.APPLICATION_JSON);

headers.set("Content-Type", "application/json;charset=UTF-8");

headers.set("Accept", "application/json");

//封装请求参数

Map map=new HashMap();

map.put("commonHeader", map1);

map.put("LIST", map2);

String mapJsonStr = JSON.toJSONString(map,true);

JSONObject mapJson =  JSONObject.parseObject((String) JSON.toJSON(mapJsonStr));

System.out.println("请求报文--------->>>>>>"+mapJson.toString());

//发送请求

HttpEntity> he = new HttpEntity>(map, headers);

RestTemplate restTemplate = new RestTemplate();

JSONObject responseJson=new JSONObject();

try {

ResponseEntity result = restTemplate.exchange(url,

HttpMethod.POST, he, JSONObject.class);

JSONObject body = result.getBody();

Object object = body.get("LIST");

JSONObject jsonObject=(JSONObject)JSON.toJSON(object);

responseJson= (JSONObject)JSON.toJSON(jsonObject.get("o"));

/*if(url.contains("Pipeline/querySaleTotalBySaler_PS")) {

responseJson= (JSONObject)JSON.toJSON(jsonObject.get("o"));

}else {

responseJson= (JSONObject)JSON.toJSON(jsonObject.get("DATA"));

}*/

System.out.println("响应报文***********************" + body.toString());

String code =responseJson.getString("code");

if ("1".equals(code)) {

System.out.println("接口调用成功" + responseJson.toString());

} else {

System.out.println("数据操作异常" + result.toString());

}

} catch (Exception e) {

e.printStackTrace();

System.out.println("接口调用失败"+e);

}

return responseJson;

}

/**

* CONSUMER这个参数还没有赋值

* @param BIZTRANSACTIONID 接口系统唯一标识

* @return

*/

public static Map setCommonHeaderMap(String BIZTRANSACTIONID) {

Map map =new HashMap();

map.put("BIZTRANSACTIONID", BIZTRANSACTIONID);

map.put("COUNT", "1");

map.put("SRVLEVEL", "");

map.put("ACCOUNT", "");

map.put("PASSWORD", "");

map.put("CONSUMER", "");

return map;

}

}

分享到:

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐