在http请求连接器中传递动态查询参数和路径参数

时间:2015-11-01 15:42:33

标签: java spring mule mule-studio mule-component

我希望将查询参数列表和路径参数传递给http请求mule连接器。进入此连接器的输入是pojo列表,其中包含查询和路径参数的值。

http请求连接器如下所示:

<http:request config-ref="HTTP_Request_Configuration" path="/rates/api/v1/rates/{in}.csv" method="GET" doc:name="End_HTTP request">
<http:request-builder>
    <http:query-param paramName="api_key"
        value="abcde" />
    <http:query-param paramName="quote" value={target_currency} />
    <http:query-param paramName="date" value={date} />
    <http:query-param paramName="fields" value="averages" />
    <http:uri-param paramName="in" value={source_currency} />
</http:request-builder>

pojo类看起来像这样:

public class Data {

    private String sourceCurrency;
    private String targetCurrency;
    private String date = (new SimpleDateFormat("yyyy-MM-dd")).format(new Date());

    public Data() {
    }

    public String getSourceCurrency() {
        return sourceCurrency;
    }

    public void setSourceCurrency(String sourceCurrency) {
        this.sourceCurrency = sourceCurrency;
    }

    public String getTargetCurrency() {
        return targetCurrency;
    }

    public void setTargetCurrency(String targetCurrency) {
        this.targetCurrency = targetCurrency;
    }

    public String getDate() {

        return date;
    }
}

如果http请求连接器的输入是Data类的对象,那么如何设置查询和路径参数。

有人可以帮我举个例子吗?

谢谢。

Sumved

2 个答案:

答案 0 :(得分:3)

首先,您必须将对象Data(有效负载)中的值分配给所需的变量(targetCurrency,date,sourceCurrency),然后您必须将变量的值分配给查询参数和路径参数。这里有一个例子:

