http接口的创建与调用

直接上代码我用的框架是ssm

@RequestMapping(path="xxx",consumes="application/json",method=RequestMethod.POST)
@ResponseBody
public JSONObject updateBorrowStateToLark(@RequestBody String param) throws Exception
{
	JSONObject outerJson = new JSONObject();
	try {
		logger.info("输出参数:"+param.toString());
		System.out.println("接收的参数:"+param.toString());
		//JSONObject jsonObject = JSONObject.fromObject(param);
		com.alibaba.fastjson.JSONObject jsonObject = JSON.parseObject(param);
		String dataJson=jsonObject.getString("param");
		JSONArray paramArra = JSONArray.fromObject(dataJson);
		for(int x=0;x<paramArra.hashCode();x++) {
			
			String params = jsonObject.getString("param");
			String timestamp = jsonObject.getString("timestamp");
			String sign = jsonObject.getString("sign");
			//获取系统标识
			String key=TMAttributeNames.SYS_CONTRACT;
			logger.info("获取的参数信息:contractno:"+params+"timestamp:"+timestamp+"sign:"+sign);
			//获取参数的MD5值
			String paramMD5=DigestUtils.md5Hex(params+timestamp+key);
			logger.info("算取的MD5值"+paramMD5);
			//校验请求参数
			if(!paramMD5.equals(sign))
			{
				outerJson.put("code", "404");
				outerJson.put("contractname", "");
				outerJson.put("spare", "");
				outerJson.put("oppcompany", "");
			}else {
				//获取返回参数信息
				Object[] obj = workCountServiceDS.getContractno(params);
				if(obj!=null) {
					outerJson.put("code", "200");
					outerJson.put("contractname", obj[0].toString());
					outerJson.put("spare", obj[1].toString());
					outerJson.put("oppcompany", obj[2].toString());
				}else {
					outerJson.put("code", "404");
					outerJson.put("contractname", "is null");
					outerJson.put("spare", "is null");
					outerJson.put("oppcompany", "is null");
				}
				logger.info("输出参数:"+outerJson.toString());
				System.out.println("输出参数:"+outerJson.toString());
			}
		}
		
	} catch (Exception e) 
	{
		e.printStackTrace();
		e.getMessage();
	}		
	return outerJson;
}

上面是支持多参数的目前因为进度问题返回的参数没出处理完成

现在是接口的调用:

 public String getSignForHttp(String param, String id,long timestamp String zz,String xx) throws Exception { 	
	// TODO Auto-generated method stub 
           StringBuffer buffer = new StringBuffer("");
		 try {
		
			 String urlString = urlStr;
		        CloseableHttpClient  httpClient = HttpClientBuilder.create().build();
		        HttpPost httpPost = new HttpPost(urlString);
		        httpPost.addHeader(HTTP.CONTENT_TYPE, "application/json");
		        httpPost.addHeader("appId", appId);
		        httpPost.addHeader("version", "1.1");
		        httpPost.addHeader("timestamp",timestamp+"");
		        httpPost.addHeader("sign", sign);

		        StringEntity entity;
		        entity = new StringEntity(bizParams);
		        log.info("调用接口参数"+bizParams.toString());
		        httpPost.setEntity(entity);
		        HttpResponse response;
		        response = httpClient.execute(httpPost);
		        /*InputStream inputStream = response.getEntity().getContent();
		        String name1 = name+".png";     //图片的名称
		*/        
		      //获取返回
		      	HttpEntity entitys = response.getEntity();
		      	BufferedReader in = new BufferedReader(new InputStreamReader(entitys.getContent(), "UTF-8"));
		      	String line = null;
		      	while ((line = in.readLine()) != null) {
		      	  buffer.append(line);
		      	}
		      	 //读取响应
		        log.info(buffer.toString());
		      
	           
	           
	        } catch (Exception e) {
				// TODO: handle exception
	        	log.info("接口调用异常"+e);
			}

			return buffer.toString();
	}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值