From f8defd859870fc7ba0a4613451b55e34006ac3d7 Mon Sep 17 00:00:00 2001 From: zhangzhiyong Date: Tue, 2 Jan 2024 18:12:50 +0800 Subject: [PATCH] fix: fix for Git request parameter problem --- jcommon/docean/README.md | 2 +- jcommon/docean/pom.xml | 8 ++++++++ .../main/java/com/xiaomi/youpin/docean/Mvc.java | 8 ++++++-- .../java/com/xiaomi/youpin/docean/mvc/Get.java | 17 +++++++++++------ .../docean/mvc/httpmethod/HttpMethodUtils.java | 9 ++++++++- 5 files changed, 34 insertions(+), 10 deletions(-) diff --git a/jcommon/docean/README.md b/jcommon/docean/README.md index 25b43c750..3919b1f98 100644 --- a/jcommon/docean/README.md +++ b/jcommon/docean/README.md @@ -4,7 +4,7 @@ * Based on Java20 * Fully utilized coroutines and ScopeValue * JVM parameters that need to be added: - --enable-preview--add-modulesjdk.incubator.concurrent-ea--add-opensjava.base/java.lang=ALL-UNNAMED--add-opensjava.base/jdk.internal.misc=ALL-UNNAMED-Dio.netty.tryReflectionSetAccessible=true + --enable-preview --add-modulesjdk.incubator.concurrent -ea --add-opensjava.base/java.lang=ALL-UNNAMED--add-opensjava.base/jdk.internal.misc=ALL-UNNAMED-Dio.netty.tryReflectionSetAccessible=true * A lightweight microservices development framework. It can be embedded into the Spring framework. * Features: Compliant with Java standards, lightweight, no unnecessary libraries, low memory footprint, fast service requests, high maintainability, and supports plugin extensions. diff --git a/jcommon/docean/pom.xml b/jcommon/docean/pom.xml index 464159cfb..448cd20de 100644 --- a/jcommon/docean/pom.xml +++ b/jcommon/docean/pom.xml @@ -65,6 +65,14 @@ 21 + + org.apache.maven.plugins + maven-compiler-plugin + + 21 + 21 + + 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 c42c9be6a..cc61fd289 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 @@ -171,8 +171,12 @@ public void callMethod(MvcContext context, MvcRequest request, MvcResponse respo Safe.run(() -> { JsonElement args = getArgs(method, request.getMethod().toLowerCase(Locale.ROOT), request); context.setParams(args); - Object[] params = methodInvoker.getMethodParams(method.getMethod(), args); - setMvcContext(context, params); + Object[] params = new Object[]{null}; + if (method.getMethod().getParameterTypes().length == 1 && method.getMethod().getParameterTypes()[0].equals(MvcContext.class)) { + params[0] = context; + } else { + params = methodInvoker.getMethodParams(method.getMethod(), args); + } Object data = this.mvcConfig.isUseCglib() ? methodInvoker.invokeFastMethod(method.getObj(), method.getMethod(), params) : methodInvoker.invokeMethod(method.getObj(), method.getMethod(), params); diff --git a/jcommon/docean/src/main/java/com/xiaomi/youpin/docean/mvc/Get.java b/jcommon/docean/src/main/java/com/xiaomi/youpin/docean/mvc/Get.java index 4330d22a9..085bac1b6 100644 --- a/jcommon/docean/src/main/java/com/xiaomi/youpin/docean/mvc/Get.java +++ b/jcommon/docean/src/main/java/com/xiaomi/youpin/docean/mvc/Get.java @@ -17,6 +17,8 @@ package com.xiaomi.youpin.docean.mvc; import com.google.gson.JsonArray; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; import com.xiaomi.youpin.docean.anno.RequestParam; import com.xiaomi.youpin.docean.mvc.httpmethod.HttpMethodUtils; import io.netty.handler.codec.http.QueryStringDecoder; @@ -31,14 +33,18 @@ */ public abstract class Get { - public static JsonArray getParams(HttpRequestMethod method, String uri) { + public static JsonElement getParams(HttpRequestMethod method, String uri) { QueryStringDecoder decoder = new QueryStringDecoder(uri); Map params = decoder.parameters().entrySet().stream().collect(Collectors.toMap(it -> it.getKey(), it -> it.getValue().get(0))); - JsonArray array = new JsonArray(); - HttpMethodUtils.addMvcContext(method, array); - if (null == params) { - return array; + + //只有一个参数,并且是MvcContext + if (HttpMethodUtils.paramIsMvcContext(method)) { + JsonObject res = new JsonObject(); + params.entrySet().forEach(it-> res.addProperty(it.getKey(),it.getValue())); + return res; } + + JsonArray array = new JsonArray(); Annotation[][] anns = method.getMethod().getParameterAnnotations(); Arrays.stream(anns).forEach(it -> { if (it.length > 0) { @@ -46,7 +52,6 @@ public static JsonArray getParams(HttpRequestMethod method, String uri) { String name = param.value(); if (!params.containsKey(name)) { array.add(""); -// throw new DoceanException("Missing parameter:" + name); } else { array.add(params.get(name)); } diff --git a/jcommon/docean/src/main/java/com/xiaomi/youpin/docean/mvc/httpmethod/HttpMethodUtils.java b/jcommon/docean/src/main/java/com/xiaomi/youpin/docean/mvc/httpmethod/HttpMethodUtils.java index d91d9faf5..ff6e7e889 100644 --- a/jcommon/docean/src/main/java/com/xiaomi/youpin/docean/mvc/httpmethod/HttpMethodUtils.java +++ b/jcommon/docean/src/main/java/com/xiaomi/youpin/docean/mvc/httpmethod/HttpMethodUtils.java @@ -12,10 +12,17 @@ public class HttpMethodUtils { public static void addMvcContext(HttpRequestMethod method, JsonArray array) { + if (paramIsMvcContext(method)) { + array.add(new JsonObject()); + } + } + + public static boolean paramIsMvcContext(HttpRequestMethod method) { Class[] types = method.getMethod().getParameterTypes(); if (types.length > 0 && types[0] == MvcContext.class) { - array.add(new JsonObject()); + return true; } + return false; }