Merge branch 'master' into registry-nacos

pull/1/head
tanghc 5 years ago
commit 753fbc651a
  1. 60
      doc/docs/_sidebar.md
  2. 27
      doc/docs/files/10020_新增接口.md
  3. 7
      doc/docs/files/90100_常见问题.md
  4. 16
      sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/bean/OpenBeanFactory.java
  5. 15
      sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/bean/OpenContext.java
  6. 12
      sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/bean/OpenContextImpl.java
  7. 36
      sop-example/sop-story/sop-story-web/src/main/java/com/gitee/sop/storyweb/controller/AlipayController.java
  8. 53
      sop-test/src/test/java/com/gitee/sop/test/AlipayClientPostTest.java

@ -1,32 +1,32 @@
* [首页](/?t=1564057981506)
* [首页](/?t=1564370846275)
* 开发文档
* [快速体验](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)
* [快速体验](files/10010_快速体验.md?t=1564370846281)
* [项目接入到SOP](files/10011_项目接入到SOP.md?t=1564370846298)
* [新增接口](files/10020_新增接口.md?t=1564370846298)
* [业务参数校验](files/10030_业务参数校验.md?t=1564370846299)
* [错误处理](files/10040_错误处理.md?t=1564370846299)
* [编写文档](files/10041_编写文档.md?t=1564370846299)
* [接口交互详解](files/10050_接口交互详解.md?t=1564370846299)
* [easyopen支持](files/10070_easyopen支持.md?t=1564370846299)
* [使用签名校验工具](files/10080_使用签名校验工具.md?t=1564370846299)
* [ISV管理](files/10085_ISV管理.md?t=1564370846299)
* [自定义路由](files/10086_自定义路由.md?t=1564370846299)
* [路由授权](files/10090_路由授权.md?t=1564370846300)
* [接口限流](files/10092_接口限流.md?t=1564370846300)
* [监控日志](files/10093_监控日志.md?t=1564370846300)
* [SDK开发](files/10095_SDK开发.md?t=1564370846300)
* [使用SpringCloudGateway](files/10096_使用SpringCloudGateway.md?t=1564370846300)
* [应用授权](files/10097_应用授权.md?t=1564370846300)
* [更改数据节点名称](files/10099_更改数据节点名称.md?t=1564370846300)
* [传统web开发](files/10100_传统web开发.md?t=1564370846300)
* [自定义过滤器](files/10102_自定义过滤器.md?t=1564370846300)
* [文件上传](files/10104_文件上传.md?t=1564370846301)
* [nacos注册中心](files/10106_nacos注册中心.md?t=1564370846301)
* [扩展其它注册中心](files/10107_扩展其它注册中心.md?t=1564370846301)
* [配置Sleuth链路追踪](files/10109_配置Sleuth链路追踪.md?t=1564370846301)
* 原理分析
* [原理分析之@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)
* [原理分析之@ApiMapping](files/90010_原理分析之@ApiMapping.md?t=1564370846301)
* [原理分析之路由存储](files/90011_原理分析之路由存储.md?t=1564370846301)
* [原理分析之如何路由](files/90012_原理分析之如何路由.md?t=1564370846301)
* [原理分析之文档归纳](files/90013_原理分析之文档归纳.md?t=1564370846301)
* [常见问题](files/90100_常见问题.md?t=1564370846301)

