首页 > 解决方案 > 如何在 mule 中设置查询参数以服务端点请求

问题描述

我有一个服务端点 http://xyzhost:port/greet123 ,它接受请求参数 salaryage,我需要将查询参数作为请求的一部分传递,例如 http://xyzhost:port/greet123?salary=100000&age=32 。我是 mule 新手,我成功调用了其他没有请求参数的服务端点,但是当有要传递的查询参数时,我无法设置查询参数并获得正确的响应。我正在使用 mule 3.3 版本。下面是我的 mule 流程。在使用http:outbound-endpoint到达服务端点之前在 mule 流中我正在使用自定义转换器(自定义请求转换器)将查询参数设置为出站请求范围。但是没有运气,由于我收到 400 - Bad request 作为来自服务端点的响应,因此无法将查询参数设置为请求的一部分。甚至我也尝试过使用在骡流中。谁能建议我如何将查询参数设置为服务端点。

下面是 mule flow xml 文件和自定义请求转换器

<http:connector name="ClientOutboundHttpConnector">
       <service-overrides sessionHandler="org.mule.session.NullSessionHandler"/>
    </http:connector>
   
    
<!--  Below is the flow for sample rest endpoint -->

<custom-transformer
        name="SampleRequestTransformer"
        class="com.example.SampleRequestTrasformer" />
        
        <custom-transformer
        name="SampleResponseTransformer"
        class="com.example.SampleResponseTrasformer" />
    
    <flow name="sampleRESTFlow" doc:name="sample rest Flow">
                     
        <inbound-endpoint address="servlet://getDetails" exchange-pattern="request-response" connector-ref="muleCXFServletConnector" 
                transformer-refs="SampleRequestTransformer"  responseTimeout="600000" />
                
        
        
        <http:outbound-endpoint
            address="http://xyzhost:port/greet123"
            connector-ref="ClientOutboundHttpConnector"
            exchange-pattern="request-response"
            contentType="application/x-www-form-urlencoded"
            keep-alive="true"
            responseTimeout="360000"
            responseTransformer-refs="InputStreamToStringTransformer"/>

        <logger
            message="MappingServer Response after reading stream=#[payload]"
            level="INFO" />

        <transformer ref="SampleResponseTransformer" />

        <transformer ref="clientErrorResponseTransformer" />

        

    </flow>

下面是自定义请求转换器的代码


    @Override
    public Object transformMessage(MuleMessage message, String arg1)
            throws TransformerException {
        // TODO Auto-generated method stub
        Object payload = message.getPayload();
        Map<String,String> queryParams= new HashMap<String,String>();
        queryParams.put("salary", "400000");
        queryParams.put("age", "32");
        
        
        
    
        message.setProperty("Content-Type", "application/x-www-form-urlencoded", PropertyScope.OUTBOUND);
        message.setProperty("mimeType","application/x-www-form-urlencoded",PropertyScope.OUTBOUND);
        message.setProperty("http.method", "POST", PropertyScope.OUTBOUND);
        message.setProperty("http.query.params",queryParams, PropertyScope.OUTBOUND);
        
        
        return message;
    }

}```
    

标签: mulemule-componentanypoint-studiomule-esb

解决方案


首先 - http 错误 400 表示资源不可用。但是服务器和端口都很好。因此,/greet123 不可用。问题不在于您的代码 - 问题在于没有人在听它。尝试常规浏览器或 Postman - 结果将相同 - 400 错误。

第二 - Mule 3 已弃用。据我所知,今年 3.8 将不再受支持。3.3 好久不见了。Mule 4 是当前版本,Mule 3 无法转移到 Mule 4。

https://simpleflatservice.com/mule4/Mule3toMule4Transformation.html


推荐阅读