WSO2 Micro Integrator Base64转JSON发送触发AxisFault错误求助
解决方案:WSO2 MI发送含Base64的JSON到Spring Boot报错问题
问题根源
- 非法控制字符:从SOAP响应提取的Base64内容混入了非打印控制字符(如code31的Unit Separator),这些字符违反JSON语法规范,导致Spring Boot解析失败。
- GZIP压缩异常:大体积Base64 payload触发WSO2默认GZIP压缩,但Spring Boot未正确处理GZIP请求,或压缩过程中因非法字符抛出错误。
- JSON模板格式问题:PayloadFactory模板中的硬换行可能引入额外不可见字符,干扰JSON解析。
具体修复步骤
1. 清理Base64内容中的非法字符
修改提取Base64的property,使用fn:replace过滤掉非Base64允许的字符(仅保留A-Z,a-z,0-9,+,/,=):
<property name="base64" action="set" type="STRING" expression="fn:replace(//fileContents, '[^A-Za-z0-9+/=]', '')"/>
该操作会移除所有控制字符和其他非Base64字符,确保payload中的Base64格式合法。
2. 禁用GZIP压缩
在发送到Spring Boot的序列或端点中添加禁用GZIP的配置,避免压缩过程出错:
<!-- 在第二个call之前添加 --> <property name="DISABLE_GZIP" value="true" scope="axis2"/>
或者直接在端点配置中设置:
<endpoint name="samStagingUploadFD"> <http uri="http://your-spring-boot-service-url" method="POST"> <property name="DISABLE_GZIP" value="true" scope="axis2"/> </http> </endpoint>
3. 修复JSON模板的格式问题
将PayloadFactory的JSON模板改为单行,移除硬换行,避免引入额外字符:
<payloadFactory media-type="json" template-type="default"> <format>{"docsToUpload":[{"fileNature":"A0023","fileType":"CONTRACT","fileCode":"D0051","fileContents":"$1","longLabelDocument":"Proces verbal de reception SFP"}]}</format> <args> <arg evaluator="xml" expression="$ctx:base64"/> </args> </payloadFactory>
4. 验证Base64完整性
添加日志打印Base64长度,确认内容未被截断或篡改:
<log category="INFO" level="simple"> <property name="base64_length" expression="fn:string-length($ctx:base64)"/> </log>
修改后的完整序列代码
<sequence> <property name="uri.var.loanId" scope="default" type="STRING" expression="//mi:numDemande" action="set" xmlns:mi="http://mi.sofac.ma"/> <log category="INFO" level="full"> <property name="loanID" expression="$ctx:uri.var.loanId"/> </log> <payloadFactory media-type="xml" template-type="default"> <format><docListReception xmlns="******"> <offerId xmlns="">309264</offerId> <dealerLogin xmlns="">SAMG1001</dealerLogin> <documentType xmlns=""></documentType> <documentNature xmlns=""></documentNature> <documentCode xmlns="">D0053</documentCode> </docListReception></format> <args> </args> </payloadFactory> <header name="Action" value=" "/> <call> <endpoint key="DocListReception"/> </call> <!-- 清理Base64中的非法字符 --> <property name="base64" action="set" type="STRING" expression="fn:replace(//fileContents, '[^A-Za-z0-9+/=]', '')"/> <log category="INFO" level="simple"> <property name="base64" expression="$ctx:base64"/> <property name="base64_length" expression="fn:string-length($ctx:base64)"/> </log> <payloadFactory media-type="json" template-type="default"> <format>{"docsToUpload":[{"fileNature":"A0023","fileType":"CONTRACT","fileCode":"D0051","fileContents":"$1","longLabelDocument":"Proces verbal de reception SFP"}]}</format> <args> <arg evaluator="xml" expression="$ctx:base64"/> </args> </payloadFactory> <!-- 禁用GZIP压缩 --> <property name="DISABLE_GZIP" value="true" scope="axis2"/> <call> <endpoint key="samStagingUploadFD"/> </call> </sequence>
内容的提问来源于stack exchange,提问作者Mohamed Assedmer
相关产品推荐
相关产品推荐

