2016-09-19 29 views
0

this question所示,當請求帶有mule的http端點時,可能會在變量中包含部分url。如何在Mule中的http請求連接器中動態設置完整url

但是我有一個完整的url,從支持討厭的端點返回。是否有可能使用這個完整的url請求而不將它分成幾個部分(主機,端口,路徑)?

有了完整的url我的意思是......像「http://example.com/a/specific/path」而不是host =「example.com」,port =「80」,path =「/ a/specific/path」

+0

我在騾子JIRA創建的票這樣的:https://www.mulesoft.org/jira/browse/MULE-10741 –

回答

2

一個簡單的方法是將url拆分成多個部分並設置它變成流量變量。然後你可以在任何你想要的地方使用這個流變量。

一個簡單的例子來做到這一點是如下: -

<http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8081" doc:name="HTTP Listener Configuration"/> 
    <flow name="csv-to-smtpFlow"> 
     <http:listener config-ref="HTTP_Listener_Configuration" path="/aa" doc:name="HTTP"/> 
     <logger message="#[payload]" level="INFO" doc:name="Logger"/> 
     <set-payload value="http://example.com:80/a/specific/path" doc:name="Set Payload"/> 
     <set-variable variableName="url" value="#[new URL(payload);]" doc:name="Variable"/> 
     <set-variable variableName="protocol" value="#[url.getProtocol();]" doc:name="Variable"/> 
     <set-variable variableName="host" value="#[url.getHost();]" doc:name="Variable"/> 
     <set-variable variableName="port" value="#[url.getPort();]" doc:name="Variable"/> 
     <set-variable variableName="path" value="#[url.getPath();]" doc:name="Variable"/> 
     <logger message="Done" level="INFO"/> 
    </flow> 

在這裏你可以看到我設置的URL http://example.com:80/a/specific/path的有效載荷,然後將其拆分成主機,端口,路徑等並將其存儲在變量。

請注意如果您的網址形式http://example.com:80/a/specific/path,你會使用表達式#[url.getProtocol();]
得到端口,但如果你的網址形式http://example.com/a/specific/path,你不會得到的端口號。

參考: - https://docs.oracle.com/javase/tutorial/networking/urls/urlInfo.html

+0

謝謝您的答覆。但正如問題中所描述的,我希望能夠在不拆分url的情況下找到解決方案。所以我現在不會接受你的答案,並等待有人有另一種解決方案 –