修复方法参数个数为0,OpenContext获取null问题

1.x
tanghc 5 years ago
parent 0b4e09b0e5
commit de00526ef7
  1. 4
      sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/bean/OpenContextImpl.java
  2. 28
      sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/interceptor/ServiceContextInterceptor.java
  3. 2
      sop-example/sop-story/sop-story-web/src/main/java/com/gitee/sop/storyweb/controller/AlipayController.java

@ -25,6 +25,10 @@ public class OpenContextImpl<T> implements OpenContext<T> {
private JSONObject rootJsonObject; private JSONObject rootJsonObject;
private T bizObject; private T bizObject;
public OpenContextImpl(JSONObject rootJsonObject) {
this(rootJsonObject, null);
}
public OpenContextImpl(JSONObject rootJsonObject, Class<?> bizClass) { public OpenContextImpl(JSONObject rootJsonObject, Class<?> bizClass) {
this.rootJsonObject = rootJsonObject; this.rootJsonObject = rootJsonObject;
if (bizClass != null) { if (bizClass != null) {

@ -1,6 +1,12 @@
package com.gitee.sop.servercommon.interceptor; 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.bean.ServiceContext;
import com.gitee.sop.servercommon.util.OpenUtil;
import lombok.Setter;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
@ -9,15 +15,37 @@ import javax.servlet.http.HttpServletResponse;
/** /**
* @author tanghc * @author tanghc
*/ */
@Setter
public class ServiceContextInterceptor extends HandlerInterceptorAdapter { public class ServiceContextInterceptor extends HandlerInterceptorAdapter {
@Override @Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
ServiceContext context = ServiceContext.getCurrentContext(); ServiceContext context = ServiceContext.getCurrentContext();
context.setRequest(request); context.setRequest(request);
context.setResponse(response); context.setResponse(response);
if (handler instanceof HandlerMethod) {
HandlerMethod handlerMethod = (HandlerMethod) handler;
// 修复方法参数个数为0,OpenContext获取null问题
if (handlerMethod.getMethodParameters().length == 0) {
initOpenContext(request);
}
}
return true; 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 @Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
ServiceContext.getCurrentContext().unset(); ServiceContext.getCurrentContext().unset();

@ -109,6 +109,8 @@ public class AlipayController {
@ApiAbility @ApiAbility
@GetMapping("getStory2") @GetMapping("getStory2")
public Story getStory2_0() { public Story getStory2_0() {
OpenContext openContext = ServiceContext.getCurrentContext().getOpenContext();
System.out.println(openContext.getAppId());
Story story = new Story(); Story story = new Story();
story.setId(1); story.setId(1);
story.setName("海底小纵队(默认版本号)"); story.setName("海底小纵队(默认版本号)");

Loading…
Cancel
Save