WSO2 EI 6.6.0代理返回JSON单元素数组方括号丢失问题求助
WSO2 Integration Studio 8.0 代理服务单元素JSON数组丢失方括号问题解决
问题描述
在WSO2 Integration Studio 8.0中创建安全代理服务后,出现以下异常:
- 直接请求后端服务(地址:
http://1.2.3.4/json?list=[{nm_1:4,nm_2:9}])时,返回正常JSON响应:{"success":true,"total":1,"data":[{"nm_1":4,"nm_2":9,"id_1":5,"nm_num":"12345","id_2":13,"err":0}]} - 通过代理请求(地址:
http://wso2/json?list=[{nm_1:4,nm_2:9}])时,data字段的单元素数组被转为对象,丢失方括号:{"success":true,"total":1,"data":{"nm_1":4,"nm_2":9,"id_1":5,"nm_num":"12345","id_2":13,"err":0}} - 请求多元素数组时,代理返回的
data字段格式正常。
尝试添加JsonTransformer中介并配置JSON Schema后,问题仍未解决。原始代理代码及Schema配置如下:
原始代理代码
<?xml version="1.0" encoding="UTF-8"?> <proxy name="test_proxy" startOnLoad="true" transports="http https" xmlns="http://ws.apache.org/ns/synapse"> <target> <endpoint key="conf:/test.bs"/> <inSequence> <header action="remove" description="Delete Header Security" name="wsse:Security" scope="default" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"/> <property name="messageType" scope="default" type="STRING" value="application/json"/> <jsontransform schema="JsonSchema"/> </inSequence> <outSequence> <send/> </outSequence> <faultSequence/> </target> <policy key="conf:policy/test_wspolicy.xml"/> <enableSec/> </proxy>
原始JSON Schema(localEntry)
<?xml version="1.0" encoding="UTF-8"?> <localEntry key="JsonSchema" xmlns="http://ws.apache.org/ns/synapse"><![CDATA[ { "$schema": "http://json-schema.org/draft-04/schema#", "type": "object", "properties": { "data": { "type": "array", "items":[ { "type": "object", "properties": { "nm_1": { "type": "integer" } }, "required": [ "nm_kng" ] }, { "type": "object", "properties": { "nm_2": { "type": "integer" } } } ] }, "required": [ "nm_1", "nm_2" ] } } ]]></localEntry>
原因分析
- WSO2默认的JSON处理引擎(基于Jackson)在单元素数组转换时,会默认将其折叠为单个对象,尤其是在安全策略触发XML-JSON转换流程时,更容易出现这种结构丢失。
- 之前的
JsonTransformer配置在inSequence(处理请求),但问题出在响应处理环节(outSequence),且Schema存在错误(如required字段不存在、items格式定义错误),导致校验失效。
解决方案
方案1:强制保留单元素数组(最简方案)
在代理的outSequence中添加FORCE_JSON_ARRAY属性,直接告诉JSON处理引擎保留数组结构:
<outSequence> <property name="FORCE_JSON_ARRAY" value="true" scope="axis2" type="BOOLEAN"/> <property name="messageType" scope="default" type="STRING" value="application/json"/> <send/> </outSequence>
方案2:配合修正后的JSON Schema校验(如需严格结构约束)
- 修正JSON Schema,确保
data字段明确为数组类型,且items定义正确:
<?xml version="1.0" encoding="UTF-8"?> <localEntry key="JsonSchema" xmlns="http://ws.apache.org/ns/synapse"><![CDATA[ { "$schema": "http://json-schema.org/draft-04/schema#", "type": "object", "properties": { "success": {"type": "boolean"}, "total": {"type": "integer"}, "data": { "type": "array", "items": { "type": "object", "properties": { "nm_1": {"type": "integer"}, "nm_2": {"type": "integer"}, "id_1": {"type": "integer"}, "nm_num": {"type": "string"}, "id_2": {"type": "integer"}, "err": {"type": "integer"} } } } }, "required": ["success", "total", "data"] } ]]></localEntry>
- 在
outSequence中添加JsonTransformer处理响应:
<outSequence> <property name="FORCE_JSON_ARRAY" value="true" scope="axis2" type="BOOLEAN"/> <property name="messageType" scope="default" type="STRING" value="application/json"/> <jsontransform schema="JsonSchema"/> <send/> </outSequence>
完整修正后的代理代码
<?xml version="1.0" encoding="UTF-8"?> <proxy name="test_proxy" startOnLoad="true" transports="http https" xmlns="http://ws.apache.org/ns/synapse"> <target> <endpoint key="conf:/test.bs"/> <inSequence> <header action="remove" description="Delete Header Security" name="wsse:Security" scope="default" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"/> <property name="messageType" scope="default" type="STRING" value="application/json"/> </inSequence> <outSequence> <property name="FORCE_JSON_ARRAY" value="true" scope="axis2" type="BOOLEAN"/> <property name="messageType" scope="default" type="STRING" value="application/json"/> <!-- 如需严格校验,取消下面一行注释 --> <!-- <jsontransform schema="JsonSchema"/> --> <send/> </outSequence> <faultSequence/> </target> <policy key="conf:policy/test_wspolicy.xml"/> <enableSec/> </proxy>
说明
- 优先尝试方案1,无需依赖Schema即可快速解决单元素数组折叠问题。
- 如果业务需要严格的JSON结构校验,再配合方案2的修正后Schema使用。
内容的提问来源于stack exchange,提问作者Sergo
相关产品推荐
相关产品推荐

