这里接口用表存起来,标记请求方式,然后接受参数,消息或者请求参数都可以,
然后先是遍历需要调用的接口,封装参数,再分别调用get与post即可,没有微服务还是得自己写
//消息转发-获取参数中对应参数调用对应接口 public void pmsForward(Mapmap){ List address = forwardAddressHelper.getAddress();//从内存获取转发地址 //封装参数 List params = new ArrayList (); Iterator > iterator = map.entrySet().iterator(); while(iterator.hasNext()){ Map.Entry next = iterator.next(); params.add(new BasicNameValuePair(next.getKey(),next.getValue())); } address.forEach(x->{ if(StringUtil.isNotEmpty(x.getType())){ if(StringUtil.equals(x.getType(),"GET")){ //get请求 pmsForwardService.getInvoking(x.getAddress(),params); }else if(StringUtil.equals(x.getType(),"POST")){ //post请求 pmsForwardService.postInvoking(x.getAddress(),params); } }else{ log.debug("所请求地址请求方式为空:"+x.getId()+","+x.getAddress()); } }); }
,然后是get调用与post调用:
public void getInvoking(String address, Listparams) { log.debug("进入get接口调用,参数:"+params+",地址:"+address); CloseableHttpResponse response = null; CloseableHttpClient httpClient = HttpClients.createDefault(); try { URIBuilder builder = new URIBuilder(address); builder.setParameters(params); HttpGet get = new HttpGet(builder.build()); response = httpClient.execute(get); if(response != null && response.getStatusLine().getStatusCode() == 200){ HttpEntity entity = response.getEntity(); log.info("pms消息转发成功,返回结果"+entityToString(entity)); } } catch (Exception e) { e.printStackTrace();log.error("pms消息转发失败"+response.getEntity()); } finally { try { httpClient.close();if(response != null)response.close(); } catch (IOException e) { e.printStackTrace(); } } } @Override public void postInvoking(String address, List params) { log.debug("进入post接口调用,参数:"+params+",地址:"+address); CloseableHttpClient httpClient = HttpClients.createDefault(); HttpPost post = new HttpPost(address); CloseableHttpResponse response = null; try { post.setEntity(new UrlEncodedFormEntity(params,"UTF-8")); response = httpClient.execute(post); if(response != null && response.getStatusLine().getStatusCode() == 200){ HttpEntity entity = response.getEntity(); log.info("pms消息转发成功,返回结果"+entityToString(entity)); } } catch (Exception e) { e.printStackTrace();log.error("pms消息转发失败"+response.getEntity()); } finally { try { httpClient.close(); if(response != null) { response.close(); } } catch (IOException e) { e.printStackTrace(); } } }
,最后是将response转换成String的方法:
//解析httpclient返回结果,转换成String private String entityToString(HttpEntity entity) throws IOException { String result = null; if(entity != null) { long lenth = entity.getContentLength(); if(lenth != -1 && lenth < 2048){ result = EntityUtils.toString(entity,"UTF-8"); }else { InputStreamReader reader1 = new InputStreamReader(entity.getContent(), "UTF-8"); CharArrayBuffer buffer = new CharArrayBuffer(2048); char[] tmp = new char[1024]; int l; while((l = reader1.read(tmp)) != -1) { buffer.append(tmp, 0, l); } result = buffer.toString(); } } return result; }
可以debug查看结果了!
还有就是直接使用JSON封装参数:
POST: 只需要设置一下请求头就行了
CloseableHttpClient httpClient = HttpClients.createDefault(); HttpPost post = new HttpPost(address); post.setHeader("Content-Type", "application/json;charset=UTF-8"); String parameter = GSON.toJson(params); StringEntity se = new StringEntity(parameter); se.setContentType(CONTENT_TYPE_TEXT_JSON); post.setEntity(se); CloseableHttpResponse response = null; try { response = httpClient.execute(post); if(response != null && response.getStatusLine().getStatusCode() == 200){ HttpEntity entity = response.getEntity(); log.info("消息转发成功,返回结果"+entityToString(entity)); } } catch (Exception e) { e.printStackTrace();log.error("消息转发失败"+response.getEntity()); } finally { try { httpClient.close(); if(response != null) { response.close(); } } catch (IOException e) { e.printStackTrace(); } }