ssm远程调用接口小笔记

在项目里只写controller和service,接口调用别人的大概流程,做个小笔记,controller层调service,service调接口
在service层开始吧

 Map<String, String> paramMap = new HashMap<String, String>();
        paramMap.put("key", value);
        //把map转成json字符串
        String jsonObject = JSONObject.toJSONString(paramMap);
        //获取接口URL
        String url = SystemConfig.getinterface();
        //远程接口调用
        PerfectApplyResult vo = callPcUrl(url, jsonObject);

systemconfig里面是

static Properties sysConfigProp = new Properties();
static {
		String sysConfigPath = "sysconfig.properties";
		try {		sysConfigProp.load(SystemConfig.class.getClassLoader().getResourceAsStream(sysConfigPath));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
public static String getinterface() {
		return  (String) getProperty("getinterface")  ;
	}
public static Object getProperty(String key) {
		if (sysConfigProp != null && sysConfigProp.get(key) != null) {
			return sysConfigProp.get(key);
		}
		return null;
	}

下面是sysconfig.properties文件

getinterface=xxxxxxxxxx即远程接口的路径

远程接口调用的方法

private Result callPcUrl(String url, String inputParam) {
      //Result是结果统一返回对象
        String resultStr = "";
        Result vo = new Result();
        try {
            vo.setRequestUrl(url);
            vo.setRequestBody(inputParam);
            ;
            JSONObject jsonResult = httpsRequestForBack(url, inputParam);
           vo.setSuccess(jsonResult.getString("success"));
           vo.setMessage(jsonResult.getString("message"));
            vo.setData(jsonResult.getString("data"));
        } catch (Exception e) {
            logger.error("接口调用失败,参数:{}", inputParam, e);
            vo.setNetStatus(false);
        }

        return vo;
    }

httpsRequestForBack方法

/*
	 * @description  远程调用https请求并返回结果
	 * @author cjh
	 * @date 2022/3/1
	 * @param
	 * @return JSONObject
	 */
	public static	JSONObject httpsRequestForBack(String url,String outputStr) {
		JSONObject jsonObject=new JSONObject();
		try {
			SSLContext sslContext = SSLContextBuilder.create().useProtocol(SSLConnectionSocketFactory.SSL).loadTrustMaterial((x, y) -> true).build();
			CloseableHttpClient httpClient = HttpClientBuilder.create().setSSLContext(sslContext).setSSLHostnameVerifier((x, y) -> true).build();
			HttpPost httpPost = new HttpPost(url);

			// 设置请求的header,数据传输的格式
			httpPost.addHeader("Content-Type", "application/json;charset=utf-8");

			// 设置请求的参数
			StringEntity entity = new StringEntity(outputStr, "utf-8");
			entity.setContentEncoding("UTF-8");
			entity.setContentType("application/json");
			httpPost.setEntity(entity);

			// 执行请求
			HttpResponse response = httpClient.execute(httpPost);
			String json2 = EntityUtils.toString(response.getEntity(), "utf-8");
			jsonObject = JSONObject.parseObject(json2);
		}catch (Exception e){
			logger.error("发送HTTP请求失败:requestUrl:{},outputStr:{}",url,outputStr,e);
		}
		// 打印执行结果
		System.out.println(jsonObject);
		return jsonObject;
	}

OK!

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值