JEPaaS 社区

 找回密码
 立即注册
JEPaaS低代码平台-官网
查看: 3718|回复: 0

使用接口模拟调运其他平台数据并返回

[复制链接]

44

主题

150

帖子

671

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
671
发表于 2022-3-25 13:42:46 | 显示全部楼层 |阅读模式
案例描述
在平台A上利用接口模拟调运平台B中功能数据

实现思路
  • 确定平台B的功能数据正常,获取指定用户token、功能编码、数据ID(只需要一条数据的时候),组成url
  • 在平台A写接口,利用HttpClientUtil工具类的doPost()方法进行解析并获取数据
  • 在平台A上写个测试按钮,调运接口进行测试

操作步骤
1. 确定平台B的url
//定义api,一般情况下这个api的url是第三方提供

  1. String  id = "5qZUoTMjp3LsbO8E4y2";
  2. String apiurl = "http://example.jepaas.com/je/getInfoById"+"?pkValue="+id+"&authorization=6Dy0QDIS7IRuVXzqmYh&tableCode=JE_DEMO_XMINFO";
复制代码
2. 在平台A写接口,利用HttpClientUtil工具类的doPost()方法进行解析并获取数据
3. 在平台A上写个测试按钮,调运接口进行测试
相关代码
1 、HttpClientUtil代码
  1. package com.project.demo.controller;
  2. import java.io.IOException;
  3. import java.net.URI;
  4. import java.util.ArrayList;
  5. import java.util.List;
  6. import java.util.Map;

  7. import org.apache.http.NameValuePair;
  8. import org.apache.http.client.entity.UrlEncodedFormEntity;
  9. import org.apache.http.client.methods.CloseableHttpResponse;
  10. import org.apache.http.client.methods.HttpGet;
  11. import org.apache.http.client.methods.HttpPost;
  12. import org.apache.http.client.utils.URIBuilder;
  13. import org.apache.http.entity.ContentType;
  14. import org.apache.http.entity.StringEntity;
  15. import org.apache.http.impl.client.CloseableHttpClient;
  16. import org.apache.http.impl.client.HttpClients;
  17. import org.apache.http.message.BasicNameValuePair;
  18. import org.apache.http.util.EntityUtils;

  19. public class HttpClientUtilTest {

  20.     /**
  21.      * 带参数的get请求
  22.      * @param url
  23.      * @param param
  24.      * @return String
  25.      */
  26.     public static String doGet(String url, Map<String, String> param) {
  27.         // 创建Httpclient对象
  28.         CloseableHttpClient httpclient = HttpClients.createDefault();

  29.         String resultString = "";
  30.         CloseableHttpResponse response = null;
  31.         try {
  32.             // 创建uri
  33.             URIBuilder builder = new URIBuilder(url);
  34.             if (param != null) {
  35.                 for (String key : param.keySet()) {
  36.                     builder.addParameter(key, param.get(key));
  37.                 }
  38.             }
  39.             URI uri = builder.build();
  40.             // 创建http GET请求
  41.             HttpGet httpGet = new HttpGet(uri);
  42.             // 执行请求
  43.             response = httpclient.execute(httpGet);
  44.             // 判断返回状态是否为200
  45.             if (response.getStatusLine().getStatusCode() == 200) {
  46.                 resultString = EntityUtils.toString(response.getEntity(), "UTF-8");
  47.             }
  48.         } catch (Exception e) {
  49.             e.printStackTrace();
  50.         } finally {
  51.             try {
  52.                 if (response != null) {
  53.                     response.close();
  54.                 }
  55.                 httpclient.close();
  56.             } catch (IOException e) {
  57.                 e.printStackTrace();
  58.             }
  59.         }
  60.         return resultString;
  61.     }

  62.     /**
  63.      * 不带参数的get请求
  64.      * @param url
  65.      * @return String
  66.      */
  67.     public static String doGet(String url) {
  68.         return doGet(url, null);
  69.     }

  70.     /**
  71.      * 带参数的post请求
  72.      * @param url
  73.      * @param param
  74.      * @return String
  75.      */
  76.     public static String doPost(String url, Map<String, String> param) {
  77.         // 创建Httpclient对象
  78.         CloseableHttpClient httpClient = HttpClients.createDefault();
  79.         CloseableHttpResponse response = null;
  80.         String resultString = "";
  81.         try {
  82.             // 创建Http Post请求
  83.             HttpPost httpPost = new HttpPost(url);
  84.             // 创建参数列表
  85.             if (param != null) {
  86.                 List<NameValuePair> paramList = new ArrayList<>();
  87.                 for (String key : param.keySet()) {
  88.                     paramList.add(new BasicNameValuePair(key, param.get(key)));
  89.                 }
  90.                 // 模拟表单
  91.                 UrlEncodedFormEntity entity = new UrlEncodedFormEntity(paramList);
  92.                 httpPost.setEntity(entity);
  93.             }
  94.             // 执行http请求
  95.             response = httpClient.execute(httpPost);
  96.             resultString = EntityUtils.toString(response.getEntity(), "utf-8");
  97.         } catch (Exception e) {
  98.             e.printStackTrace();
  99.         } finally {
  100.             try {
  101.                 response.close();
  102.             } catch (IOException e) {
  103.                 e.printStackTrace();
  104.             }
  105.         }
  106.         return resultString;
  107.     }

  108.     /**
  109.      * 不带参数的post请求
  110.      * @param url
  111.      * @return String
  112.      */
  113.     public static String doPost(String url) {
  114.         return doPost(url, null);
  115.     }

  116.     /**
  117.      * 传送json类型的post请求
  118.      * @param url
  119.      * @param json
  120.      * @return String
  121.      */
  122.     public static String doPostJson(String url, String json) {
  123.         // 创建Httpclient对象
  124.         CloseableHttpClient httpClient = HttpClients.createDefault();
  125.         CloseableHttpResponse response = null;
  126.         String resultString = "";
  127.         try {
  128.             // 创建Http Post请求
  129.             HttpPost httpPost = new HttpPost(url);
  130.             // 创建请求内容
  131.             StringEntity entity = new StringEntity(json, ContentType.APPLICATION_JSON);
  132.             httpPost.setEntity(entity);
  133.             // 执行http请求
  134.             response = httpClient.execute(httpPost);
  135.             resultString = EntityUtils.toString(response.getEntity(), "utf-8");
  136.         } catch (Exception e) {
  137.             e.printStackTrace();
  138.         } finally {
  139.             try {
  140.                 response.close();
  141.             } catch (IOException e) {
  142.                 e.printStackTrace();
  143.             }
  144.         }
  145.         return resultString;
  146.     }
  147. }
