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 6a5bd95a..fbd55e8e 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 @@ -51,6 +51,7 @@ import java.util.concurrent.ConcurrentHashMap; @Slf4j public class ApiArgumentResolver implements SopHandlerMethodArgumentResolver { + private static final String HAS_INIT_OPEN_CONTEXT = "hasInitOpenContext"; private final Map argumentResolverCache = new ConcurrentHashMap<>(256); private static List NEED_INIT_OPEN_CONTEXT = new ArrayList<>(16); @@ -163,10 +164,15 @@ public class ApiArgumentResolver implements SopHandlerMethodArgumentResolver { private OpenContextImpl initOpenContextImpl(NativeWebRequest nativeWebRequest) { HttpServletRequest request = (HttpServletRequest) nativeWebRequest.getNativeRequest(); + Object hasInit = request.getAttribute(HAS_INIT_OPEN_CONTEXT); + if (hasInit != null) { + return (OpenContextImpl) ServiceContext.getCurrentContext().getOpenContext(); + } ServiceContext currentContext = ServiceContext.getCurrentContext(); JSONObject requestParams = OpenUtil.getRequestParams(request); OpenContextImpl openContext = new OpenContextImpl(requestParams); currentContext.setOpenContext(openContext); + request.setAttribute(HAS_INIT_OPEN_CONTEXT, true); return openContext; } 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 3517bf14..27ef9059 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 @@ -35,6 +35,7 @@ import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes; import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; import javax.validation.constraints.Min; import javax.validation.constraints.NotBlank; import javax.validation.constraints.NotNull; @@ -136,6 +137,14 @@ public class Example1001_BaseController { @Open("story.system.param.get4") @RequestMapping("/get/system/param/v4") public Object addGoods3(HttpServletRequest request, StoryParam param) { + param.setRemark(request.getRequestURI()); + return param; + } + + @Open("story.system.param.get5") + @RequestMapping("/get/system/param/v5") + public Object get5(HttpServletRequest request, HttpServletResponse response, StoryParam param) { + param.setRemark(request.getRequestURI() + ", getCharacterEncoding:" + response.getCharacterEncoding()); return param; } 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 0e935a0e..b970d8bb 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 @@ -422,6 +422,24 @@ public class AllInOneTest extends TestBase { client.execute(requestBuilder); } + public void testParamBind2() { + Client.RequestBuilder requestBuilder = new Client.RequestBuilder() + .method("story.system.param.get5") + .version("1.0") + .bizContent(new BizContent().add("id", "1").add("name", "葫芦娃")) + .postJson(true) + .httpMethod(HttpTool.HTTPMethod.POST) + .callback((requestInfo, responseData) -> { + System.out.println(responseData); + String node = requestInfo.getDataNode(); + JSONObject jsonObject = JSON.parseObject(responseData).getJSONObject(node); + Assert.assertEquals(1L, jsonObject.getLongValue("id")); + Assert.assertEquals("葫芦娃", jsonObject.getString("name")); + }); + + client.execute(requestBuilder); + } + /** * 未知异常 */