WSO2 API Manager工具SOAP转JSON报400错误求助
Hey there, let's break down why your SOAPtoJSON sequence is throwing a 400 Bad Request error. I've reviewed your configuration and spotted a couple of critical issues that don't align with how WSO2 processes SOAP responses in the OUT mediation flow.
Key Issues in Your Current SOAPtoJSON Sequence
- Wrong XPath Targets: Your payload factory uses
//request/PhoneNumberand//request/LicenseKey, but in the OUT sequence, the incoming message is the SOAP response from the backend service—not the original JSON request. Thoserequestnodes don't exist here, so the payload factory fails to build a valid payload, triggering the 400 error. - Incorrect Payload Structure for JSON Conversion: You're generating an XML payload that mirrors the request structure, but you need to map the backend's SOAP response fields instead. Additionally, forcing the
messageTypeto JSON without a valid, conversion-ready XML payload will cause parsing failures.
Fixed SOAPtoJSON Sequence
Here's a corrected version tailored to the PhoneVerify service's SOAP response structure (adjust fields based on the actual response you get from the backend):
<?xml version="1.0" encoding="UTF-8"?> <sequence name="SOAPtoJSON" trace="disable" xmlns="http://ws.apache.org/ns/synapse"> <log level="custom" separator=","> <property name="TRACE" value="Global Mediation Extension - Processing SOAP Response"/> </log> <!-- Extract data from the backend's SOAP response --> <payloadFactory media-type="xml"> <format> <PhoneVerificationResult xmlns=""> <PhoneNumber>$1</PhoneNumber> <IsValid>$2</IsValid> <PhoneType>$3</PhoneType> <State>$4</State> <Country>$5</Country> </PhoneVerificationResult> </format> <args> <!-- Use the correct namespace from the backend SOAP response --> <arg evaluator="xml" expression="//ns:CheckPhoneNumberResult/ns:PhoneNumber" xmlns:ns="http://ws.cdyne.com/PhoneVerify/query"/> <arg evaluator="xml" expression="//ns:CheckPhoneNumberResult/ns:Valid" xmlns:ns="http://ws.cdyne.com/PhoneVerify/query"/> <arg evaluator="xml" expression="//ns:CheckPhoneNumberResult/ns:Use" xmlns:ns="http://ws.cdyne.com/PhoneVerify/query"/> <arg evaluator="xml" expression="//ns:CheckPhoneNumberResult/ns:State" xmlns:ns="http://ws.cdyne.com/PhoneVerify/query"/> <arg evaluator="xml" expression="//ns:CheckPhoneNumberResult/ns:Country" xmlns:ns="http://ws.cdyne.com/PhoneVerify/query"/> </args> </payloadFactory> <!-- Remove the original SOAP envelope to clean up the payload --> <envelope> <soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope"> <soapenv:Body/> </soapenv:Envelope> </envelope> <payloadFactory media-type="xml"> <format>$1</format> <args> <arg evaluator="xml" expression="$body/*"/> </args> </payloadFactory> <!-- Tell WSO2 to convert the cleaned XML payload to JSON --> <property name="messageType" scope="axis2" type="STRING" value="application/json"/> </sequence>
Critical Fixes Explained
Target the Correct SOAP Response Nodes:
The backend service returns aCheckPhoneNumberResultelement under thehttp://ws.cdyne.com/PhoneVerify/querynamespace. We use an explicit namespace prefix (ns) in the XPath expressions to correctly locate these nodes—without this, WSO2 can't find the response fields.Simplify Payload for JSON Conversion:
We build a clean, flat XML structure (PhoneVerificationResult) that maps directly to a JSON object. WSO2's built-in converter handles the XML-to-JSON conversion seamlessly when the payload is structured this way.Clean Up the SOAP Envelope:
Removing the original SOAP envelope ensures only our custom payload is converted to JSON, avoiding extra SOAP-specific nodes in the final response.
Debugging Tips for Future Issues
- Enable Debug Logs: Update
repository/conf/log4j2.propertiesto setlogger.org.apache.synapse.level = DEBUG. This will show you exactly how each mediation step processes the message, including any XPath failures or payload errors. - Validate Backend Response: Use SOAPUI to call the backend PhoneVerify service directly. Copy the SOAP response XML and use it to verify your XPath expressions are targeting the right nodes.
内容的提问来源于stack exchange,提问作者Gustavo Gianini

