From 3446a32bdf4b3faf278c73ac6e85585ff5782f43 Mon Sep 17 00:00:00 2001 From: tanghc Date: Mon, 26 Aug 2019 10:50:10 +0800 Subject: [PATCH] 2.0 --- .../servercommon/bean/OpenContextImpl.java | 17 +++++++------ .../ServiceContextInterceptor.java | 25 +++++++++++++++++++ .../param/ApiArgumentResolver.java | 6 ++--- 3 files changed, 38 insertions(+), 10 deletions(-) diff --git a/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/bean/OpenContextImpl.java b/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/bean/OpenContextImpl.java index 3dbd0f77..ebe03d80 100644 --- a/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/bean/OpenContextImpl.java +++ b/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/bean/OpenContextImpl.java @@ -26,15 +26,18 @@ public class OpenContextImpl implements OpenContext { private JSONObject rootJsonObject; private T bizObject; + public OpenContextImpl(JSONObject rootJsonObject) { + this(rootJsonObject, null); + } + public OpenContextImpl(JSONObject rootJsonObject, Class bizClass) { this.rootJsonObject = rootJsonObject; if (bizClass != null) { - String bizContent = this.rootJsonObject.getString(BIZ_CONTENT_NAME); - if (bizContent == null) { - bizObject = (T) rootJsonObject.toJavaObject(bizClass); - } else { - bizObject = (T) JSON.parseObject(bizContent, bizClass); + JSONObject bizJsonObj = this.rootJsonObject.getJSONObject(BIZ_CONTENT_NAME); + if (bizJsonObj == null) { + bizJsonObj = rootJsonObject; } + bizObject = (T) bizJsonObj.toJavaObject(bizClass); } } @@ -103,10 +106,10 @@ public class OpenContextImpl implements OpenContext { if (bizObject != null && bizObject.getClass() == clazz) { return (E) bizObject; } - JSONObject bizJsonObj = this.rootJsonObject.getJSONObject(BIZ_CONTENT_NAME); + String bizJsonObj = this.rootJsonObject.getString(BIZ_CONTENT_NAME); if (bizJsonObj == null) { return null; } - return bizJsonObj.toJavaObject(clazz); + return JSON.parseObject(bizJsonObj, clazz); } } diff --git a/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/interceptor/ServiceContextInterceptor.java b/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/interceptor/ServiceContextInterceptor.java index 7ff9495f..e5da2b2a 100644 --- a/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/interceptor/ServiceContextInterceptor.java +++ b/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/interceptor/ServiceContextInterceptor.java @@ -1,6 +1,11 @@ package com.gitee.sop.servercommon.interceptor; +import com.alibaba.fastjson.JSONObject; +import com.gitee.sop.servercommon.bean.OpenContext; +import com.gitee.sop.servercommon.bean.OpenContextImpl; import com.gitee.sop.servercommon.bean.ServiceContext; +import com.gitee.sop.servercommon.util.OpenUtil; +import org.springframework.web.method.HandlerMethod; import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; import javax.servlet.http.HttpServletRequest; @@ -10,14 +15,34 @@ import javax.servlet.http.HttpServletResponse; * @author tanghc */ public class ServiceContextInterceptor extends HandlerInterceptorAdapter { + @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { ServiceContext context = ServiceContext.getCurrentContext(); context.setRequest(request); context.setResponse(response); + if (handler instanceof HandlerMethod) { + HandlerMethod handlerMethod = (HandlerMethod) handler; + // 修复方法参数个数为0,OpenContext获取null问题 + if (handlerMethod.getMethodParameters().length == 0) { + initOpenContext(request); + } + } return true; } + + /** + * 初始化OpenContext + * + * @param request request + */ + protected void initOpenContext(HttpServletRequest request) { + JSONObject requestParams = OpenUtil.getRequestParams(request); + OpenContext openContext = new OpenContextImpl(requestParams); + ServiceContext.getCurrentContext().setOpenContext(openContext); + } + @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { ServiceContext.getCurrentContext().unset(); 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 cc0660e1..68dafc12 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 @@ -90,9 +90,9 @@ public class ApiArgumentResolver implements SopHandlerMethodArgumentResolver { /** * 获取参数对象,将request中的参数绑定到实体对象中去 * - * @param methodParameter - * @param nativeWebRequest - * @return 没有返回null + * @param methodParameter 方法参数 + * @param nativeWebRequest request + * @return 返回参数绑定的对象,没有返回null */ protected Object getParamObject(MethodParameter methodParameter, NativeWebRequest nativeWebRequest) { HttpServletRequest request = (HttpServletRequest) nativeWebRequest.getNativeRequest();