From 9f78ecededc5560f13f8bb76e538165e08b4c3c1 Mon Sep 17 00:00:00 2001 From: tanghc Date: Thu, 25 Jul 2019 20:34:29 +0800 Subject: [PATCH] 1.13.2 --- changelog.md | 5 ++ doc/docs/_sidebar.md | 60 ++++++------- doc/docs/files/90100_常见问题.md | 7 ++ sop-admin/sop-admin-server/pom.xml | 2 +- sop-common/pom.xml | 2 +- sop-common/sop-gateway-common/pom.xml | 4 +- sop-common/sop-registry-api/pom.xml | 2 +- sop-common/sop-service-common/pom.xml | 4 +- .../sop/servercommon/bean/OpenContext.java | 74 ++++++++++++++++ .../servercommon/bean/OpenContextImpl.java | 73 ++++++++++++++++ .../sop/servercommon/bean/ServiceContext.java | 9 ++ .../param/ApiArgumentResolver.java | 48 +++++++++-- .../gitee/sop/servercommon/util/OpenUtil.java | 85 +++++++++++++++++++ sop-example/sop-auth/pom.xml | 2 +- sop-example/sop-book/sop-book-web/pom.xml | 2 +- sop-example/sop-easyopen/pom.xml | 2 +- sop-example/sop-springmvc/pom.xml | 2 +- sop-example/sop-story/sop-story-web/pom.xml | 2 +- .../storyweb/controller/AlipayController.java | 9 +- .../controller/PostJsonController.java | 41 +++++++++ sop-gateway/pom.xml | 2 +- .../SDKCSharp/Client/OpenRequest.cs | 2 +- .../com/gitee/sop/sdk/client/OpenRequest.java | 2 +- .../java/com/gitee/sop/test/PostJsonTest.java | 56 ++++++++++++ .../java/com/gitee/sop/test/TestBase.java | 8 ++ sop-website/website-server/pom.xml | 2 +- 26 files changed, 449 insertions(+), 58 deletions(-) create mode 100644 sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/bean/OpenContext.java create mode 100644 sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/bean/OpenContextImpl.java create mode 100644 sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/util/OpenUtil.java create mode 100644 sop-example/sop-story/sop-story-web/src/main/java/com/gitee/sop/storyweb/controller/PostJsonController.java create mode 100644 sop-test/src/test/java/com/gitee/sop/test/PostJsonTest.java diff --git a/changelog.md b/changelog.md index 681d86b1..2f976f0e 100644 --- a/changelog.md +++ b/changelog.md @@ -1,5 +1,10 @@ # changelog +## 1.13.2 + +- 修复json方式请求获取不到参数问题 +- 微服务端新增获取开放平台请求参数 + ## 1.13.1 - 支持json方式请求(application/json) diff --git a/doc/docs/_sidebar.md b/doc/docs/_sidebar.md index 1249b307..8cb54fc6 100644 --- a/doc/docs/_sidebar.md +++ b/doc/docs/_sidebar.md @@ -1,32 +1,32 @@ -* [首页](/?t=1564027102043) +* [首页](/?t=1564057981506) * 开发文档 - * [快速体验](files/10010_快速体验.md?t=1564027102045) - * [项目接入到SOP](files/10011_项目接入到SOP.md?t=1564027102062) - * [新增接口](files/10020_新增接口.md?t=1564027102062) - * [业务参数校验](files/10030_业务参数校验.md?t=1564027102062) - * [错误处理](files/10040_错误处理.md?t=1564027102062) - * [编写文档](files/10041_编写文档.md?t=1564027102063) - * [接口交互详解](files/10050_接口交互详解.md?t=1564027102063) - * [easyopen支持](files/10070_easyopen支持.md?t=1564027102063) - * [使用签名校验工具](files/10080_使用签名校验工具.md?t=1564027102063) - * [ISV管理](files/10085_ISV管理.md?t=1564027102063) - * [自定义路由](files/10086_自定义路由.md?t=1564027102063) - * [路由授权](files/10090_路由授权.md?t=1564027102063) - * [接口限流](files/10092_接口限流.md?t=1564027102063) - * [监控日志](files/10093_监控日志.md?t=1564027102063) - * [SDK开发](files/10095_SDK开发.md?t=1564027102063) - * [使用SpringCloudGateway](files/10096_使用SpringCloudGateway.md?t=1564027102064) - * [应用授权](files/10097_应用授权.md?t=1564027102064) - * [更改数据节点名称](files/10099_更改数据节点名称.md?t=1564027102064) - * [传统web开发](files/10100_传统web开发.md?t=1564027102064) - * [自定义过滤器](files/10102_自定义过滤器.md?t=1564027102064) - * [文件上传](files/10104_文件上传.md?t=1564027102064) - * [nacos注册中心](files/10106_nacos注册中心.md?t=1564027102064) - * [扩展其它注册中心](files/10107_扩展其它注册中心.md?t=1564027102064) - * [配置Sleuth链路追踪](files/10109_配置Sleuth链路追踪.md?t=1564027102064) + * [快速体验](files/10010_快速体验.md?t=1564057981508) + * [项目接入到SOP](files/10011_项目接入到SOP.md?t=1564057981525) + * [新增接口](files/10020_新增接口.md?t=1564057981525) + * [业务参数校验](files/10030_业务参数校验.md?t=1564057981525) + * [错误处理](files/10040_错误处理.md?t=1564057981525) + * [编写文档](files/10041_编写文档.md?t=1564057981526) + * [接口交互详解](files/10050_接口交互详解.md?t=1564057981526) + * [easyopen支持](files/10070_easyopen支持.md?t=1564057981526) + * [使用签名校验工具](files/10080_使用签名校验工具.md?t=1564057981526) + * [ISV管理](files/10085_ISV管理.md?t=1564057981526) + * [自定义路由](files/10086_自定义路由.md?t=1564057981526) + * [路由授权](files/10090_路由授权.md?t=1564057981526) + * [接口限流](files/10092_接口限流.md?t=1564057981526) + * [监控日志](files/10093_监控日志.md?t=1564057981526) + * [SDK开发](files/10095_SDK开发.md?t=1564057981526) + * [使用SpringCloudGateway](files/10096_使用SpringCloudGateway.md?t=1564057981526) + * [应用授权](files/10097_应用授权.md?t=1564057981527) + * [更改数据节点名称](files/10099_更改数据节点名称.md?t=1564057981527) + * [传统web开发](files/10100_传统web开发.md?t=1564057981527) + * [自定义过滤器](files/10102_自定义过滤器.md?t=1564057981527) + * [文件上传](files/10104_文件上传.md?t=1564057981527) + * [nacos注册中心](files/10106_nacos注册中心.md?t=1564057981527) + * [扩展其它注册中心](files/10107_扩展其它注册中心.md?t=1564057981527) + * [配置Sleuth链路追踪](files/10109_配置Sleuth链路追踪.md?t=1564057981527) * 原理分析 - * [原理分析之@ApiMapping](files/90010_原理分析之@ApiMapping.md?t=1564027102064) - * [原理分析之路由存储](files/90011_原理分析之路由存储.md?t=1564027102064) - * [原理分析之如何路由](files/90012_原理分析之如何路由.md?t=1564027102064) - * [原理分析之文档归纳](files/90013_原理分析之文档归纳.md?t=1564027102064) - * [常见问题](files/90100_常见问题.md?t=1564027102065) + * [原理分析之@ApiMapping](files/90010_原理分析之@ApiMapping.md?t=1564057981527) + * [原理分析之路由存储](files/90011_原理分析之路由存储.md?t=1564057981527) + * [原理分析之如何路由](files/90012_原理分析之如何路由.md?t=1564057981528) + * [原理分析之文档归纳](files/90013_原理分析之文档归纳.md?t=1564057981528) + * [常见问题](files/90100_常见问题.md?t=1564057981528) diff --git a/doc/docs/files/90100_常见问题.md b/doc/docs/files/90100_常见问题.md index b1177e01..8def132e 100644 --- a/doc/docs/files/90100_常见问题.md +++ b/doc/docs/files/90100_常见问题.md @@ -1,5 +1,12 @@ # 常见问题 +## 微服务端如何获取appId等参数 + +```java +OpenContext openContext = ServiceContext.getCurrentContext().getOpenContext(); +String appId = openContext.getAppId(); +``` + ## Socket error occurred: `localhost/0:0:0:0:0:0:0:1:2181`: Connection refused 检查本地zookeeper有没启动,如果zookeeper在其他机器上,修改application-dev.yml diff --git a/sop-admin/sop-admin-server/pom.xml b/sop-admin/sop-admin-server/pom.xml index d91702f7..2ee0eb82 100644 --- a/sop-admin/sop-admin-server/pom.xml +++ b/sop-admin/sop-admin-server/pom.xml @@ -30,7 +30,7 @@ com.gitee.sop sop-registry-api - 1.13.1-SNAPSHOT + 1.13.2-SNAPSHOT diff --git a/sop-common/pom.xml b/sop-common/pom.xml index 1d079bdd..4048a6c2 100644 --- a/sop-common/pom.xml +++ b/sop-common/pom.xml @@ -5,7 +5,7 @@ 4.0.0 com.gitee.sop sop-common - 1.13.1-SNAPSHOT + 1.13.2-SNAPSHOT pom diff --git a/sop-common/sop-gateway-common/pom.xml b/sop-common/sop-gateway-common/pom.xml index 59d171c4..de238aee 100644 --- a/sop-common/sop-gateway-common/pom.xml +++ b/sop-common/sop-gateway-common/pom.xml @@ -5,11 +5,11 @@ com.gitee.sop sop-common - 1.13.1-SNAPSHOT + 1.13.2-SNAPSHOT ../pom.xml sop-gateway-common - 1.13.1-SNAPSHOT + 1.13.2-SNAPSHOT jar sop-gateway-common diff --git a/sop-common/sop-registry-api/pom.xml b/sop-common/sop-registry-api/pom.xml index 00c4191c..2e96c9ea 100644 --- a/sop-common/sop-registry-api/pom.xml +++ b/sop-common/sop-registry-api/pom.xml @@ -5,7 +5,7 @@ 4.0.0 com.gitee.sop sop-registry-api - 1.13.1-SNAPSHOT + 1.13.2-SNAPSHOT UTF-8 diff --git a/sop-common/sop-service-common/pom.xml b/sop-common/sop-service-common/pom.xml index a0818acb..27d89f62 100644 --- a/sop-common/sop-service-common/pom.xml +++ b/sop-common/sop-service-common/pom.xml @@ -6,11 +6,11 @@ com.gitee.sop sop-common - 1.13.1-SNAPSHOT + 1.13.2-SNAPSHOT ../pom.xml sop-service-common - 1.13.1-SNAPSHOT + 1.13.2-SNAPSHOT jar sop-service-common diff --git a/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/bean/OpenContext.java b/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/bean/OpenContext.java new file mode 100644 index 00000000..20134dfd --- /dev/null +++ b/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/bean/OpenContext.java @@ -0,0 +1,74 @@ +package com.gitee.sop.servercommon.bean; + +import java.util.Date; + +/** + * 获取开放平台请求参数。 + * 可当做接口方法参数,泛型参数填业务参数类,如: + *
+ * @ApiMapping(value = "alipay.goods.get")
+ * public Goods getGoods(OpenRequest<GoodsParam> openRequest){...} 
+ *
+ * 可通过OpenRequest openRequest = ServiceContext.getCurrentContext().getOpenRequest();方式进行获取 + * OpenContext + * OpenParam + * @author tanghc + * + */ +public interface OpenContext { + + /** + * 返回appid + * @return 返回appid + */ + String getAppId(); + + /** + * 返回业务参数json字符串 + * @return 返回字符串业务参数 + */ + String getBizContent(); + + /** + * 返回业务对象 + * @return 业务对象 + */ + T getBizObject(); + + /** + * 返回字符编码 + * @return 如UTF-8 + */ + String getCharset(); + + /** + * 返回接口名 + * @return 如:alipay.goods.get + */ + String getMethod(); + + /** + * 返回版本号 + * @return 如:1.0 + */ + String getVersion(); + + /** + * 返回参数格式化 + * @return 如:json + */ + String getFormat(); + + /** + * 返回签名类型 + * @return 如:RSA2 + */ + String getSignType(); + + /** + * 返回时间戳 + * @return + */ + Date getTimestamp(); + +} diff --git a/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/bean/OpenContextImpl.java b/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/bean/OpenContextImpl.java new file mode 100644 index 00000000..138a4082 --- /dev/null +++ b/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/bean/OpenContextImpl.java @@ -0,0 +1,73 @@ +package com.gitee.sop.servercommon.bean; + +import com.alibaba.fastjson.JSONObject; + +import java.util.Date; + +import static com.gitee.sop.servercommon.bean.ParamNames.API_NAME; +import static com.gitee.sop.servercommon.bean.ParamNames.APP_KEY_NAME; +import static com.gitee.sop.servercommon.bean.ParamNames.BIZ_CONTENT_NAME; +import static com.gitee.sop.servercommon.bean.ParamNames.CHARSET_NAME; +import static com.gitee.sop.servercommon.bean.ParamNames.FORMAT_NAME; +import static com.gitee.sop.servercommon.bean.ParamNames.SIGN_TYPE_NAME; +import static com.gitee.sop.servercommon.bean.ParamNames.TIMESTAMP_NAME; +import static com.gitee.sop.servercommon.bean.ParamNames.VERSION_NAME; + +/** + * @author tanghc + */ +public class OpenContextImpl implements OpenContext { + private JSONObject jsonObject; + private T bizObject; + + public OpenContextImpl(JSONObject jsonObject, Class bizClass) { + this.jsonObject = jsonObject; + JSONObject bizJsonObj = this.jsonObject.getJSONObject(BIZ_CONTENT_NAME); + bizObject = (bizClass == null || bizJsonObj == null) ? null : (T) bizJsonObj.toJavaObject(bizClass); + } + + @Override + public String getAppId() { + return jsonObject.getString(APP_KEY_NAME); + } + + @Override + public T getBizObject() { + return bizObject; + } + + @Override + public String getBizContent() { + return jsonObject.getString(BIZ_CONTENT_NAME); + } + + @Override + public String getCharset() { + return jsonObject.getString(CHARSET_NAME); + } + + @Override + public String getMethod() { + return jsonObject.getString(API_NAME); + } + + @Override + public String getVersion() { + return jsonObject.getString(VERSION_NAME); + } + + @Override + public String getFormat() { + return jsonObject.getString(FORMAT_NAME); + } + + @Override + public String getSignType() { + return jsonObject.getString(SIGN_TYPE_NAME); + } + + @Override + public Date getTimestamp() { + return jsonObject.getDate(TIMESTAMP_NAME); + } +} diff --git a/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/bean/ServiceContext.java b/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/bean/ServiceContext.java index 73877b2d..41e22ee5 100644 --- a/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/bean/ServiceContext.java +++ b/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/bean/ServiceContext.java @@ -12,6 +12,7 @@ public class ServiceContext extends ConcurrentHashMap { public static final String REQUEST_KEY = "request"; public static final String RESPONSE_KEY = "response"; + public static final String OPEN_CONTEXT_KEY = "openContext"; protected static Class contextClass = ServiceContext.class; protected static final ThreadLocal THREAD_LOCAL = new ThreadLocal() { @@ -30,6 +31,14 @@ public class ServiceContext extends ConcurrentHashMap { super(); } + public void setOpenContext(OpenContext openContext) { + this.set(OPEN_CONTEXT_KEY, openContext); + } + + public OpenContext getOpenContext() { + return (OpenContext) this.get(OPEN_CONTEXT_KEY); + } + public Locale getLocale() { HttpServletRequest request = getRequest(); if (request == null) { 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 ddf3d09b..38982d29 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 @@ -1,9 +1,13 @@ package com.gitee.sop.servercommon.param; -import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONObject; import com.gitee.sop.servercommon.annotation.ApiAbility; import com.gitee.sop.servercommon.annotation.ApiMapping; -import com.gitee.sop.servercommon.bean.ParamNames; +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 lombok.extern.slf4j.Slf4j; import org.springframework.core.MethodParameter; import org.springframework.util.ReflectionUtils; import org.springframework.web.bind.support.WebDataBinderFactory; @@ -14,6 +18,9 @@ import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartHttpServletRequest; import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter; +import javax.servlet.http.HttpServletRequest; +import java.lang.reflect.ParameterizedType; +import java.lang.reflect.Type; import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; @@ -23,6 +30,7 @@ import java.util.concurrent.ConcurrentHashMap; * * @author tanghc */ +@Slf4j public class ApiArgumentResolver implements SopHandlerMethodArgumentResolver { private final Map argumentResolverCache = new ConcurrentHashMap<>(256); @@ -69,14 +77,38 @@ public class ApiArgumentResolver implements SopHandlerMethodArgumentResolver { * @return 没有返回null */ protected Object getParamObject(MethodParameter methodParameter, NativeWebRequest nativeWebRequest) { - String bizContent = nativeWebRequest.getParameter(ParamNames.BIZ_CONTENT_NAME); - Object bizObj = null; - if (bizContent != null) { - Class parameterType = methodParameter.getParameterType(); - bizObj = JSON.parseObject(bizContent, parameterType); + HttpServletRequest request = (HttpServletRequest) nativeWebRequest.getNativeRequest(); + JSONObject requestParams = OpenUtil.getRequestParams(request); + Class parameterType = methodParameter.getParameterType(); + // 方法参数类型 + Class bizObjClass = parameterType; + boolean isOpenRequestParam = parameterType == OpenContext.class; + // 参数是OpenRequest,则取OpenRequest的泛型参数类型 + if (isOpenRequestParam) { + bizObjClass = this.getOpenRequestGenericParameterClass(methodParameter); } + OpenContext openContext = new OpenContextImpl(requestParams, bizObjClass); + ServiceContext.getCurrentContext().setOpenContext(openContext); + Object bizObj = openContext.getBizObject(); this.bindUploadFile(bizObj, nativeWebRequest); - return bizObj; + return isOpenRequestParam ? openContext : bizObj; + } + + /** + * 获取泛型参数类型 + * @param methodParameter 参数 + * @return 返回泛型参数class + */ + protected Class getOpenRequestGenericParameterClass(MethodParameter methodParameter) { + Type genericParameterType = methodParameter.getGenericParameterType(); + Class bizObjClass = null; + if (genericParameterType instanceof ParameterizedType) { + Type[] params = ((ParameterizedType) genericParameterType).getActualTypeArguments(); + if (params != null && params.length >= 1) { + bizObjClass = (Class) params[0]; + } + } + return bizObjClass; } /** diff --git a/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/util/OpenUtil.java b/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/util/OpenUtil.java new file mode 100644 index 00000000..10f590c1 --- /dev/null +++ b/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/util/OpenUtil.java @@ -0,0 +1,85 @@ +package com.gitee.sop.servercommon.util; + +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONObject; +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.io.IOUtils; +import org.apache.commons.lang3.StringUtils; + +import javax.servlet.http.HttpServletRequest; +import java.io.IOException; +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; +import java.util.Set; + +/** + * @author tanghc + */ +@Slf4j +public class OpenUtil { + + private static final String CONTENT_TYPE_URLENCODED = "application/x-www-form-urlencoded"; + private static final String CONTENT_TYPE_JSON = "application/json"; + private static final String CONTENT_TYPE_TEXT = "text/plain"; + + private static final String UTF8 = "UTF-8"; + + /** + * 从request中获取参数。如果提交方式是application/x-www-form-urlencoded,则组装成json格式。 + * + * @param request request对象 + * @return 返回json + * @throws IOException + */ + public static JSONObject getRequestParams(HttpServletRequest request) { + String requestJson = "{}"; + String contentType = request.getContentType(); + if (StringUtils.isBlank(contentType)) { + return new JSONObject(); + } + contentType = contentType.toLowerCase(); + if (StringUtils.containsAny(contentType, CONTENT_TYPE_JSON, CONTENT_TYPE_TEXT)) { + try { + requestJson = IOUtils.toString(request.getInputStream(), UTF8); + } catch (IOException e) { + log.error("获取文本数据流失败", e); + } + } else if (StringUtils.containsAny(contentType, CONTENT_TYPE_URLENCODED)) { + Map params = convertRequestParamsToMap(request); + requestJson = JSON.toJSONString(params); + } + return JSON.parseObject(requestJson); + } + + /** + * request中的参数转换成map + * + * @param request request对象 + * @return 返回参数键值对 + */ + public static Map convertRequestParamsToMap(HttpServletRequest request) { + Map paramMap = request.getParameterMap(); + if(paramMap == null || paramMap.isEmpty()) { + return Collections.emptyMap(); + } + Map retMap = new HashMap(paramMap.size()); + + Set> entrySet = paramMap.entrySet(); + + for (Map.Entry entry : entrySet) { + String name = entry.getKey(); + String[] values = entry.getValue(); + if (values.length == 1) { + retMap.put(name, values[0]); + } else if (values.length > 1) { + retMap.put(name, values); + } else { + retMap.put(name, ""); + } + } + + return retMap; + } + +} diff --git a/sop-example/sop-auth/pom.xml b/sop-example/sop-auth/pom.xml index 0baa41b7..f705c20e 100644 --- a/sop-example/sop-auth/pom.xml +++ b/sop-example/sop-auth/pom.xml @@ -26,7 +26,7 @@ com.gitee.sop sop-service-common - 1.13.1-SNAPSHOT + 1.13.2-SNAPSHOT org.springframework.cloud diff --git a/sop-example/sop-book/sop-book-web/pom.xml b/sop-example/sop-book/sop-book-web/pom.xml index e758cd4a..a01339ba 100644 --- a/sop-example/sop-book/sop-book-web/pom.xml +++ b/sop-example/sop-book/sop-book-web/pom.xml @@ -24,7 +24,7 @@ com.gitee.sop sop-service-common - 1.13.1-SNAPSHOT + 1.13.2-SNAPSHOT com.gitee.sop diff --git a/sop-example/sop-easyopen/pom.xml b/sop-example/sop-easyopen/pom.xml index f90b7570..9198b804 100644 --- a/sop-example/sop-easyopen/pom.xml +++ b/sop-example/sop-easyopen/pom.xml @@ -29,7 +29,7 @@ com.gitee.sop sop-service-common - 1.13.1-SNAPSHOT + 1.13.2-SNAPSHOT org.springframework.cloud diff --git a/sop-example/sop-springmvc/pom.xml b/sop-example/sop-springmvc/pom.xml index 5502e361..b7ad30f7 100644 --- a/sop-example/sop-springmvc/pom.xml +++ b/sop-example/sop-springmvc/pom.xml @@ -20,7 +20,7 @@ com.gitee.sop sop-service-common - 1.13.1-SNAPSHOT + 1.13.2-SNAPSHOT diff --git a/sop-example/sop-story/sop-story-web/pom.xml b/sop-example/sop-story/sop-story-web/pom.xml index 9ab14a4c..1f09cd60 100644 --- a/sop-example/sop-story/sop-story-web/pom.xml +++ b/sop-example/sop-story/sop-story-web/pom.xml @@ -24,7 +24,7 @@ com.gitee.sop sop-service-common - 1.13.1-SNAPSHOT + 1.13.2-SNAPSHOT com.gitee.sop diff --git a/sop-example/sop-story/sop-story-web/src/main/java/com/gitee/sop/storyweb/controller/AlipayController.java b/sop-example/sop-story/sop-story-web/src/main/java/com/gitee/sop/storyweb/controller/AlipayController.java index 5ea2f8ab..e15f79e8 100644 --- a/sop-example/sop-story/sop-story-web/src/main/java/com/gitee/sop/storyweb/controller/AlipayController.java +++ b/sop-example/sop-story/sop-story-web/src/main/java/com/gitee/sop/storyweb/controller/AlipayController.java @@ -2,6 +2,8 @@ package com.gitee.sop.storyweb.controller; import com.gitee.sop.servercommon.annotation.ApiAbility; import com.gitee.sop.servercommon.annotation.ApiMapping; +import com.gitee.sop.servercommon.bean.OpenContext; +import com.gitee.sop.servercommon.bean.ServiceContext; import com.gitee.sop.story.api.domain.Story; import com.gitee.sop.storyweb.controller.param.StoryParam; import io.swagger.annotations.Api; @@ -14,7 +16,6 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; -import javax.servlet.http.HttpServletRequest; import java.util.Date; /** @@ -97,15 +98,15 @@ public class AlipayController { /** * 参数绑定 * @param story 对应biz_content中的内容,并自动JSR-303校验 - * @param request * @return */ @ApiOperation(value = "获取故事信息", notes = "说明接口的详细信息,介绍,用途,注意事项等。") @ApiMapping(value = "alipay.story.find") - public StoryVO getStory2(StoryParam story, HttpServletRequest request) { + public StoryVO getStory2(StoryParam story) { log.info("获取故事信息参数, story: {}", story); // 获取其它参数 - String app_id = request.getParameter("app_id"); + OpenContext openContext = ServiceContext.getCurrentContext().getOpenContext(); + String app_id = openContext.getAppId(); StoryVO storyVO = new StoryVO(); storyVO.id = 1L; storyVO.name = "白雪公主, app_id:" + app_id; diff --git a/sop-example/sop-story/sop-story-web/src/main/java/com/gitee/sop/storyweb/controller/PostJsonController.java b/sop-example/sop-story/sop-story-web/src/main/java/com/gitee/sop/storyweb/controller/PostJsonController.java new file mode 100644 index 00000000..0bc5e45a --- /dev/null +++ b/sop-example/sop-story/sop-story-web/src/main/java/com/gitee/sop/storyweb/controller/PostJsonController.java @@ -0,0 +1,41 @@ +package com.gitee.sop.storyweb.controller; + +import com.gitee.sop.servercommon.annotation.ApiMapping; +import com.gitee.sop.servercommon.bean.ServiceContext; +import com.gitee.sop.servercommon.bean.OpenContext; +import com.gitee.sop.story.api.domain.Story; +import com.gitee.sop.storyweb.controller.param.StoryParam; +import org.apache.commons.lang3.StringUtils; +import org.springframework.web.bind.annotation.RestController; + +import java.util.Arrays; +import java.util.List; + +/** + * @author tanghc + */ +@RestController +public class PostJsonController { + + /** + * 演示客户端使用json方式请求(application/json) + * @param param + * @return + */ + @ApiMapping("demo.post.json") + public Story postJson(StoryParam param) { + // 获取开放平台请求参数 + OpenContext openContext = ServiceContext.getCurrentContext().getOpenContext(); + List params = Arrays.asList( + openContext.getAppId(), + openContext.getMethod(), + openContext.getVersion() + ); + Story story = new Story(); + story.setId(1); + story.setName("参数:" + param.getName() + ", openParams:" + StringUtils.join(params)); + return story; + } + + +} diff --git a/sop-gateway/pom.xml b/sop-gateway/pom.xml index 6767b266..cd7f594d 100644 --- a/sop-gateway/pom.xml +++ b/sop-gateway/pom.xml @@ -23,7 +23,7 @@ com.gitee.sop sop-gateway-common - 1.13.1-SNAPSHOT + 1.13.2-SNAPSHOT diff --git a/sop-sdk/sdk-csharp/SDKCSharp/Client/OpenRequest.cs b/sop-sdk/sdk-csharp/SDKCSharp/Client/OpenRequest.cs index 6974a405..922c5091 100644 --- a/sop-sdk/sdk-csharp/SDKCSharp/Client/OpenRequest.cs +++ b/sop-sdk/sdk-csharp/SDKCSharp/Client/OpenRequest.cs @@ -49,7 +49,7 @@ namespace SDKCSharp.Client } return openHttp.Get(url, header); } - return this.openHttp.RequestFormBody(url, form, header); + return this.openHttp.PostJsonBody(url, JsonUtil.ToJSONString(form), header); } } diff --git a/sop-sdk/sdk-java/src/main/java/com/gitee/sop/sdk/client/OpenRequest.java b/sop-sdk/sdk-java/src/main/java/com/gitee/sop/sdk/client/OpenRequest.java index 4ce3b2b3..c04f2792 100644 --- a/sop-sdk/sdk-java/src/main/java/com/gitee/sop/sdk/client/OpenRequest.java +++ b/sop-sdk/sdk-java/src/main/java/com/gitee/sop/sdk/client/OpenRequest.java @@ -42,7 +42,7 @@ public class OpenRequest { } return openHttp.get(url, header); } else { - return openHttp.request(url, form, header, requestMethod.name()); + return openHttp.requestJson(url, JSON.toJSONString(form), header); } } } catch (IOException e) { diff --git a/sop-test/src/test/java/com/gitee/sop/test/PostJsonTest.java b/sop-test/src/test/java/com/gitee/sop/test/PostJsonTest.java new file mode 100644 index 00000000..308ef486 --- /dev/null +++ b/sop-test/src/test/java/com/gitee/sop/test/PostJsonTest.java @@ -0,0 +1,56 @@ +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; + +/** + * 以json方式请求(application/json) + */ +public class PostJsonTest extends TestBase { + + String url = "http://localhost:8081/api"; // zuul + String appId = "2019032617262200001"; + // 支付宝私钥 + String privateKey = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCXJv1pQFqWNA/++OYEV7WYXwexZK/J8LY1OWlP9X0T6wHFOvxNKRvMkJ5544SbgsJpVcvRDPrcxmhPbi/sAhdO4x2PiPKIz9Yni2OtYCCeaiE056B+e1O2jXoLeXbfi9fPivJZkxH/tb4xfLkH3bA8ZAQnQsoXA0SguykMRZntF0TndUfvDrLqwhlR8r5iRdZLB6F8o8qXH6UPDfNEnf/K8wX5T4EB1b8x8QJ7Ua4GcIUqeUxGHdQpzNbJdaQvoi06lgccmL+PHzminkFYON7alj1CjDN833j7QMHdPtS9l7B67fOU/p2LAAkPMtoVBfxQt9aFj7B8rEhGCz02iJIBAgMBAAECggEARqOuIpY0v6WtJBfmR3lGIOOokLrhfJrGTLF8CiZMQha+SRJ7/wOLPlsH9SbjPlopyViTXCuYwbzn2tdABigkBHYXxpDV6CJZjzmRZ+FY3S/0POlTFElGojYUJ3CooWiVfyUMhdg5vSuOq0oCny53woFrf32zPHYGiKdvU5Djku1onbDU0Lw8w+5tguuEZ76kZ/lUcccGy5978FFmYpzY/65RHCpvLiLqYyWTtaNT1aQ/9pw4jX9HO9NfdJ9gYFK8r/2f36ZE4hxluAfeOXQfRC/WhPmiw/ReUhxPznG/WgKaa/OaRtAx3inbQ+JuCND7uuKeRe4osP2jLPHPP6AUwQKBgQDUNu3BkLoKaimjGOjCTAwtp71g1oo+k5/uEInAo7lyEwpV0EuUMwLA/HCqUgR4K9pyYV+Oyb8d6f0+Hz0BMD92I2pqlXrD7xV2WzDvyXM3s63NvorRooKcyfd9i6ccMjAyTR2qfLkxv0hlbBbsPHz4BbU63xhTJp3Ghi0/ey/1HQKBgQC2VsgqC6ykfSidZUNLmQZe3J0p/Qf9VLkfrQ+xaHapOs6AzDU2H2osuysqXTLJHsGfrwVaTs00ER2z8ljTJPBUtNtOLrwNRlvgdnzyVAKHfOgDBGwJgiwpeE9voB1oAV/mXqSaUWNnuwlOIhvQEBwekqNyWvhLqC7nCAIhj3yvNQKBgQCqYbeec56LAhWP903Zwcj9VvG7sESqXUhIkUqoOkuIBTWFFIm54QLTA1tJxDQGb98heoCIWf5x/A3xNI98RsqNBX5JON6qNWjb7/dobitti3t99v/ptDp9u8JTMC7penoryLKK0Ty3bkan95Kn9SC42YxaSghzqkt+uvfVQgiNGQKBgGxU6P2aDAt6VNwWosHSe+d2WWXt8IZBhO9d6dn0f7ORvcjmCqNKTNGgrkewMZEuVcliueJquR47IROdY8qmwqcBAN7Vg2K7r7CPlTKAWTRYMJxCT1Hi5gwJb+CZF3+IeYqsJk2NF2s0w5WJTE70k1BSvQsfIzAIDz2yE1oPHvwVAoGAA6e+xQkVH4fMEph55RJIZ5goI4Y76BSvt2N5OKZKd4HtaV+eIhM3SDsVYRLIm9ZquJHMiZQGyUGnsvrKL6AAVNK7eQZCRDk9KQz+0GKOGqku0nOZjUbAu6A2/vtXAaAuFSFx1rUQVVjFulLexkXR3KcztL1Qu2k5pB6Si0K/uwQ="; + + @Test + public void testPostJson() throws Exception { + + // 公共请求参数 + Map params = new HashMap(); + params.put("app_id", appId); + params.put("method", "demo.post.json"); + 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"); + + // 业务参数 + Map bizContent = new HashMap<>(); + bizContent.put("name", "葫芦娃"); + + params.put("biz_content", JSON.toJSONString(bizContent)); + + System.out.println("----------- 请求信息 -----------"); + System.out.println("请求参数:" + buildParamQuery(params)); + System.out.println("商户秘钥:" + privateKey); + String content = AlipaySignature.getSignContent(params); + System.out.println("待签名内容:" + content); + String sign = AlipaySignature.rsa256Sign(content, privateKey, "utf-8"); + System.out.println("签名(sign):" + sign); + + params.put("sign", sign); + + System.out.println("----------- 返回结果 -----------"); + String responseData = get(url, params);// 发送请求 + System.out.println(responseData); + } + +} diff --git a/sop-test/src/test/java/com/gitee/sop/test/TestBase.java b/sop-test/src/test/java/com/gitee/sop/test/TestBase.java index fb96518e..bc34209c 100644 --- a/sop-test/src/test/java/com/gitee/sop/test/TestBase.java +++ b/sop-test/src/test/java/com/gitee/sop/test/TestBase.java @@ -21,6 +21,14 @@ public class TestBase extends TestCase { * @return JSON或者字符串 */ public static String post(String url, Map params) { + try { + return httpTool.request(url, params, Collections.emptyMap(), HttpTool.HTTPMethod.POST); + } catch (IOException e) { + throw new RuntimeException("网络请求异常", e); + } + } + + public static String postJson(String url, Map params) { try { return httpTool.requestJson(url, JSON.toJSONString(params), Collections.emptyMap()); } catch (IOException e) { diff --git a/sop-website/website-server/pom.xml b/sop-website/website-server/pom.xml index d6ae3866..8e0d2c59 100644 --- a/sop-website/website-server/pom.xml +++ b/sop-website/website-server/pom.xml @@ -25,7 +25,7 @@ com.gitee.sop sop-registry-api - 1.13.1-SNAPSHOT + 1.13.2-SNAPSHOT