tanghc 5 years ago
parent bf49c68286
commit 9f78eceded
  1. 5
      changelog.md
  2. 60
      doc/docs/_sidebar.md
  3. 7
      doc/docs/files/90100_常见问题.md
  4. 2
      sop-admin/sop-admin-server/pom.xml
  5. 2
      sop-common/pom.xml
  6. 4
      sop-common/sop-gateway-common/pom.xml
  7. 2
      sop-common/sop-registry-api/pom.xml
  8. 4
      sop-common/sop-service-common/pom.xml
  9. 74
      sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/bean/OpenContext.java
  10. 73
      sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/bean/OpenContextImpl.java
  11. 9
      sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/bean/ServiceContext.java
  12. 48
      sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/param/ApiArgumentResolver.java
  13. 85
      sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/util/OpenUtil.java
  14. 2
      sop-example/sop-auth/pom.xml
  15. 2
      sop-example/sop-book/sop-book-web/pom.xml
  16. 2
      sop-example/sop-easyopen/pom.xml
  17. 2
      sop-example/sop-springmvc/pom.xml
  18. 2
      sop-example/sop-story/sop-story-web/pom.xml
  19. 9
      sop-example/sop-story/sop-story-web/src/main/java/com/gitee/sop/storyweb/controller/AlipayController.java
  20. 41
      sop-example/sop-story/sop-story-web/src/main/java/com/gitee/sop/storyweb/controller/PostJsonController.java
  21. 2
      sop-gateway/pom.xml
  22. 2
      sop-sdk/sdk-csharp/SDKCSharp/Client/OpenRequest.cs
  23. 2
      sop-sdk/sdk-java/src/main/java/com/gitee/sop/sdk/client/OpenRequest.java
  24. 56
      sop-test/src/test/java/com/gitee/sop/test/PostJsonTest.java
  25. 8
      sop-test/src/test/java/com/gitee/sop/test/TestBase.java
  26. 2
      sop-website/website-server/pom.xml

@ -1,5 +1,10 @@
# changelog
## 1.13.2
- 修复json方式请求获取不到参数问题
- 微服务端新增获取开放平台请求参数
## 1.13.1
- 支持json方式请求(application/json)

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

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

@ -30,7 +30,7 @@
<dependency>
<groupId>com.gitee.sop</groupId>
<artifactId>sop-registry-api</artifactId>
<version>1.13.1-SNAPSHOT</version>
<version>1.13.2-SNAPSHOT</version>
</dependency>
<!-- easyopen starter -->

@ -5,7 +5,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.gitee.sop</groupId>
<artifactId>sop-common</artifactId>
<version>1.13.1-SNAPSHOT</version>
<version>1.13.2-SNAPSHOT</version>
<packaging>pom</packaging>
<properties>

@ -5,11 +5,11 @@
<parent>
<groupId>com.gitee.sop</groupId>
<artifactId>sop-common</artifactId>
<version>1.13.1-SNAPSHOT</version>
<version>1.13.2-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>sop-gateway-common</artifactId>
<version>1.13.1-SNAPSHOT</version>
<version>1.13.2-SNAPSHOT</version>
<packaging>jar</packaging>
<name>sop-gateway-common</name>

@ -5,7 +5,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.gitee.sop</groupId>
<artifactId>sop-registry-api</artifactId>
<version>1.13.1-SNAPSHOT</version>
<version>1.13.2-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

@ -6,11 +6,11 @@
<parent>
<groupId>com.gitee.sop</groupId>
<artifactId>sop-common</artifactId>
<version>1.13.1-SNAPSHOT</version>
<version>1.13.2-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>sop-service-common</artifactId>
<version>1.13.1-SNAPSHOT</version>
<version>1.13.2-SNAPSHOT</version>
<packaging>jar</packaging>
<name>sop-service-common</name>

