请求协助修复ESB代码:URIException无效查询错误
org.apache.commons.httpclient.URIException: Invalid query in WSO2 ESB Let's break down the issues causing this URI parsing error and fix them step by step:
Key Problems in Your Code
Mismatched URL Parameter Reference
Your API'suri-templatedefines the query parameter asname={namVal}, but in theinSequenceyou're trying to fetch it with$url:name— this is incorrect. The$url:{param}syntax references the placeholder name from theuri-template, not the actual query parameter key. So you should use$url:namValinstead.Unmatched Variable Name in Endpoint
Your endpoint uses{uri.var.fname}as the query parameter placeholder, but yourinSequencesets a property nameduri.var.name. Since the variable names don't match, Synapse can't replace the placeholder, leaving raw{uri.var.fname}in the URI — which causes the invalid query error during URI parsing.Non-Standard POST Usage (Optional but Recommended)
While POST requests can technically include query parameters, it's not REST-compliant. Typically, POST payloads are sent in the request body, which avoids URI parsing issues entirely.
Corrected Code Snippets
1. Fixed API Configuration
<?xml version="1.0" encoding="UTF-8"?> <api context="/test_api" name="rest" xmlns="http://ws.apache.org/ns/synapse"> <resource methods="GET" uri-template="/hello?name={namVal}"> <inSequence> <!-- Correctly reference the uri-template placeholder --> <property name="SYSTEM" expression="$url:namVal"/> <!-- Match the variable name used in the endpoint --> <property name="uri.var.fname" expression="$ctx:SYSTEM"/> <call blocking="true"> <endpoint key="hello_EP"/> </call> <payloadFactory media-type="xml"> ..... </payloadFactory> <respond /> </inSequence> <outSequence /> <faultSequence /> </resource> </api>
2. Endpoint (If Keeping Query Parameters)
Your existing endpoint is fine once the variable name matches, but ensure the port is filled in correctly:
<?xml version="1.0" encoding="UTF-8"?> <endpoint name="hello_EP" xmlns="http://ws.apache.org/ns/synapse"> <address uri="http://xxxxxxxxxx:YOUR_PORT/spring/hello?name={uri.var.fname}" methods="POST"/> </endpoint>
3. REST-Compliant Alternative (POST with Body)
If you want to follow REST best practices, modify the endpoint and inSequence to send the parameter in the request body:
Updated Endpoint
<?xml version="1.0" encoding="UTF-8"?> <endpoint name="hello_EP" xmlns="http://ws.apache.org/ns/synapse"> <address uri="http://xxxxxxxxxx:YOUR_PORT/spring/hello" methods="POST"/> </endpoint>
Modified inSequence (Add Payload Factory Before Call)
<inSequence> <property name="SYSTEM" expression="$url:namVal"/> <!-- Construct POST request body with the parameter --> <payloadFactory media-type="xml"> <format> <request> <name>$1</name> </request> </format> <args> <arg expression="$ctx:SYSTEM"/> </args> </payloadFactory> <call blocking="true"> <endpoint key="hello_EP"/> </call> <payloadFactory media-type="xml"> ..... </payloadFactory> <respond /> </inSequence>
Why This Works
The original URIException occurred because Synapse couldn't resolve the placeholder in the endpoint URI (due to mismatched variable names), leaving invalid characters (the unprocessed {uri.var.fname}) in the query string. By aligning the parameter references and variable names, Synapse correctly replaces the placeholders, generating a valid URI that passes HttpClient's parsing checks.
内容的提问来源于stack exchange,提问作者WHAT IS WRONG WITH YOU

