From f753faed66aff9bb067b139c76ea67f3973ad9c5 Mon Sep 17 00:00:00 2001 From: tanghc Date: Thu, 25 Mar 2021 17:16:34 +0800 Subject: [PATCH] 4.3.2 --- changelog.md | 2 +- .../param/ApiArgumentResolver.java | 52 ++++++++++++++----- .../Example1001_BaseController.java | 11 ++++ .../java/com/gitee/sop/test/AllInOneTest.java | 15 ++++++ 4 files changed, 67 insertions(+), 13 deletions(-) diff --git a/changelog.md b/changelog.md index eaa46040..2efc0a3d 100644 --- a/changelog.md +++ b/changelog.md @@ -2,7 +2,7 @@ ## 4.3.2 -- 修复微服务方法没有参数时获取不到OpenContext问题 +- 修复微服务方法获取不到OpenContext问题 ## 4.3.1 diff --git a/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/param/ApiArgumentResolver.java b/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/param/ApiArgumentResolver.java index 8b741ea4..abb95648 100644 --- a/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/param/ApiArgumentResolver.java +++ b/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/param/ApiArgumentResolver.java @@ -56,6 +56,8 @@ public class ApiArgumentResolver implements SopHandlerMethodArgumentResolver { private final Map argumentResolverCache = new ConcurrentHashMap<>(256); + private static List NEED_INIT_OPEN_CONTEXT = new ArrayList<>(16); + private ParamValidator paramValidator = new ServiceParamValidator(); private static Class pushBuilder; @@ -89,8 +91,8 @@ public class ApiArgumentResolver implements SopHandlerMethodArgumentResolver { return false; } Class paramType = methodParameter.getParameterType(); - // 排除的 - boolean exclude = ( + // 特殊参数 + boolean special = ( WebRequest.class.isAssignableFrom(paramType) || ServletRequest.class.isAssignableFrom(paramType) || MultipartRequest.class.isAssignableFrom(paramType) || @@ -107,12 +109,17 @@ public class ApiArgumentResolver implements SopHandlerMethodArgumentResolver { OutputStream.class.isAssignableFrom(paramType) || Writer.class.isAssignableFrom(paramType) ); - // 除此之外都匹配 - boolean support = !exclude; - if (support) { - this.wrapSingleParam(methodParameter, open); + // 特殊参数只需要初始化OpenContext + if (special) { + NEED_INIT_OPEN_CONTEXT.add(methodParameter); } - return support; + this.wrapSingleParam(methodParameter, open); + return true; + } + + + private boolean isOnlyInitOpenContext(MethodParameter methodParameter) { + return NEED_INIT_OPEN_CONTEXT.contains(methodParameter); } /** @@ -136,6 +143,20 @@ public class ApiArgumentResolver implements SopHandlerMethodArgumentResolver { , NativeWebRequest nativeWebRequest , WebDataBinderFactory webDataBinderFactory ) throws Exception { + // 特殊参数只需要初始化OpenContext + if (isOnlyInitOpenContext(methodParameter)) { + this.initOpenContextImpl(nativeWebRequest); + HandlerMethodArgumentResolver resolver = getOtherArgumentResolver(methodParameter); + if (resolver != null) { + return resolver.resolveArgument( + methodParameter + , modelAndViewContainer + , nativeWebRequest + , webDataBinderFactory + ); + } + return null; + } nativeWebRequest = new SopServletWebRequest( (HttpServletRequest) nativeWebRequest.getNativeRequest(), (HttpServletResponse) nativeWebRequest.getNativeResponse() @@ -170,6 +191,14 @@ public class ApiArgumentResolver implements SopHandlerMethodArgumentResolver { return null; } + private OpenContextImpl initOpenContextImpl(NativeWebRequest nativeWebRequest) { + HttpServletRequest request = (HttpServletRequest) nativeWebRequest.getNativeRequest(); + ServiceContext currentContext = ServiceContext.getCurrentContext(); + JSONObject requestParams = OpenUtil.getRequestParams(request); + OpenContextImpl openContext = new OpenContextImpl(requestParams); + currentContext.setOpenContext(openContext); + return openContext; + } /** * 获取参数对象,将request中的参数绑定到实体对象中去 @@ -180,11 +209,10 @@ public class ApiArgumentResolver implements SopHandlerMethodArgumentResolver { */ protected Object getParamObject(MethodParameter methodParameter, NativeWebRequest nativeWebRequest) { HttpServletRequest request = (HttpServletRequest) nativeWebRequest.getNativeRequest(); - ServiceContext currentContext = ServiceContext.getCurrentContext(); - JSONObject requestParams = OpenUtil.getRequestParams(request); - OpenContextImpl openContext = new OpenContextImpl(requestParams); - currentContext.setOpenContext(openContext); - String bizContent = requestParams.getString(ParamNames.BIZ_CONTENT_NAME); + OpenContextImpl openContext = initOpenContextImpl(nativeWebRequest); + Map requestParams = openContext.getParameterMap(); + Object bizObj = requestParams.get(ParamNames.BIZ_CONTENT_NAME); + String bizContent = bizObj == null ? null : bizObj.toString(); if (bizContent == null) { return null; } diff --git a/sop-example/sop-story/src/main/java/com/gitee/sop/storyweb/controller/Example1001_BaseController.java b/sop-example/sop-story/src/main/java/com/gitee/sop/storyweb/controller/Example1001_BaseController.java index 0c6679c8..9745245c 100644 --- a/sop-example/sop-story/src/main/java/com/gitee/sop/storyweb/controller/Example1001_BaseController.java +++ b/sop-example/sop-story/src/main/java/com/gitee/sop/storyweb/controller/Example1001_BaseController.java @@ -121,6 +121,17 @@ public class Example1001_BaseController { return result; } + @Open(value = "story.system.param.get3") + @GetMapping("/get/system/param/v3") + public StoryResult systemParam3(HttpServletRequest request) { + System.out.println(request.getParameter("app_id")); + StoryResult result = new StoryResult(); + OpenContext openContext = ServiceContext.getCurrentContext().getOpenContext(); + String token = openContext.getAppAuthToken(); + result.setName(token); + return result; + } + // 参数绑定,少量参数可以这样写,参数多了建议放进类里面 @Open(value = "story.oneparam") @GetMapping("/oneParam/v1") diff --git a/sop-test/src/test/java/com/gitee/sop/test/AllInOneTest.java b/sop-test/src/test/java/com/gitee/sop/test/AllInOneTest.java index 8dcca3ff..dc8a9320 100644 --- a/sop-test/src/test/java/com/gitee/sop/test/AllInOneTest.java +++ b/sop-test/src/test/java/com/gitee/sop/test/AllInOneTest.java @@ -389,6 +389,21 @@ public class AllInOneTest extends TestBase { client.execute(requestBuilder); } + public void testParamSystem() { + String token = "asdfasdfadsf"; + Client.RequestBuilder requestBuilder = new Client.RequestBuilder() + .method("story.system.param.get3") + .version("1.0") + .appAuthToken(token) + .httpMethod(HttpTool.HTTPMethod.GET) + .callback((requestInfo, responseData) -> { + System.out.println(responseData); + Assert.assertTrue(responseData.contains(token)); + }); + + client.execute(requestBuilder); + } + static class BizContent extends HashMap { public BizContent add(String key, Object value) { this.put(key, value);