1、传入路径、方法、参数、编码集
public static SendGateWayResult sendURL(String urlParam, String method, Map<String, Object> params, String charset) {
StringBuilder resultBuffer = new StringBuilder();
HttpClient client = HttpClientBuilder.create().build();
SendGateWayResult sendGateWayResult=new SendGateWayResult();
BufferedReader br = null;
try {
HttpWithBody httpWthBody = new HttpWithBody(urlParam, method);
// 构建请求参数
JSONObject jsonObject = new JSONObject();
if (params != null && params.size() > 0) {
for (Map.Entry<String, Object> entry : params.entrySet()) {
jsonObject.put(entry.getKey(), entry.getValue());
}
}
StringEntity formEntity = new StringEntity(jsonObject.toString(),charset);
formEntity.setContentType(“application/json”);
httpWthBody.setEntity(formEntity);
logger.info(“请求参数为:”+jsonObject.toString()+“请求地址:”+urlParam+",方法类型:"+method);
HttpResponse response = client.execute(httpWthBody);
int responseStatus = response.getStatusLine().getStatusCode();
logger.info(“sendURL responseStatus:”+responseStatus);
if(response!=null) {
HttpEntity entity = response.getEntity();
if(entity!=null) {
br = new BufferedReader(new InputStreamReader(entity.getContent()));
}
String temp;
if(br!=null) {
while ((temp = br.readLine()) != null) {
resultBuffer.append(temp);
}
}
if (responseStatus == 200||responseStatus == 201||responseStatus == 204) {
//组装返回数据
sendGateWayResult.setResultCode(responseStatus+"");
sendGateWayResult.setResultError("");
sendGateWayResult.setResultMessage(resultBuffer.toString());
sendGateWayResult.setSuccess(true);
}else {
sendGateWayResult.setResultCode(responseStatus+"");
sendGateWayResult.setResultError(resultBuffer.toString());
sendGateWayResult.setResultMessage(resultBuffer.toString());
sendGateWayResult.setSuccess(false);
}
}
} catch (Exception e) {
logger.error(“请求kong网关失败!”, e);
sendGateWayResult.setResultCode(“500”);
sendGateWayResult.setSuccess(false);
sendGateWayResult.setResultError(“请求kong网关失败!:”+e);
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
br = null;
logger.error(“关闭流失败!”, e);
}
}
}
return sendGateWayResult;
}
2、HttpWithBody 类的定义
public class HttpWithBody extends HttpEntityEnclosingRequestBase {
private String METHOD_NAME = “GET”;
public String getMethod() { return METHOD_NAME; }
public HttpWithBody(final String uri) {
super();
setURI(URI.create(uri));
}
public HttpWithBody(final URI uri) {
super();
setURI(uri);
}
public HttpWithBody() { super(); }
//
public HttpWithBody(final String uri, String method) {
super();
setURI(URI.create(uri));
setMethod(method);
}
public void setMethod(String method)
{
this.METHOD_NAME = method;
}
}
3、入口
KongServiceResult kongServiceResult=new KongServiceResult();
logger.info(“新增消费者信息传入参数为:”+consumerInfo);
String urlParam=kongGateWayAddress+“consumers/”;
//数据封装
try {
Map<String, Object> mapParams = TaskUtils.transformBeanToMap(consumerInfo);
kongServiceResult=TaskUtils.dealKongGateMethod(urlParam, mapParams, RestMethodEnum.POST+"", ConsumerInfo.class);
} catch (IllegalAccessException e) {
logger.error(“将javabean转换成map对象失败!”, e);
kongServiceResult.setReturnCode(“500”);
kongServiceResult.setSuccess(false);
kongServiceResult.setErrorMessage(“将javabean转换成map对象失败!”);
}
return kongServiceResult;
4、dealKongGateMethod方法的封装
public static KongServiceResult dealKongGateMethod( String urlParam,
Map<String, Object> mapParams,String restMethod,Class z) {
KongServiceResult kongServiceResult =new KongServiceResult();
//调用公共接口发送请求到kong网关
SendGateWayResult gateWayResult = TaskUtils.sendURL(urlParam,restMethod, mapParams, “UTF-8”);
logger.info(“请求路由信息返回参数:”+gateWayResult.toString());
//解析对象返回对象
if(gateWayResult.isSuccess()) {
Object parseObject=JSON.parseObject(gateWayResult.getResultMessage(), z);
if (parseObject != null) {
if (parseObject instanceof ArrayList) {
kongServiceResult.setObjects((List) parseObject);
} else {
kongServiceResult.setObject((T) parseObject);
}
}
}
//封装结果集返回
kongServiceResult.setReturnCode(gateWayResult.getResultCode());
kongServiceResult.setSuccess(gateWayResult.isSuccess());
kongServiceResult.setErrorMessage(gateWayResult.getResultError());
return kongServiceResult;
5、transformBeanToMap的封装
public static Map<String, Object> transformBeanToMap(Object object)
throws IllegalArgumentException, IllegalAccessException {
Map<String, Object> map = new HashMap<String, Object>();
if (object != null) {
Field[] declaredFields = object.getClass().getDeclaredFields();
for (Field field : declaredFields) {
field.setAccessible(true);
Object value = field.get(object);
if(value==null) {
continue;
}
map.put(field.getName(), value);
}
}
return map;