复制代码
2、接口代码
  1. package com.project.demo.controller;

  2. import com.je.core.base.MethodArgument;
  3. import com.je.core.result.BaseRespResult;
  4. import org.springframework.stereotype.Controller;
  5. import org.springframework.web.bind.annotation.RequestMapping;
  6. import org.springframework.web.bind.annotation.RequestMethod;
  7. import org.springframework.web.bind.annotation.ResponseBody;


  8. @Controller
  9. @RequestMapping(value = "/je/demo/test")
  10. public class TestDemoController {
  11.     @RequestMapping(value = "/testUrl", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
  12.     @ResponseBody
  13.     public BaseRespResult testUrl(MethodArgument param){
  14.         //定义api,一般情况下这个api的url是第三方提供
  15.         String  id = "5qZUoTMjp3LsbO8E4y2";
  16.         String apiurl = "http://example.jepaas.com/je/getInfoById"+"?pkValue="+id+"&authorization=6Dy0QDIS7IRuVXzqmYh&tableCode=JE_DEMO_XMINFO";
  17.         String result="";
  18.         try {
  19.             result = HttpClientUtilTest.doPost(apiurl);
  20.         } catch (Exception e) {
  21.             e.printStackTrace();
  22.         }
  23.         return  BaseRespResult.successResult(result);
  24.     }
  25. }
复制代码

平台调试
1、在某个功能上写个按钮,进行Ajax调运测试
2、调运代码
  1. function(btn,event,eOpts){
  2. var obj = JE.ajax({
  3.        url:'/je/demo/test/testUrl',
  4.        text:false,//返回字符串,如果不配置,返回对象
  5.        params:{}
  6.     });
  7.     if(obj.success){
  8.         console.log(obj);
  9.         JE.alert(obj.obj);
  10.     }else{
  11.         JE.alert('失败,请联系管理员');
  12.     }
  13. }
复制代码

3、后台断点调试



回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|手机版|JEPaaS 低代码平台社区 ( 京ICP备18023569号 )

GMT+8, 2024-4-20 19:10 , Processed in 0.070424 second(s), 20 queries .

Powered by 北京凯特伟业科技有限公司

Copyright © 2001-2021, JEPaaS.COM

快速回复 返回顶部 返回列表