You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

请求协助修复ESB代码:URIException无效查询错误

Fixing the 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

  1. Mismatched URL Parameter Reference
    Your API's uri-template defines the query parameter as name={namVal}, but in the inSequence you're trying to fetch it with $url:name — this is incorrect. The $url:{param} syntax references the placeholder name from the uri-template, not the actual query parameter key. So you should use $url:namVal instead.

  2. Unmatched Variable Name in Endpoint
    Your endpoint uses {uri.var.fname} as the query parameter placeholder, but your inSequence sets a property named uri.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.

  3. 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 07:10:22