From 4ff738f377504fe8f2296df18cf7d0123641cdd9 Mon Sep 17 00:00:00 2001
From: wuxw <928255095@qq.com>
Date: 星期四, 17 十月 2019 22:11:03 +0800
Subject: [PATCH] 优化调用微服务时的报错

---
 WebService/src/main/java/com/java110/web/controller/CallComponentController.java |  107 ++++++++++++++++++++++++++++++++++++++++++++++++-----
 1 files changed, 97 insertions(+), 10 deletions(-)

diff --git a/WebService/src/main/java/com/java110/web/controller/CallComponentController.java b/WebService/src/main/java/com/java110/web/controller/CallComponentController.java
index 828dcbb..6e87433 100644
--- a/WebService/src/main/java/com/java110/web/controller/CallComponentController.java
+++ b/WebService/src/main/java/com/java110/web/controller/CallComponentController.java
@@ -1,24 +1,25 @@
 package com.java110.web.controller;
 
-import com.java110.common.constant.CommonConstant;
-import com.java110.common.exception.SMOException;
-import com.java110.common.factory.ApplicationContextFactory;
-import com.java110.common.util.Assert;
+import com.alibaba.fastjson.JSONObject;
+import com.java110.utils.constant.CommonConstant;
+import com.java110.utils.exception.SMOException;
+import com.java110.utils.factory.ApplicationContextFactory;
+import com.java110.utils.util.Assert;
 import com.java110.core.base.controller.BaseController;
 import com.java110.core.context.IPageData;
-import com.java110.web.smo.impl.LoginServiceSMOImpl;
+import com.java110.core.context.PageData;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
-import org.springframework.http.HttpHeaders;
 import org.springframework.http.HttpStatus;
 import org.springframework.http.ResponseEntity;
-import org.springframework.util.MultiValueMap;
 import org.springframework.web.bind.annotation.*;
 import org.springframework.web.multipart.MultipartFile;
 
 import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
 import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
+import java.util.Map;
 
 
 /**
@@ -85,6 +86,7 @@
 
     /**
      * 璋冪敤缁勪欢 鏂囦欢涓婁紶
+     * /callComponent/upload/assetImport/importData
      *
      * @return
      */
@@ -97,6 +99,8 @@
             //@RequestBody String info,
             HttpServletRequest request) {
         ResponseEntity<String> responseEntity = null;
+        Map formParam = null;
+        IPageData pd = null;
         try {
             Assert.hasLength(componentCode, "鍙傛暟閿欒锛屾湭浼犲叆缁勪欢缂栫爜");
             Assert.hasLength(componentMethod, "鍙傛暟閿欒锛屾湭浼犲叆璋冪敤缁勪欢鏂规硶");
@@ -108,18 +112,19 @@
             Method cMethod = componentInstance.getClass().getDeclaredMethod(componentMethod, IPageData.class, MultipartFile.class);
 
             Assert.notNull(cMethod, "鏈壘鍒扮粍浠跺搴斿鐞嗙被鐨勬柟娉曪紝璇风‘璁� " + componentCode + "鏂规硶锛�" + componentMethod);
-
-            IPageData pd = (IPageData) request.getAttribute(CommonConstant.CONTEXT_PAGE_DATA);
+            pd = freshPageDate(request);
 
             logger.debug("缁勪欢缂栫爜{}锛岀粍浠舵柟娉晎}锛宲d 涓簕}", componentCode, componentMethod, pd.toString());
 
-            responseEntity = (ResponseEntity<String>) cMethod.invoke(componentInstance, pd);
+            responseEntity = (ResponseEntity<String>) cMethod.invoke(componentInstance, pd, uploadFile);
 
         } catch (SMOException e) {
+            logger.error("缁勪欢杩愯寮傚父",e);
             /*MultiValueMap<String, String> headers = new HttpHeaders();
             headers.add("code", e.getResult().getCode());*/
             responseEntity = new ResponseEntity<>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
         } catch (Exception e) {
+            logger.error("缁勪欢杩愯寮傚父",e);
             String msg = "";
             if (e instanceof InvocationTargetException) {
                 Throwable targetEx = ((InvocationTargetException) e).getTargetException();
@@ -136,5 +141,87 @@
         }
     }
 
+    /**
+     * 璋冪敤缁勪欢 鏂囦欢涓婁紶
+     * /callComponent/upload/assetImport/importData
+     *
+     * @return
+     */
+
+    @RequestMapping(path = "/callComponent/download/{componentCode}/{componentMethod}")
+    public ResponseEntity<Object> callComponentDownloadFile(
+            @PathVariable String componentCode,
+            @PathVariable String componentMethod,
+            HttpServletRequest request,
+            HttpServletResponse response) {
+        ResponseEntity<Object> responseEntity = null;
+        Map formParam = null;
+        IPageData pd = null;
+        try {
+            Assert.hasLength(componentCode, "鍙傛暟閿欒锛屾湭浼犲叆缁勪欢缂栫爜");
+            Assert.hasLength(componentMethod, "鍙傛暟閿欒锛屾湭浼犲叆璋冪敤缁勪欢鏂规硶");
+
+            Object componentInstance = ApplicationContextFactory.getBean(componentCode);
+
+            Assert.notNull(componentInstance, "鏈壘鍒扮粍浠跺搴旂殑澶勭悊绫伙紝璇风‘璁� " + componentCode);
+
+            Method cMethod = componentInstance.getClass().getDeclaredMethod(componentMethod, IPageData.class);
+
+            Assert.notNull(cMethod, "鏈壘鍒扮粍浠跺搴斿鐞嗙被鐨勬柟娉曪紝璇风‘璁� " + componentCode + "鏂规硶锛�" + componentMethod);
+            pd = freshPageDate(request);
+            responseEntity = (ResponseEntity<Object>) cMethod.invoke(componentInstance, pd);
+
+        } catch (SMOException e) {
+            logger.error("缁勪欢杩愯寮傚父",e);
+            responseEntity = new ResponseEntity<Object>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
+        } catch (Exception e) {
+            logger.error("缁勪欢杩愯寮傚父",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<Object>(msg, HttpStatus.INTERNAL_SERVER_ERROR);
+        } finally {
+            logger.debug("缁勪欢璋冪敤杩斿洖淇℃伅涓簕}", responseEntity);
+            return responseEntity;
+        }
+    }
+
+    /**
+     * 鍒锋柊 pd 瀵硅薄
+     *
+     * @param request HttpServletRequest 瀵硅薄
+     * @return pd 瀵硅薄
+     */
+    private IPageData freshPageDate(HttpServletRequest request) {
+        Map<String, String[]> params = request.getParameterMap();
+        IPageData pd = (IPageData) request.getAttribute(CommonConstant.CONTEXT_PAGE_DATA);
+        String reqData = "";
+        if (params != null && !params.isEmpty()) {
+            JSONObject paramObj = new JSONObject();
+            for (String key : params.keySet()) {
+                if (params.get(key).length > 0) {
+                    String value = "";
+                    for (int paramIndex = 0; paramIndex < params.get(key).length; paramIndex++) {
+                        value = params.get(key)[paramIndex] + ",";
+                    }
+                    value = value.endsWith(",") ? value.substring(0, value.length() - 1) : value;
+                    paramObj.put(key, value);
+                }
+                continue;
+            }
+            reqData = paramObj.toJSONString();
+        }
+
+        IPageData newPd = PageData.newInstance().builder(pd.getUserId(), pd.getToken(),
+                reqData, pd.getComponentCode(), pd.getComponentMethod(), "", pd.getSessionId());
+        return newPd;
+    }
+
 
 }

--
Gitblit v1.8.0