WSO2 EI:REST API调用数据服务后无法触发重定向,返回JSON结果求助
WSO2 EI 重定向失效问题解决
问题背景
在WSO2 Enterprise Integrator中创建了数据服务查询SQL Server数据库的UserCode,需求为:若UserCode存在则REST API重定向到指定网页,不存在则重定向到另一网页。但测试时API直接返回数据服务的JSON结果,未触发重定向。
问题诊断
- 参数未传递:REST API调用数据服务时,未将URI中的UserCode参数传递给数据服务,导致查询返回空结果。
- XPath判断失效:Filter使用的
$body//FirstName/text() != ''表达式,在查询无结果时因节点不存在无法正确判断用户是否存在。 - 重定向配置不完整:设置重定向属性后未清空响应体,
respond会直接返回原有JSON内容,覆盖重定向响应。
解决方案
1. 修复数据服务参数传递
修改REST API中call节点的endpoint,传递URI中的UserCode参数:
<call> <endpoint> <http method="GET" uri-template="http://localhost:8280/services/restds/Users?UserCode={uri.var.UserCode}"/> </endpoint> </call>
2. 修正Filter的XPath表达式
改为判断是否存在用户节点,确保空结果时也能正确分支:
<filter xpath="count($body//user) > 0">
3. 完善重定向配置
添加响应标记、清空响应体,并使用send发送重定向(替换原有的respond):
<then> <log> <property name="Message" value="Name Exists Lets redirect"/> </log> <property name="RESPONSE" value="true" scope="default" type="STRING"/> <property name="NO_ENTITY_BODY" value="true" scope="axis2" type="STRING"/> <property name="HTTP_SC" value="302" scope="axis2" type="STRING"/> <property name="Location" value="https://wso2.com/" scope="transport" type="STRING"/> </then> <else> <log> <property name="Message" value="No user found, redirecting"/> <property name="HTTP_SC" value="302"/> <property name="Location" value="https://www.youtube.com/"/> </log> <property name="RESPONSE" value="true" scope="default" type="STRING"/> <property name="NO_ENTITY_BODY" value="true" scope="axis2" type="STRING"/> <property name="HTTP_SC" value="302" scope="axis2" type="STRING"/> <property name="Location" value="https://www.youtube.com/" scope="transport" type="STRING"/> </else> <send/>
4. 优化数据服务返回格式(可选)
调整数据服务的result映射,确保无结果时返回空数组,让XPath判断更一致:
<result outputType="json" useColumnNumbers="true"> { "users": { "user": [ { "UserCode": "$1", "FirstName": "$2", "LastName": "$3" } ] } } </result>
修改后的完整REST API代码
<api xmlns="http://ws.apache.org/ns/synapse" name="DSAPI2" context="/dsapi2"> <resource methods="GET" uri-template="/{UserCode}"> <inSequence> <call> <endpoint> <http method="GET" uri-template="http://localhost:8280/services/restds/Users?UserCode={uri.var.UserCode}"/> </endpoint> </call> <filter xpath="count($body//user) > 0"> <then> <log> <property name="Message" value="Name Exists Lets redirect"/> </log> <property name="RESPONSE" value="true" scope="default" type="STRING"/> <property name="NO_ENTITY_BODY" value="true" scope="axis2" type="STRING"/> <property name="HTTP_SC" value="302" scope="axis2" type="STRING"/> <property name="Location" value="https://wso2.com/" scope="transport" type="STRING"/> </then> <else> <log> <property name="Message" value="No user found, redirecting"/> <property name="HTTP_SC" value="302"/> <property name="Location" value="https://www.youtube.com/"/> </log> <property name="RESPONSE" value="true" scope="default" type="STRING"/> <property name="NO_ENTITY_BODY" value="true" scope="axis2" type="STRING"/> <property name="HTTP_SC" value="302" scope="axis2" type="STRING"/> <property name="Location" value="https://www.youtube.com/" scope="transport" type="STRING"/> </else> </filter> <send/> </inSequence> <outSequence/> <faultSequence/> </resource> </api>
内容的提问来源于stack exchange,提问作者Kelisha Govender
相关产品推荐
相关产品推荐

