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

标签 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类的对象,那么如何设置查询和路径参数。

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

谢谢。

总和

最佳答案

首先,您必须将对象 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

关于java - 在http请求连接器中传递动态查询参数和路径参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33463989/

相关文章:

java - 字符串到字节数组的转换

java - org.springframework.boot.autoconfigure.jdbc.DataSourceInitializerInvoker 循环引用 Spring Boot 2.1.3

java - Spring Controller 映射配置和静态资源

java - 如何在 Mule Studio 中为 Java 组件提供属性 Bean?

java - 如何将 JSON 对象作为正文发布?

Java 俄罗斯方 block -如何更改一件掉落的计时器间隔,然后恢复回来?

java - Spring Boot - 将应用程序部署到 Google Cloud App 引擎但缺少 app.yaml

Spring MVC 3.2 @ResponseBody 拦截器

http - 在 Mule 中使用 Jersey 实现文件上传时出现不支持的媒体类型 (415) 错误

java - MULE ESB 简单 SOAP 服务创建必填字段