From afc6ef022fdc6d7b64eddc8f75e3dc6a5a2d939a Mon Sep 17 00:00:00 2001 From: zhangzhiyong Date: Wed, 3 Jul 2024 18:48:46 +0800 Subject: [PATCH] [enhancement] support Native Return in docean-mvc Module #855 --- .../java/com/xiaomi/youpin/docean/Mvc.java | 23 +++++++++++++++---- .../youpin/docean/anno/RequestMapping.java | 3 +++ .../youpin/docean/mvc/HttpRequestMethod.java | 3 +++ .../xiaomi/youpin/docean/mvc/MvcContext.java | 2 +- .../xiaomi/youpin/docean/mvc/MvcRunnable.java | 1 - 5 files changed, 25 insertions(+), 7 deletions(-) diff --git a/jcommon/docean/src/main/java/com/xiaomi/youpin/docean/Mvc.java b/jcommon/docean/src/main/java/com/xiaomi/youpin/docean/Mvc.java index 7d5f6c201..0a73ec97f 100644 --- a/jcommon/docean/src/main/java/com/xiaomi/youpin/docean/Mvc.java +++ b/jcommon/docean/src/main/java/com/xiaomi/youpin/docean/Mvc.java @@ -16,6 +16,7 @@ package com.xiaomi.youpin.docean; +import com.google.common.base.Throwables; import com.google.common.collect.Maps; import com.google.gson.Gson; import com.google.gson.JsonElement; @@ -138,6 +139,7 @@ private void registerControllerMethods(Bean bean) { } HttpRequestMethod hrm = new HttpRequestMethod(); hrm.setTimeout(rm.timeout()); + hrm.setOriginalRes(rm.originalRes()); hrm.setPath(path); hrm.setObj(bean.getObj()); hrm.setMethod(m); @@ -252,11 +254,8 @@ public void callMethod(MvcContext context, MvcRequest request, MvcResponse respo return; } // get whether the configuration returns an unwrapped value - boolean needOriginalValue = this.mvcConfig.isResponseOriginalValue(); - if (!needOriginalValue && StringUtils.isNotBlank(this.mvcConfig.getResponseOriginalPath())) { - needOriginalValue = Arrays.stream(mvcConfig.getResponseOriginalPath().split(",")) - .anyMatch(i -> i.equals(method.getPath())); - } + boolean needOriginalValue = isNeedOriginalValue(method); + if (needOriginalValue) { String responseData = data instanceof String ? (String) data : gson.toJson(data); response.writeAndFlush(context, responseData); @@ -269,6 +268,7 @@ public void callMethod(MvcContext context, MvcRequest request, MvcResponse respo context.setResponse(ex); return; } + ex = Throwables.getRootCause(ex); Throwable unwrapThrowable = ExceptionUtil.unwrapThrowable(ex); result.setCode(500); int httpCode = 200; @@ -284,6 +284,19 @@ public void callMethod(MvcContext context, MvcRequest request, MvcResponse respo }); } + private boolean isNeedOriginalValue(HttpRequestMethod method) { + //优先级最高 + if (method.isOriginalRes()) { + return true; + } + boolean needOriginalValue = this.mvcConfig.isResponseOriginalValue(); + if (!needOriginalValue && StringUtils.isNotBlank(this.mvcConfig.getResponseOriginalPath())) { + needOriginalValue = Arrays.stream(mvcConfig.getResponseOriginalPath().split(",")) + .anyMatch(i -> i.equals(method.getPath())); + } + return needOriginalValue; + } + private Object[] getMethodParams(MvcContext context, MvcRequest request, HttpRequestMethod method) { Object[] params = new Object[]{null}; if (context.isWebsocket()) { diff --git a/jcommon/docean/src/main/java/com/xiaomi/youpin/docean/anno/RequestMapping.java b/jcommon/docean/src/main/java/com/xiaomi/youpin/docean/anno/RequestMapping.java index a1cb37aa1..9b24e7d98 100644 --- a/jcommon/docean/src/main/java/com/xiaomi/youpin/docean/anno/RequestMapping.java +++ b/jcommon/docean/src/main/java/com/xiaomi/youpin/docean/anno/RequestMapping.java @@ -33,4 +33,7 @@ long timeout() default 2000; + //是否是原始res + boolean originalRes() default false; + } diff --git a/jcommon/docean/src/main/java/com/xiaomi/youpin/docean/mvc/HttpRequestMethod.java b/jcommon/docean/src/main/java/com/xiaomi/youpin/docean/mvc/HttpRequestMethod.java index 70756ec32..8faa6bc89 100644 --- a/jcommon/docean/src/main/java/com/xiaomi/youpin/docean/mvc/HttpRequestMethod.java +++ b/jcommon/docean/src/main/java/com/xiaomi/youpin/docean/mvc/HttpRequestMethod.java @@ -37,6 +37,9 @@ public class HttpRequestMethod { private long timeout; + //结果不封装 + private boolean originalRes; + private Map genericSuperclassTypeArguments; diff --git a/jcommon/docean/src/main/java/com/xiaomi/youpin/docean/mvc/MvcContext.java b/jcommon/docean/src/main/java/com/xiaomi/youpin/docean/mvc/MvcContext.java index d3acec10a..a05a23d17 100644 --- a/jcommon/docean/src/main/java/com/xiaomi/youpin/docean/mvc/MvcContext.java +++ b/jcommon/docean/src/main/java/com/xiaomi/youpin/docean/mvc/MvcContext.java @@ -63,7 +63,7 @@ public class MvcContext { private Object response; - private String contentType; + private String contentType = "application/json; charset=utf-8"; /** * rate limited or exceeded quota diff --git a/jcommon/docean/src/main/java/com/xiaomi/youpin/docean/mvc/MvcRunnable.java b/jcommon/docean/src/main/java/com/xiaomi/youpin/docean/mvc/MvcRunnable.java index 8e848470c..70242831e 100644 --- a/jcommon/docean/src/main/java/com/xiaomi/youpin/docean/mvc/MvcRunnable.java +++ b/jcommon/docean/src/main/java/com/xiaomi/youpin/docean/mvc/MvcRunnable.java @@ -2,7 +2,6 @@ import com.google.gson.Gson; import com.google.gson.JsonElement; -import com.google.gson.JsonParser; import com.xiaomi.youpin.docean.Mvc; import com.xiaomi.youpin.docean.bo.MvcConfig; import com.xiaomi.youpin.docean.common.Cons;