pull/1/head
tanghc 5 years ago
parent 7d5d6ed3e3
commit 3446a32bdf
  1. 17
      sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/bean/OpenContextImpl.java
  2. 25
      sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/interceptor/ServiceContextInterceptor.java
  3. 6
      sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/param/ApiArgumentResolver.java

@ -26,15 +26,18 @@ public class OpenContextImpl<T> implements OpenContext<T> {
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<T> implements OpenContext<T> {
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);
}
}

@ -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();

@ -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();

Loading…
Cancel
Save