2016-09-19 30 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的情况下找到解决方案。所以我现在不会接受你的答案,并等待有人有另一种解决方案 –