@ -65,6 +65,33 @@ public Object addGoods(GoodsParam param, HttpServletRequest request) {
}
```
- 方式2
```java
@ApiMapping(value = "story.get", version = "2.2")
public Story getStory22(OpenContext<Story> openContext) {
// 业务参数
Story bizObject = openContext.getBizObject();
// 获取appid,更多方法查看OpenContext类
String appId = openContext.getAppId();
System.out.println(appId);
return bizObject;
}
```
另一种方式,OpenContext泛型参数填bizObject类,调用openContext.getBizObject()可直接获得对象
此方式等价于:
```java
public Story getStory22(Story bizObject) {
OpenContext openContext = ServiceContext.getCurrentContext().getOpenContext();
String appId = openContext.getAppId();
System.out.println(appId);
return bizObject;
}
```
## 接口命名
接口命名没有做强制要求,但我们还是推荐按照下面的方式进行命名:

@ -11,7 +11,12 @@ String appId = openContext.getAppId();
```java
OpenContext openContext = ServiceContext.getCurrentContext().getOpenContext();
Object bizObject = openContext.getBizObject();
Story bizObject = (Story)openContext.getBizObject();
OpenContext openContext = ServiceContext.getCurrentContext().getOpenContext();
Story bizObject = openContext.getBizObject(Story.class);
```
## Socket error occurred: `localhost/0:0:0:0:0:0:0:1:2181`: Connection refused

@ -0,0 +1,16 @@
package com.gitee.sop.servercommon.bean;
/**
* @author tanghc
*/
public interface OpenBeanFactory {
/**
* 返回业务参数对象
*
* @param clazz 业务参数类class
* @param <T> 业务参数对象
* @return 返回业务参数对象
*/
<T> T getBizObject(Class<T> clazz);
}

@ -12,73 +12,84 @@ import java.util.Date;
* 可通过<code>OpenRequest openRequest = ServiceContext.getCurrentContext().getOpenRequest();</code>方式进行获取
* OpenContext
* OpenParam
* @author tanghc
*
* @author tanghc
*/
public interface OpenContext<T> {
public interface OpenContext<T> extends OpenBeanFactory {
/**
* 返回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();
/**
* 返回token即access_token
*
* @return 返回token
*/
String appAuthToken();
/**
* 返回回调地址
*
* @return 返回回调地址
*/
String getNotifyUrl();

@ -95,4 +95,16 @@ public class OpenContextImpl<T> implements OpenContext<T> {
public String getNotifyUrl() {
return rootJsonObject.getString(NOTIFY_URL_NAME);
}
@Override
public <E> E getBizObject(Class<E> clazz) {
if (bizObject != null && bizObject.getClass() == clazz) {
return (E) bizObject;
}
JSONObject bizJsonObj = this.rootJsonObject.getJSONObject(BIZ_CONTENT_NAME);
if (bizJsonObj == null) {
return null;
}
return bizJsonObj.toJavaObject(clazz);
}
}

@ -41,9 +41,12 @@ public class AlipayController {
// 接口名,使用默认版本号
@ApiMapping(value = "story.get")
public Story storyget() {
// 获取开放平台参数
OpenContext openContext = ServiceContext.getCurrentContext().getOpenContext();
String appId = openContext.getAppId();
Story story = new Story();
story.setId(1);
story.setName("海底小纵队(默认版本号)");
story.setName("海底小纵队(默认版本号), app_id:" + appId);
return story;
}
@ -67,10 +70,39 @@ public class AlipayController {
// 忽略验证
@ApiMapping(value = "story.get", version = "2.1", ignoreValidate = true)
public Story getStory21(Story story) {
story.setName(story.getName() + ", story.get2.1, ignoreValidate = true");
OpenContext openContext = ServiceContext.getCurrentContext().getOpenContext();
// 此处的param和story参数是一样的
Story param = openContext.getBizObject(Story.class);
boolean isSame = story == param;
story.setName(story.getName() + ", story.get2.1, ignoreValidate = true, story==param:" + isSame);
return story;
}
/**
* 另一种方式OpenContext泛型参数填bizObject类<br>
* 调用openContext.getBizObject()可直接获得对象<br>
* 此方式等价于
* <pre>
* public Story getStory22(Story bizObject) {
* OpenContext openContext = ServiceContext.getCurrentContext().getOpenContext();
* // 获取appid,更多方法查看OpenContext类
* String appId = openContext.getAppId();
* System.out.println(appId);
* return bizObject;
* }
* </pre>
* @param openContext
* @return
*/
@ApiMapping(value = "story.get", version = "2.2")
public Story getStory22(OpenContext<Story> openContext) {
Story bizObject = openContext.getBizObject();
// 获取appid,更多方法查看OpenContext类
String appId = openContext.getAppId();
System.out.println(appId);
return bizObject;
}
// http://localhost:2222/getStory2
// 遗留接口具备开放平台能力
@ApiAbility

@ -1,6 +1,7 @@
package com.gitee.sop.test;
import com.alibaba.fastjson.JSON;
import com.gitee.sop.test.alipay.AlipayApiException;
import com.gitee.sop.test.alipay.AlipaySignature;
import org.junit.Test;
@ -178,4 +179,56 @@ public class AlipayClientPostTest extends TestBase {
System.out.println(responseData);
}
// 忽略验证,不校验签名,只需传接口名、版本号、业务参数
@Test
public void testIgnore() {
// 公共请求参数
Map<String, String> params = new HashMap<String, String>();
params.put("method", "story.get");
params.put("version", "2.1");
// 业务参数
Map<String, String> bizContent = new HashMap<>();
bizContent.put("id", "222");
bizContent.put("name", "忽略验证name");
params.put("biz_content", JSON.toJSONString(bizContent));
System.out.println("----------- 返回结果 -----------");
String responseData = post(url, params);// 发送请求
System.out.println(responseData);
}
@Test
public void testStoryget() throws AlipayApiException {
// 公共请求参数
Map<String, String> params = new HashMap<String, String>();
params.put("app_id", appId);
params.put("method", "story.get");
params.put("version", "2.2");
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()));
// 业务参数
Map<String, String> bizContent = new HashMap<>();
bizContent.put("id", "222");
bizContent.put("name", "忽略验证22");
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 = post(url, params);// 发送请求
System.out.println(responseData);
}
}

Loading…
Cancel
Save