2016-03-08 61 views
1

我對騾很新,需要縫合一些微服務來實現業務目標。在這方面,我的服務是基於REST的,並具有JSON負載。下面是我的配置(到目前爲止使用),以便從mule調用服務。如何使用來自Mule Flow的json有效載荷發佈到REST API?

<?xml version="1.0" encoding="UTF-8"?> 

<mule xmlns:dw="http://www.mulesoft.org/schema/mule/ee/dw" xmlns:metadata="http://www.mulesoft.org/schema/mule/metadata" xmlns:json="http://www.mulesoft.org/schema/mule/json" xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" 
    xmlns:spring="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd 
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.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/json http://www.mulesoft.org/schema/mule/json/current/mule-json.xsd 
http://www.mulesoft.org/schema/mule/ee/dw http://www.mulesoft.org/schema/mule/ee/dw/current/dw.xsd"> 
    <http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="9001" doc:name="HTTP Listener Configuration"/> 
    <http:request-config name="HTTP_Request_Configuration" host="10.208.165.126" port="8080" basePath="/ingestion" doc:name="HTTP Request Configuration"/> 
    <flow name="ingestionsampleFlow" > 
     <http:listener config-ref="HTTP_Listener_Configuration" path="/ingestion/" doc:name="HTTP"/> 
     <set-payload doc:name="Set Payload" value="#[payload.user]"/> 
     <!-- <json:json-to-object-transformer returnClass="java.util.Map" mimeType="application/json" doc:name="JSON to Object"/> 

     <http:request config-ref="HTTP_Request_Configuration" path="esb" method="POST" doc:name="HTTP"/> 
     --> 

     <!-- <http:inbound-endpoint doc:name="HTTP" exchange-pattern="request-response" contentType="application/json" host="10.208.165.126" port="8080"/> --> 

     <http:outbound-endpoint exchange-pattern="request-response" host="10.208.165.126" port="8080" method="POST" contentType="application/json" doc:name="HTTP" path="esb"/> 
     <logger message="2--&gt;#[payload]" level="INFO" doc:name="Logger"/> 


    </flow> 
</mule> 

我的服務託管在10.208.165.126:8080/ingestion/esb;但這甚至不會影響我的服務。此服務具有以下格式的有效載荷:{「user」:「saurabh」}

請幫助我完成配置並幫助實現我的目標。

回答

2

我不確定你的數據來源是什麼,但這是一個簡單的例子

enter image description here

<?xml version="1.0" encoding="UTF-8"?> 

<mule xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" 
    xmlns:spring="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd 
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd 
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd"> 
    <http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8081" doc:name="HTTP Listener Configuration"/> 
    <http:request-config name="HTTP_Request_Configuration_outboundWS" host="10.208.165.126" port="8080" doc:name="HTTP Request Configuration"/> 
    <flow name="sampleFlow"> 
     <http:listener config-ref="HTTP_Listener_Configuration" path="/inbound" doc:name="Inbound HTTP"/> 
     <json:object-to-json-transformer doc:name="Object to JSON"/>doc:name="Set Payload"/> 
     <set-property propertyName="Content-Type" value="application/json" doc:name="Set Outbound Header"/> 
     <http:request config-ref="HTTP_Request_Configuration_outboundWS" path="/your/webservice/path" method="POST" doc:name="POST WebService"/> 
    </flow> 
</mule> 
+0

感謝哥們,它真的有用,只有1個小問題,json輸入值是硬編碼的,我可以讓它動態。我共享的輸入是指定格式的樣本 –

+0

是的,我硬編碼它僅用於示例目的:)您可以使用數據映射器/數據編織或任何傑克遜庫來構造您的json數據並使其成爲您當前的有效載荷之前WS –

+0

請幫我拿個樣品 –

1
  1. 讓你的JSON數據當前載荷
  2. 的HTTP REST端點之前添加屬性和設置內容類型的應用程序/ JSON
  3. 集HTTP出站enpoint爲POST
  4. 飛走
+0

嗨,約翰,你能幫我整個配置 –

+0

看到我的新答案,希望它有幫助 –

1

使用POSTMAN或任何Rest客戶端。