From 8a87e945defef06934f5a248d46f6df42ada0c81 Mon Sep 17 00:00:00 2001 From: tanghc Date: Tue, 4 Aug 2020 18:02:46 +0800 Subject: [PATCH] 4.0.2 --- .../param/ApiArgumentResolver.java | 59 ++++++++++--------- .../Example1001_BaseController.java | 9 +++ .../com/gitee/sop/test/ParamBindTest.java | 57 ++++++++++++++++++ 3 files changed, 96 insertions(+), 29 deletions(-) create mode 100644 sop-test/src/test/java/com/gitee/sop/test/ParamBindTest.java 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 afcb2fc5..4e94e389 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 @@ -86,21 +86,21 @@ public class ApiArgumentResolver implements SopHandlerMethodArgumentResolver { Class paramType = methodParameter.getParameterType(); // 排除的 boolean exclude = ( - WebRequest.class.isAssignableFrom(paramType) || - ServletRequest.class.isAssignableFrom(paramType) || - MultipartRequest.class.isAssignableFrom(paramType) || - HttpSession.class.isAssignableFrom(paramType) || - (pushBuilder != null && pushBuilder.isAssignableFrom(paramType)) || - Principal.class.isAssignableFrom(paramType) || - InputStream.class.isAssignableFrom(paramType) || - Reader.class.isAssignableFrom(paramType) || - HttpMethod.class == paramType || - Locale.class == paramType || - TimeZone.class == paramType || - ZoneId.class == paramType || - ServletResponse.class.isAssignableFrom(paramType) || - OutputStream.class.isAssignableFrom(paramType) || - Writer.class.isAssignableFrom(paramType) + WebRequest.class.isAssignableFrom(paramType) || + ServletRequest.class.isAssignableFrom(paramType) || + MultipartRequest.class.isAssignableFrom(paramType) || + HttpSession.class.isAssignableFrom(paramType) || + (pushBuilder != null && pushBuilder.isAssignableFrom(paramType)) || + Principal.class.isAssignableFrom(paramType) || + InputStream.class.isAssignableFrom(paramType) || + Reader.class.isAssignableFrom(paramType) || + HttpMethod.class == paramType || + Locale.class == paramType || + TimeZone.class == paramType || + ZoneId.class == paramType || + ServletResponse.class.isAssignableFrom(paramType) || + OutputStream.class.isAssignableFrom(paramType) || + Writer.class.isAssignableFrom(paramType) ); // 除此之外都匹配 return !exclude; @@ -119,9 +119,11 @@ public class ApiArgumentResolver implements SopHandlerMethodArgumentResolver { ) throws Exception { if (openApiParams.contains(methodParameter)) { Object paramObj = this.getParamObject(methodParameter, nativeWebRequest); - // JSR-303验证 - paramValidator.validateBizParam(paramObj); - return paramObj; + if (paramObj != null) { + // JSR-303验证 + paramValidator.validateBizParam(paramObj); + return paramObj; + } } HandlerMethodArgumentResolver resolver = getOtherArgumentResolver(methodParameter); if (resolver != null) { @@ -147,20 +149,19 @@ public class ApiArgumentResolver implements SopHandlerMethodArgumentResolver { HttpServletRequest request = (HttpServletRequest) nativeWebRequest.getNativeRequest(); JSONObject requestParams = OpenUtil.getRequestParams(request); String bizContent = requestParams.getString(ParamNames.BIZ_CONTENT_NAME); + if (bizContent == null) { + return null; + } // 方法参数类型 Class parameterType = methodParameter.getParameterType(); Object param; - if (bizContent != null) { - // 如果是json字符串 - if (JSONValidator.from(bizContent).validate()) { - param = JSON.parseObject(bizContent, parameterType); - } else { - // 否则认为是 aa=1&bb=33 形式 - Map query = OpenUtil.parseQueryToMap(bizContent); - param = new JSONObject(query).toJavaObject(parameterType); - } + // 如果是json字符串 + if (JSONValidator.from(bizContent).validate()) { + param = JSON.parseObject(bizContent, parameterType); } else { - param = requestParams.toJavaObject(parameterType); + // 否则认为是 aa=1&bb=33 形式 + Map query = OpenUtil.parseQueryToMap(bizContent); + param = new JSONObject(query).toJavaObject(parameterType); } this.bindUploadFile(param, request); return param; @@ -170,7 +171,7 @@ public class ApiArgumentResolver implements SopHandlerMethodArgumentResolver { /** * 将上传文件对象绑定到属性中 * - * @param param 业务参数 + * @param param 业务参数 * @param httpServletRequest */ protected void bindUploadFile(Object param, HttpServletRequest httpServletRequest) { 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 e3ab5752..f3525a20 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 @@ -62,6 +62,15 @@ public class Example1001_BaseController { return story; } + // 参数绑定 + @Open(value = "story.param.bind", mergeResult = false) + @RequestMapping("/get/param/v1") + public StoryResult param(int id, String name) { + StoryResult result = new StoryResult(); + result.setName("参数绑定:id:" + id + ", name:" + name); + return result; + } + // 忽略验证 @Open(value = "story.get.ignore", ignoreValidate = true) @RequestMapping("/get/ignore/v1") diff --git a/sop-test/src/test/java/com/gitee/sop/test/ParamBindTest.java b/sop-test/src/test/java/com/gitee/sop/test/ParamBindTest.java new file mode 100644 index 00000000..54c04c2b --- /dev/null +++ b/sop-test/src/test/java/com/gitee/sop/test/ParamBindTest.java @@ -0,0 +1,57 @@ +package com.gitee.sop.test; + +import com.alibaba.fastjson.JSON; +import com.gitee.sop.test.alipay.AlipaySignature; +import org.junit.Test; + +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.HashMap; +import java.util.Map; + +/** + * 模仿支付宝客户端请求接口 + */ +public class ParamBindTest extends TestBase { + + String url = "http://localhost:8081"; + String appId = "2019032617262200001"; + // 平台提供的私钥 + String privateKey = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCXJv1pQFqWNA/++OYEV7WYXwexZK/J8LY1OWlP9X0T6wHFOvxNKRvMkJ5544SbgsJpVcvRDPrcxmhPbi/sAhdO4x2PiPKIz9Yni2OtYCCeaiE056B+e1O2jXoLeXbfi9fPivJZkxH/tb4xfLkH3bA8ZAQnQsoXA0SguykMRZntF0TndUfvDrLqwhlR8r5iRdZLB6F8o8qXH6UPDfNEnf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quR47IROdY8qmwqcBAN7Vg2K7r7CPlTKAWTRYMJxCT1Hi5gwJb+CZF3+IeYqsJk2NF2s0w5WJTE70k1BSvQsfIzAIDz2yE1oPHvwVAoGAA6e+xQkVH4fMEph55RJIZ5goI4Y76BSvt2N5OKZKd4HtaV+eIhM3SDsVYRLIm9ZquJHMiZQGyUGnsvrKL6AAVNK7eQZCRDk9KQz+0GKOGqku0nOZjUbAu6A2/vtXAaAuFSFx1rUQVVjFulLexkXR3KcztL1Qu2k5pB6Si0K/uwQ="; + + // 演示参数绑定,没有用用到biz_content + @Test + public void testGet() throws Exception { + + // 公共请求参数 + Map params = new HashMap(); + params.put("app_id", appId); + params.put("method", "story.param.bind"); + params.put("format", "json"); + params.put("charset", "utf-8"); + params.put("sign_type", "RSA2"); + params.put("timestamp", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())); + params.put("version", "1.0"); + + // 业务参数 + params.put("id", "1"); + params.put("name", "葫芦娃"); + + String content = AlipaySignature.getSignContent(params); + String sign = AlipaySignature.rsa256Sign(content, privateKey, "utf-8"); + params.put("sign", sign); + + System.out.println("----------- 请求信息 -----------"); + System.out.println("请求参数:" + buildParamQuery(params)); + System.out.println("商户秘钥:" + privateKey); + System.out.println("待签名内容:" + content); + System.out.println("签名(sign):" + sign); + System.out.println("URL参数:" + buildUrlQuery(params)); + + System.out.println("----------- 返回结果 -----------"); + String responseData = get(url, params);// 发送请求 + System.out.println(responseData); + } + + +}