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<String, Object> map1,Map<String, Object> 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<String, Object> map=new HashMap<String, Object>();
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<Map<String, Object>> he = new HttpEntity<Map<String, Object>>(map, headers);
RestTemplate restTemplate = new RestTemplate();
JSONObject responseJson=new JSONObject();
try {
ResponseEntity<JSONObject> 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<String, Object> setCommonHeaderMap(String BIZTRANSACTIONID) {
Map<String, Object> map =new HashMap<String, Object>();
map.put(“BIZTRANSACTIONID”, BIZTRANSACTIONID);
map.put(“COUNT”, “1”);
map.put(“SRVLEVEL”, “”);
map.put(“ACCOUNT”, “”);
map.put(“PASSWORD”, “”);
map.put(“CONSUMER”, “”);
return map;
}
}
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/17464.html