1
votes

currently I am working with mule esb. I would like to implement a rest service. The inbound of my app is AJAX. Here is the flow example that I want:

Flow

Is it possible? if yes, can you please give me an example about how to do it and the example of the rest class (just a simple class about how to get the payload and pass it to the next element)? Thanks in advance.

1

1 Answers

0
votes

One way of having a RESTful service in Mule is:

1) Use http:listener

<http:listener-config name="HTTPListener" host="127.0.0.1" port="8080"/>

2) Exploit regex and choice

<flow name="restFlow">
<http:listener config-ref="HTTPListener" path="/path*" doc:name="HTTP"/>
<choice doc:name="Choice">
  <when expression="#[regex('/path', message.inboundProperties['http.request.path'])]">
    <json:json-to-object-transformer returnClass="java.util.HashMap" />
    <logger message="INCOMING: #[message.inboundProperties['http.request.path']] PAYLOAD: #[message.payload]" level="INFO"/>
    <set-property propertyName="success" value="true" doc:name="Property"/>      
    <set-payload value="#[new java.util.HashMap()]"/>
    <expression-transformer expression="#[
      message.payload.success=message.outboundProperties.success;
      message.payload]" />
    <json:object-to-json-transformer doc:name="Object to JSON"/>
  </when>
  <otherwise>
    <logger message="UNSUPPORTED" level="INFO"/>
  </otherwise>
</choice>

You should get JSON back.