<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns="http://www.mulesoft.org/schema/mule/core"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:spring="http://www.springframework.org/schema/beans"
      xmlns:scripting="http://www.mulesoft.org/schema/mule/scripting"
      xmlns:http="http://www.mulesoft.org/schema/mule/http"
      xmlns:jms="http://www.mulesoft.org/schema/mule/jms"
      xmlns:vm="http://www.mulesoft.org/schema/mule/vm"
      xmlns:file="http://www.mulesoft.org/schema/mule/file"
      xmlns:ftp="http://www.mulesoft.org/schema/mule/ftp"
      xmlns:db="http://www.mulesoft.org/schema/mule/db"
      xmlns:mule-xml="http://www.mulesoft.org/schema/mule/xml"
      xmlns:jersey="http://www.mulesoft.org/schema/mule/jersey"
      xmlns:json="http://www.mulesoft.org/schema/mule/json"
      xmlns:ws="http://www.mulesoft.org/schema/mule/ws"
      xmlns:smtps="http://www.mulesoft.org/schema/mule/smtps"
      xmlns:email="http://www.mulesoft.org/schema/mule/email"
      xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"      
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
        http://www.mulesoft.org/schema/mule/scripting http://www.mulesoft.org/schema/mule/scripting/current/mule-scripting.xsd
        http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
        http://www.mulesoft.org/schema/mule/jms http://www.mulesoft.org/schema/mule/jms/current/mule-jms.xsd
        http://www.mulesoft.org/schema/mule/vm http://www.mulesoft.org/schema/mule/vm/current/mule-vm.xsd
        http://www.mulesoft.org/schema/mule/file http://www.mulesoft.org/schema/mule/file/current/mule-file.xsd
        http://www.mulesoft.org/schema/mule/ftp http://www.mulesoft.org/schema/mule/ftp/current/mule-ftp.xsd
        http://www.mulesoft.org/schema/mule/db http://www.mulesoft.org/schema/mule/db/current/mule-db.xsd
        http://www.mulesoft.org/schema/mule/xml http://www.mulesoft.org/schema/mule/xml/current/mule-xml.xsd
        http://www.mulesoft.org/schema/mule/jersey http://www.mulesoft.org/schema/mule/jersey/current/mule-jersey.xsd
        http://www.mulesoft.org/schema/mule/json http://www.mulesoft.org/schema/mule/json/current/mule-json.xsd
        http://www.mulesoft.org/schema/mule/ws http://www.mulesoft.org/schema/mule/ws/current/mule-ws.xsd
        http://www.mulesoft.org/schema/mule/smtps http://www.mulesoft.org/schema/mule/smtps/current/mule-smtps.xsd
        http://www.mulesoft.org/schema/mule/email http://www.mulesoft.org/schema/mule/email/current/mule-email.xsd       
    ">

    <http:listener-config name="HTTP_Listener_Configuration" host="localhost" port="8081" doc:name="HTTP Listener Configuration">
        <http:worker-threading-profile maxThreadsActive="64" />
    </http:listener-config>

    <http:request-config name="HTTP_Request_Configuration" host="localhost" port="8081"/>

    <flow name="testingFlowService">
        <http:listener config-ref="HTTP_Listener_Configuration" path="/service/{waitTime}/*" doc:name="HTTP"/>
        <logger message="Query Params: #[message.inboundProperties.'http.query.params']" level="INFO" doc:name="Log Failure"/>
        <logger message="Uri Params: #[message.inboundProperties.'http.uri.params']" level="INFO" doc:name="Log Failure"/>
    </flow>

    <flow name="testingFlowClient">
        <http:listener config-ref="HTTP_Listener_Configuration" path="/client/*" doc:name="HTTP"/>
        <logger message="Before Transformation" level="INFO" />
        <json:json-to-object-transformer returnClass="com.testing.domain.GeneralRequest" />
        <set-variable variableName="processTypeJob" value="#[payload.processTypeJob]"/>
        <set-variable variableName="waitTime" value="#[payload.waitTime]"/>
        <logger message="After Transformation" level="INFO" />
        <json:object-to-json-transformer/>
        <http:request config-ref="HTTP_Request_Configuration" path="/service/{waitTime}" method="POST" 
                        responseTimeout="50000000">
            <http:request-builder>
                <http:query-param paramName="api_key"
                    value="abcde" />
                <http:query-param paramName="processTypeJob" value="#[processTypeJob]" />
                <http:query-param paramName="fields" value="averages" />
                <http:uri-param paramName="waitTime" value="#[waitTime]" />
            </http:request-builder>
        </http:request>
    </flow>

</mule>

这是我正在使用的课程:

package com.testing.domain;

import java.util.List;

public class GeneralRequest {
    private String processTypeJob;
    private String waitTime;
    private List<String> processIds;
    private String processTypeGroup;
    private List<JobConfiguration> jobConfigurations;

    public String getProcessTypeJob() {
        return processTypeJob;
    }
    public void setProcessTypeJob(String processTypeJob) {
        this.processTypeJob = processTypeJob;
    }
    public List<String> getProcessIds() {
        return processIds;
    }
    public void setProcessIds(List<String> processIds) {
        this.processIds = processIds;
    }
    public String getProcessTypeGroup() {
        return processTypeGroup;
    }
    public void setProcessTypeGroup(String processTypeGroup) {
        this.processTypeGroup = processTypeGroup;
    }
    public List<JobConfiguration> getJobConfigurations() {
        return jobConfigurations;
    }
    public void setJobConfigurations(List<JobConfiguration> jobConfigurations) {
        this.jobConfigurations = jobConfigurations;
    }
    public String getWaitTime() {
        return waitTime;
    }
    public void setWaitTime(String waitTime) {
        this.waitTime = waitTime;
    }



}

有关详细信息,请参阅下一个链接: https://docs.mulesoft.com/mule-user-guide/v/3.7/http-request-connector

答案 1 :(得分:1)

我认为您需要在流中添加数据对象(流变量)。然后,您可以在请求构建器中使用该变量,例如{flowVars.variable.someproperty}