parent
b87d0eaacb
commit
043b760e03
@ -1,32 +1,32 @@ |
||||
* [首页](/?t=1564370846275) |
||||
* [首页](/?t=1564558762831) |
||||
* 开发文档 |
||||
* [快速体验](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) |
||||
* [快速体验](files/10010_快速体验.md?t=1564558762834) |
||||
* [项目接入到SOP](files/10011_项目接入到SOP.md?t=1564558762850) |
||||
* [新增接口](files/10020_新增接口.md?t=1564558762850) |
||||
* [业务参数校验](files/10030_业务参数校验.md?t=1564558762850) |
||||
* [错误处理](files/10040_错误处理.md?t=1564558762850) |
||||
* [编写文档](files/10041_编写文档.md?t=1564558762850) |
||||
* [接口交互详解](files/10050_接口交互详解.md?t=1564558762850) |
||||
* [easyopen支持](files/10070_easyopen支持.md?t=1564558762850) |
||||
* [使用签名校验工具](files/10080_使用签名校验工具.md?t=1564558762850) |
||||
* [ISV管理](files/10085_ISV管理.md?t=1564558762850) |
||||
* [自定义路由](files/10086_自定义路由.md?t=1564558762851) |
||||
* [自定义返回结果](files/10087_自定义返回结果.md?t=1564558762851) |
||||
* [自定义过滤器](files/10088_自定义过滤器.md?t=1564558762851) |
||||
* [路由授权](files/10090_路由授权.md?t=1564558762851) |
||||
* [接口限流](files/10092_接口限流.md?t=1564558762851) |
||||
* [监控日志](files/10093_监控日志.md?t=1564558762851) |
||||
* [SDK开发](files/10095_SDK开发.md?t=1564558762851) |
||||
* [使用SpringCloudGateway](files/10096_使用SpringCloudGateway.md?t=1564558762851) |
||||
* [应用授权](files/10097_应用授权.md?t=1564558762851) |
||||
* [传统web开发](files/10100_传统web开发.md?t=1564558762851) |
||||
* [文件上传](files/10104_文件上传.md?t=1564558762852) |
||||
* [nacos注册中心](files/10106_nacos注册中心.md?t=1564558762852) |
||||
* [扩展其它注册中心](files/10107_扩展其它注册中心.md?t=1564558762852) |
||||
* [配置Sleuth链路追踪](files/10109_配置Sleuth链路追踪.md?t=1564558762852) |
||||
* 原理分析 |
||||
* [原理分析之@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) |
||||
* [原理分析之@ApiMapping](files/90010_原理分析之@ApiMapping.md?t=1564558762852) |
||||
* [原理分析之路由存储](files/90011_原理分析之路由存储.md?t=1564558762852) |
||||
* [原理分析之如何路由](files/90012_原理分析之如何路由.md?t=1564558762852) |
||||
* [原理分析之文档归纳](files/90013_原理分析之文档归纳.md?t=1564558762852) |
||||
* [常见问题](files/90100_常见问题.md?t=1564558762852) |
||||
|
@ -0,0 +1,125 @@ |
||||
# 自定义返回结果 |
||||
|
||||
网关默认对业务结果进行合并,然后返回统一的格式。 |
||||
|
||||
针对`alipay.story.find`接口,微服务端返回结果如下: |
||||
|
||||
```json |
||||
{ |
||||
"name": "白雪公主", |
||||
"id": 1, |
||||
"gmtCreate": 1554193987378 |
||||
} |
||||
``` |
||||
|
||||
网关合并后,最终结果如下 |
||||
|
||||
```json |
||||
{ |
||||
"alipay_story_find_response": { |
||||
"msg": "Success", |
||||
"code": "10000", |
||||
"name": "白雪公主", |
||||
"id": 1, |
||||
"gmtCreate": 1554193987378 |
||||
}, |
||||
"sign": "xxxxx" |
||||
} |
||||
``` |
||||
|
||||
其中`alipay_story_find_response`是它的数据节点。规则是: |
||||
|
||||
> 将接口名中的点`.`转换成下划线`_`,后面加上`_response` |
||||
|
||||
代码实现如下: |
||||
|
||||
```java |
||||
String method = "alipay.story.find"; |
||||
return method.replace('.', '_') + "_response"; |
||||
``` |
||||
|
||||
详见`DefaultDataNameBuilder.java` |
||||
|
||||
如果要更改数据节点,比如`result`,可使用`CustomDataNameBuilder.java`。 |
||||
|
||||
```java |
||||
@Configuration |
||||
public class ZuulConfig extends AlipayZuulConfiguration { |
||||
|
||||
static { |
||||
... |
||||
ApiConfig.getInstance().setDataNameBuilder(new CustomDataNameBuilder()); |
||||
... |
||||
} |
||||
|
||||
} |
||||
``` |
||||
|
||||
设置后,网关统一的返回结果如下: |
||||
|
||||
```json |
||||
{ |
||||
"result": { |
||||
... |
||||
}, |
||||
"sign": "xxxxx" |
||||
} |
||||
``` |
||||
|
||||
此外,构造方法可指定自定义字段名称:`new CustomDataNameBuilder("data");`。 |
||||
设置后,数据节点将变成`data` |
||||
|
||||
```json |
||||
{ |
||||
"data": { |
||||
... |
||||
}, |
||||
"sign": "xxxxx" |
||||
} |
||||
``` |
||||
|
||||
**注**:网关设置了CustomDataNameBuilder后,SDK也要做相应的更改:`SdkConfig.dataNameBuilder = new CustomDataNameBuilder();` |
||||
|
||||
## 自定义结果处理 |
||||
|
||||
如果想要对微服务结果做更深一步处理,步骤如下: |
||||
|
||||
1. 新增一个类,继承`ZuulResultExecutor.java`,并重写`public String merge(T exchange, JSONObject responseData)`方法 |
||||
|
||||
方法merge参数说明如下: |
||||
|
||||
exchange:RequestContext对象 |
||||
responseData:微服务端返回的结果 |
||||
|
||||
方法返回最终结果 |
||||
|
||||
2. 配置自定义类 |
||||
|
||||
```java |
||||
public class SopGatewayApplication { |
||||
|
||||
public static void main(String[] args) { |
||||
ApiConfig.getInstance().setZuulResultExecutor(new MyzuulResultExecutor()); |
||||
SpringApplication.run(SopGatewayApplication.class, args); |
||||
} |
||||
|
||||
} |
||||
``` |
||||
|
||||
|
||||
## 不合并结果 |
||||
|
||||
如果不希望对结果进行合并,可设置`ApiConfig.getInstance().setMergeResult(false);` |
||||
|
||||
```java |
||||
public class SopGatewayApplication { |
||||
|
||||
public static void main(String[] args) { |
||||
ApiConfig.getInstance().setMergeResult(false); |
||||
SpringApplication.run(SopGatewayApplication.class, args); |
||||
} |
||||
|
||||
} |
||||
``` |
||||
|
||||
这样,网关最终返回结果即为微服务端的返回结果。 |
@ -1,69 +0,0 @@ |
||||
# 更改数据节点名称 |
||||
|
||||
针对`alipay.story.find`接口,它的返回结果如下: |
||||
|
||||
```json |
||||
{ |
||||
"alipay_story_find_response": { |
||||
"msg": "Success", |
||||
"code": "10000", |
||||
"name": "白雪公主", |
||||
"id": 1, |
||||
"gmtCreate": 1554193987378 |
||||
}, |
||||
"sign": "xxxxx" |
||||
} |
||||
``` |
||||
|
||||
其中`alipay_story_find_response`是它的数据节点。规则是: |
||||
|
||||
> 将接口名中的点`.`转换成下划线`_`,后面加上`_response` |
||||
|
||||
代码实现如下: |
||||
|
||||
```java |
||||
String method = "alipay.story.find"; |
||||
return method.replace('.', '_') + "_response"; |
||||
``` |
||||
|
||||
详见`DefaultDataNameBuilder.java` |
||||
|
||||
如果要更改数据节点,比如`result`,可使用`CustomDataNameBuilder.java`。 |
||||
|
||||
```java |
||||
@Configuration |
||||
public class ZuulConfig extends AlipayZuulConfiguration { |
||||
|
||||
static { |
||||
... |
||||
ApiConfig.getInstance().setDataNameBuilder(new CustomDataNameBuilder()); |
||||
... |
||||
} |
||||
|
||||
} |
||||
``` |
||||
|
||||
设置后,网关统一的返回结果如下: |
||||
|
||||
```json |
||||
{ |
||||
"result": { |
||||
... |
||||
}, |
||||
"sign": "xxxxx" |
||||
} |
||||
``` |
||||
|
||||
此外,构造方法可指定自定义字段名称:`new CustomDataNameBuilder("data");`。 |
||||
设置后,数据节点将变成`data` |
||||
|
||||
```json |
||||
{ |
||||
"data": { |
||||
... |
||||
}, |
||||
"sign": "xxxxx" |
||||
} |
||||
``` |
||||
|
||||
**注**:网关设置了CustomDataNameBuilder后,SDK也要做相应的更改:`SdkConfig.dataNameBuilder = new CustomDataNameBuilder();` |
Loading…
Reference in new issue