From b1f8f5a7ffbee7d11d2c6903b5428a29bd64f61e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E6=B5=B7?= <10402852@qq.com> Date: Fri, 20 Sep 2024 11:16:32 +0800 Subject: [PATCH] 'commit' --- .../base/Dm/Controller/DmController.java | 2 - .../src/main/resource/undertow_dev.properties | 2 +- .../src/main/resource/undertow_pro.properties | 4 +- .../com/dsideal/gw/Handler/RouterHandler.java | 46 +++++++++++++++---- .../main/resources/application_dev.properties | 2 +- .../main/resources/application_pro.properties | 2 +- .../target/classes/application_dev.properties | 2 +- .../target/classes/application_pro.properties | 2 +- 8 files changed, 43 insertions(+), 19 deletions(-) diff --git a/ds-base/src/main/java/com/dsideal/base/Dm/Controller/DmController.java b/ds-base/src/main/java/com/dsideal/base/Dm/Controller/DmController.java index b46c87cd..2fbf8345 100644 --- a/ds-base/src/main/java/com/dsideal/base/Dm/Controller/DmController.java +++ b/ds-base/src/main/java/com/dsideal/base/Dm/Controller/DmController.java @@ -23,11 +23,9 @@ public class DmController extends Controller { * 时间:2019-02-23 */ @Before({GET.class}) -// @IsLoginInterface({}) public void getDmSchoolProperty() { List rs = model.getDmSchoolProperty(); renderJson(CommonUtil.renderJsonForLayUI(rs)); - return; } /** diff --git a/ds-base/src/main/resource/undertow_dev.properties b/ds-base/src/main/resource/undertow_dev.properties index 07d613d9..8329bab6 100644 --- a/ds-base/src/main/resource/undertow_dev.properties +++ b/ds-base/src/main/resource/undertow_dev.properties @@ -6,7 +6,7 @@ undertow.host=0.0.0.0 undertow.resourcePath =D:/dsWork/dsExam/ds-base/WebRoot,classpath:static # 目录名称 -undertow.contextPath=/dsBase +undertow.contextPath=/ # 设定I/O线程数. server.undertow.io-threads=8 diff --git a/ds-base/src/main/resource/undertow_pro.properties b/ds-base/src/main/resource/undertow_pro.properties index 67b5bb38..5d8aafbd 100644 --- a/ds-base/src/main/resource/undertow_pro.properties +++ b/ds-base/src/main/resource/undertow_pro.properties @@ -1,11 +1,11 @@ # true 值支持热加载 undertow.devMode=true -undertow.port=8000 +undertow.port=8001 undertow.host=0.0.0.0 undertow.resourcePath =/root/WebRoot,classpath:static # 目录名称 -undertow.contextPath=/dsBase +undertow.contextPath=/ # 设定I/O线程数. server.undertow.io-threads=8 diff --git a/ds-gw/src/main/java/com/dsideal/gw/Handler/RouterHandler.java b/ds-gw/src/main/java/com/dsideal/gw/Handler/RouterHandler.java index abf8d8ad..d04f08d2 100644 --- a/ds-gw/src/main/java/com/dsideal/gw/Handler/RouterHandler.java +++ b/ds-gw/src/main/java/com/dsideal/gw/Handler/RouterHandler.java @@ -1,6 +1,7 @@ package com.dsideal.gw.Handler; import cn.hutool.json.JSONObject; +import cn.hutool.json.JSONUtil; import com.dsideal.gw.GwApplication; import com.jfinal.handler.Handler; import okhttp3.*; @@ -30,9 +31,15 @@ public class RouterHandler extends Handler { } } - public void renderJson(HttpServletResponse res, String msg) { - renderJson(res, new JSONObject().put("msg", msg)); - } + + /** + * 测试用例: + * GET + * http://10.10.21.20:8000/ds-base/dm/getDmSchoolProperty + * http://10.10.21.20:8000/ds-base/global/getGlobalList?page=1&limit=10 + * + * + */ //OkHttp的实例,单例模式 private static final OkHttpClient OK_HTTP_CLIENT = new OkHttpClient(); @@ -42,9 +49,13 @@ public class RouterHandler extends Handler { Response response = OK_HTTP_CLIENT.newCall(request).execute(); if (response.isSuccessful()) { String responseBody = response.body().string(); - renderJson(res, responseBody); + JSONObject jo = JSONUtil.parseObj(responseBody).put("success", true).put("code", response.code()); + renderJson(res, jo); } else { - renderJson(res, "请求失败!"); + JSONObject jo = new JSONObject(); + jo.put("message", "请求失败!"); + jo.put("code", response.code()); + renderJson(res, jo); } } @@ -59,13 +70,19 @@ public class RouterHandler extends Handler { int firstSlashIndex = servletPath.indexOf('/'); // 找到第一个 '/' 的索引 int secondSlashIndex = servletPath.indexOf('/', firstSlashIndex + 1); // 从第一个 '/' 之后开始找第二个 '/' 的索引 + //action名称 + String action = servletPath.substring(secondSlashIndex + 1); + if (firstSlashIndex != -1 && secondSlashIndex != -1) { String prefix = servletPath.substring(firstSlashIndex + 1, secondSlashIndex); // 截取两个 '/' 之间的内容 if (!GwApplication.routeDict.containsKey(prefix)) { - renderJson(res, prefix + "前缀没有找到合适的路由表,请检查是否请求正确!"); + JSONObject jo = new JSONObject(); + jo.put("success", false); + jo.put("message", prefix + "前缀没有找到合适的路由表,请检查是否请求正确!"); + renderJson(res, jo); } else { //路由到哪个微服务 - String FORWARD_URL = GwApplication.routeDict.get(prefix); + String FORWARD_URL = GwApplication.routeDict.get(prefix) + "/" + action; //处理GET请求 if (req.getMethod().equals("GET")) { //参数:queryString @@ -78,7 +95,10 @@ public class RouterHandler extends Handler { try { executeRequest(request, res); } catch (IOException e) { - renderJson(res, "请求失败!"+ e); + JSONObject jo = new JSONObject(); + jo.put("success", false); + jo.put("message", "请求失败!" + e); + renderJson(res, jo); } }//处理POST请求 else if (req.getMethod().equals("POST")) { @@ -87,11 +107,17 @@ public class RouterHandler extends Handler { } else { // MinIO - 服务端签名直传(前端 + 后端 + 效果演示) //https://blog.csdn.net/CYK_byte/article/details/140254412 - renderJson(res, "系统只支持GET,POST,其它的OPTIONS不支持!文件上传请使用S3协议进行直传,不通过JAVA处理,JAVA只处理授权!"); + JSONObject jo = new JSONObject(); + jo.put("success", false); + jo.put("message", "系统只支持GET,POST,其它的OPTIONS不支持!文件上传请使用S3协议进行直传,不通过JAVA处理,JAVA只处理授权!"); + renderJson(res, jo); } } } else { - renderJson(res, "输入的字符串格式不正确,没有找到两个 '/'。"); + JSONObject jo = new JSONObject(); + jo.put("success", false); + jo.put("message", "输入的字符串格式不正确,没有找到两个 '/'。"); + renderJson(res, jo); } //停止filter isHandled[0] = true; diff --git a/ds-gw/src/main/resources/application_dev.properties b/ds-gw/src/main/resources/application_dev.properties index 8e397d08..67d10b35 100644 --- a/ds-gw/src/main/resources/application_dev.properties +++ b/ds-gw/src/main/resources/application_dev.properties @@ -16,5 +16,5 @@ excelExportTemplatePathSuffix=/ExcelExportTemplate/ ExcelImportTemplatePathSuffix=/ExcelImportTemplate/ # 基础数据 -route_dsBase= http://ds-base:8001 +route_ds-base= http://ds-base:8001 diff --git a/ds-gw/src/main/resources/application_pro.properties b/ds-gw/src/main/resources/application_pro.properties index 4783d4b7..39c7e9dd 100644 --- a/ds-gw/src/main/resources/application_pro.properties +++ b/ds-gw/src/main/resources/application_pro.properties @@ -16,4 +16,4 @@ excelExportTemplatePathSuffix=/ExcelExportTemplate/ ExcelImportTemplatePathSuffix=/ExcelImportTemplate/ # 基础数据 -route_dsBase= http://ds-base:8001 \ No newline at end of file +route_ds-base= http://ds-base:8001 \ No newline at end of file diff --git a/ds-gw/target/classes/application_dev.properties b/ds-gw/target/classes/application_dev.properties index 8e397d08..67d10b35 100644 --- a/ds-gw/target/classes/application_dev.properties +++ b/ds-gw/target/classes/application_dev.properties @@ -16,5 +16,5 @@ excelExportTemplatePathSuffix=/ExcelExportTemplate/ ExcelImportTemplatePathSuffix=/ExcelImportTemplate/ # 基础数据 -route_dsBase= http://ds-base:8001 +route_ds-base= http://ds-base:8001 diff --git a/ds-gw/target/classes/application_pro.properties b/ds-gw/target/classes/application_pro.properties index 4783d4b7..39c7e9dd 100644 --- a/ds-gw/target/classes/application_pro.properties +++ b/ds-gw/target/classes/application_pro.properties @@ -16,4 +16,4 @@ excelExportTemplatePathSuffix=/ExcelExportTemplate/ ExcelImportTemplatePathSuffix=/ExcelImportTemplate/ # 基础数据 -route_dsBase= http://ds-base:8001 \ No newline at end of file +route_ds-base= http://ds-base:8001 \ No newline at end of file