简体   繁体   English

如何使用Mule Flow的json净荷过帐到REST api?

[英]How to POST to REST api with json payload from Mule Flow?

I am pretty new to Mule and need to stitch some micro-services to achieve a business objective. 我对Mule来说还很陌生,需要结合一些微服务来实现业务目标。 In this regard, my services are REST based and have a JSON payload. 在这方面,我的服务基于REST,并且具有JSON负载。 Below is my configuration ( used so far ) so as to invoke the service from mule. 下面是我的配置(到目前为止使用),以便从mule调用服务。

<?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>

My service is hosted on 10.208.165.126:8080/ingestion/esb; 我的服务托管在10.208.165.126:8080/ingestion/esb; but this does not even hit my service. 但这甚至没有影响我的服务。 This service has a payload which is of the below format : {"user":"saurabh"} 该服务的有效负载格式如下:{“ user”:“ saurabh”}

Please help me with the configuration and assist me in achieving my objective. 请帮助我进行配置,并协助我实现目标。

I am not sure what is your data source but here is a simple example 我不确定您的数据源是什么,但这是一个简单的示例

在此处输入图片说明

<?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>
  1. make your json data the current payload 使您的json数据成为当前有效负载
  2. add properties before the HTTP Rest endpoint and set Content-Type application/json 在HTTP Rest端点之前添加属性并设置Content-Type application / json
  3. set HTTP outbound enpoint to POST 将HTTP出站设置为POST
  4. fly away 飞走

使用POSTMAN或任何Rest客户。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM