pull/10/MERGE tag-4.3.2
tanghc 4 years ago
parent 842f3a2ae5
commit f753faed66
  1. 2
      changelog.md
  2. 52
      sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/param/ApiArgumentResolver.java
  3. 11
      sop-example/sop-story/src/main/java/com/gitee/sop/storyweb/controller/Example1001_BaseController.java
  4. 15
      sop-test/src/test/java/com/gitee/sop/test/AllInOneTest.java

@ -2,7 +2,7 @@
## 4.3.2
- 修复微服务方法没有参数时获取不到OpenContext问题
- 修复微服务方法获取不到OpenContext问题
## 4.3.1

@ -56,6 +56,8 @@ public class ApiArgumentResolver implements SopHandlerMethodArgumentResolver {
private final Map<MethodParameter, HandlerMethodArgumentResolver> argumentResolverCache = new ConcurrentHashMap<>(256);
private static List<MethodParameter> 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<String, Object> requestParams = openContext.getParameterMap();
Object bizObj = requestParams.get(ParamNames.BIZ_CONTENT_NAME);
String bizContent = bizObj == null ? null : bizObj.toString();
if (bizContent == null) {
return null;
}

@ -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")

@ -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<String, Object> {
public BizContent add(String key, Object value) {
this.put(key, value);

Loading…
Cancel
Save