From 4e05734fb00accfaded060f022629379ca0d43c9 Mon Sep 17 00:00:00 2001
From: java110 <928255095@qq.com>
Date: 星期日, 22 三月 2020 11:48:37 +0800
Subject: [PATCH] 加入front 加入 api 调用方式
---
FrontService/src/main/java/com/java110/front/controller/CallComponentController.java | 62 ++++++++++++++++++++++++++++++
1 files changed, 61 insertions(+), 1 deletions(-)
diff --git a/FrontService/src/main/java/com/java110/front/controller/CallComponentController.java b/FrontService/src/main/java/com/java110/front/controller/CallComponentController.java
index ffac30b..1a43645 100644
--- a/FrontService/src/main/java/com/java110/front/controller/CallComponentController.java
+++ b/FrontService/src/main/java/com/java110/front/controller/CallComponentController.java
@@ -35,6 +35,66 @@
@Autowired
private RestTemplate restTemplate;
+
+ /**
+ * 鍓嶅彴璋冪敤api鏂规硶
+ * add by wuxw 2020-03-16
+ *
+ * @return
+ */
+
+ @RequestMapping(path = "/callComponent/{api}")
+ public ResponseEntity<String> callApi(
+ @PathVariable String api,
+ //@RequestBody String info,
+ HttpServletRequest request) {
+ ResponseEntity<String> responseEntity = null;
+ String componentCode = "api";
+ String componentMethod = "callApi";
+ try {
+ Assert.hasLength(api, "鍙傛暟閿欒锛屾湭浼犲叆api缂栫爜");
+
+ IPageData pd = (IPageData) request.getAttribute(CommonConstant.CONTEXT_PAGE_DATA);
+ pd.setApiUrl("/api/" + api);
+ //鏉冮檺鏍¢獙
+ hasPrivilege(restTemplate, pd, "/" + api);
+
+ Object componentInstance = ApplicationContextFactory.getBean(componentCode);
+
+ Assert.notNull(componentInstance, "鏈壘鍒扮粍浠跺搴旂殑澶勭悊绫伙紝璇风‘璁� " + componentCode);
+
+ Method cMethod = componentInstance.getClass().getDeclaredMethod(componentMethod, IPageData.class);
+
+ Assert.notNull(cMethod, "鏈壘鍒扮粍浠跺搴斿鐞嗙被鐨勬柟娉曪紝璇风‘璁� " + componentCode + "鏂规硶锛�" + componentMethod);
+
+
+ logger.debug("缁勪欢缂栫爜{}锛岀粍浠舵柟娉晎}锛宲d 涓簕}", componentCode, componentMethod, pd.toString());
+
+ responseEntity = (ResponseEntity<String>) cMethod.invoke(componentInstance, pd);
+
+ } catch (SMOException e) {
+ /*MultiValueMap<String, String> headers = new HttpHeaders();
+ headers.add("code", e.getResult().getCode());*/
+ logger.error("璋冪敤api寮傚父", e);
+ responseEntity = new ResponseEntity<>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
+ } catch (Exception e) {
+ logger.error("璋冪敤api寮傚父", e);
+ String msg = "";
+ if (e instanceof InvocationTargetException) {
+ Throwable targetEx = ((InvocationTargetException) e).getTargetException();
+ if (targetEx != null) {
+ msg = targetEx.getMessage();
+ }
+ } else {
+ msg = e.getMessage();
+ }
+ responseEntity = new ResponseEntity<>(msg, HttpStatus.INTERNAL_SERVER_ERROR);
+ } finally {
+ logger.debug("api璋冪敤杩斿洖淇℃伅涓簕}", responseEntity);
+ return responseEntity;
+ }
+ }
+
/**
* 璋冪敤缁勪欢鏂规硶
* add by wuxw 2020-03-16
@@ -236,7 +296,7 @@
reqData = paramObj.toJSONString();
}
- IPageData newPd = PageData.newInstance().builder(pd.getUserId(),pd.getUserName(), pd.getToken(),
+ IPageData newPd = PageData.newInstance().builder(pd.getUserId(), pd.getUserName(), pd.getToken(),
reqData, pd.getComponentCode(), pd.getComponentMethod(), "", pd.getSessionId(), "");
return newPd;
}
--
Gitblit v1.8.0