@ -0,0 +1,74 @@
package com.gitee.sop.servercommon.bean;
import java.util.Date;
/**
* 获取开放平台请求参数
* 可当做接口方法参数泛型参数填业务参数类:
* <pre>
* <code>@ApiMapping(value = "alipay.goods.get")</code>
* public Goods getGoods(OpenRequest&lt;GoodsParam&gt; openRequest){...}</code> <br/>
* </pre>
* 可通过<code>OpenRequest openRequest = ServiceContext.getCurrentContext().getOpenRequest();</code>方式进行获取
* OpenContext
* OpenParam
* @author tanghc
*
*/
public interface OpenContext<T> {
/**
* 返回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();
}

@ -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<T> implements OpenContext<T> {
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);
}
}

@ -12,6 +12,7 @@ public class ServiceContext extends ConcurrentHashMap<String, Object> {
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<? extends ServiceContext> contextClass = ServiceContext.class;
protected static final ThreadLocal<? extends ServiceContext> THREAD_LOCAL = new ThreadLocal<ServiceContext>() {
@ -30,6 +31,14 @@ public class ServiceContext extends ConcurrentHashMap<String, Object> {
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) {

@ -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<MethodParameter, HandlerMethodArgumentResolver> 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;
}
/**

@ -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<String, Object> params = convertRequestParamsToMap(request);
requestJson = JSON.toJSONString(params);
}
return JSON.parseObject(requestJson);
}
/**
* request中的参数转换成map
*
* @param request request对象
* @return 返回参数键值对
*/
public static Map<String, Object> convertRequestParamsToMap(HttpServletRequest request) {
Map<String, String[]> paramMap = request.getParameterMap();
if(paramMap == null || paramMap.isEmpty()) {
return Collections.emptyMap();
}
Map<String, Object> retMap = new HashMap<String, Object>(paramMap.size());
Set<Map.Entry<String, String[]>> entrySet = paramMap.entrySet();
for (Map.Entry<String, String[]> 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;
}
}

@ -26,7 +26,7 @@
<dependency>
<groupId>com.gitee.sop</groupId>
<artifactId>sop-service-common</artifactId>
<version>1.13.1-SNAPSHOT</version>
<version>1.13.2-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>

@ -24,7 +24,7 @@
<dependency>
<groupId>com.gitee.sop</groupId>
<artifactId>sop-service-common</artifactId>
<version>1.13.1-SNAPSHOT</version>
<version>1.13.2-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.gitee.sop</groupId>

@ -29,7 +29,7 @@
<dependency>
<groupId>com.gitee.sop</groupId>
<artifactId>sop-service-common</artifactId>
<version>1.13.1-SNAPSHOT</version>
<version>1.13.2-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>

@ -20,7 +20,7 @@
<dependency>
<groupId>com.gitee.sop</groupId>
<artifactId>sop-service-common</artifactId>
<version>1.13.1-SNAPSHOT</version>
<version>1.13.2-SNAPSHOT</version>
</dependency>
<!-- eureka 服务发现 -->
<dependency>

@ -24,7 +24,7 @@
<dependency>
<groupId>com.gitee.sop</groupId>
<artifactId>sop-service-common</artifactId>
<version>1.13.1-SNAPSHOT</version>
<version>1.13.2-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.gitee.sop</groupId>

@ -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;

@ -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<Object> 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;
}
}

@ -23,7 +23,7 @@
<dependency>
<groupId>com.gitee.sop</groupId>
<artifactId>sop-gateway-common</artifactId>
<version>1.13.1-SNAPSHOT</version>
<version>1.13.2-SNAPSHOT</version>
</dependency>
<!-- ↓↓↓ 使用spring cloud zuul ↓↓↓ -->

@ -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);
}
}

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

@ -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<String, String> params = new HashMap<String, String>();
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<String, String> 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);
}
}

@ -21,6 +21,14 @@ public class TestBase extends TestCase {
* @return JSON或者字符串
*/
public static String post(String url, Map<String, String> 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<String, String> params) {
try {
return httpTool.requestJson(url, JSON.toJSONString(params), Collections.emptyMap());
} catch (IOException e) {

@ -25,7 +25,7 @@
<dependency>
<groupId>com.gitee.sop</groupId>
<artifactId>sop-registry-api</artifactId>
<version>1.13.1-SNAPSHOT</version>
<version>1.13.2-SNAPSHOT</version>
</dependency>
<dependency>

Loading…
Cancel
Save