MuleSoft: REST web Service Development

Learn about how to expose and consume simple RESTful web service in Mule Anypoint.

 

Steps required to execute:

  1. Configure Http endpoint in Anypoint studio
  2. Create Java implementation for service
  3. Configure the REST component
  4. Test the Rest Service

Configure Http endpoint in Anypoint studio:

create a Flow with Http endpoint, configure the HTTP endpoint with Host and port as localhost and 8081.

HTTP-endpointhttp-configure

create a package com.javamirchi.rest under source folder src/main/java

package

 

Create Java implementation for service

package com.javamirchi.rest;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Response;

@Path(“/users”)
public class SimpleRestTest {

@GET
@Path(“{name}”)
public Response getWishes(@PathParam(“name”)String name){
String output = “Hi ” + name;
return Response.status(200).entity(output).build();
}

}

Configure the REST component

Drag and drop REST component from component pallette, and  configure the developed Java implementation as Spring bean.

Rest-configure

Rest-configure-2

Click ok, Finish and Finish.

Configuration is complete and ready to deploy the flow file as below

<?xml version=”1.0″ encoding=”UTF-8″?>

<mule xmlns:jersey=”http://www.mulesoft.org/schema/mule/jersey&#8221; xmlns:http=”http://www.mulesoft.org/schema/mule/http&#8221; xmlns=”http://www.mulesoft.org/schema/mule/core&#8221; xmlns:doc=”http://www.mulesoft.org/schema/mule/documentation&#8221;
xmlns:spring=”http://www.springframework.org/schema/beans&#8221;
xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance&#8221;
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/jersey http://www.mulesoft.org/schema/mule/jersey/current/mule-jersey.xsd”&gt;
<http:listener-config name=”HTTP_Listener_Configuration” host=”localhost” port=”8081″ doc:name=”HTTP Listener Configuration”/>
<spring:beans>
<spring:bean id=”Bean” name=”Bean” class=”com.javamirchi.rest.SimpleRestTest” scope=”singleton”/>
<spring:bean id=”simpleRest” name=”simpleRest” class=”com.javamirchi.rest.SimpleRestTest”/>
</spring:beans>
<flow name=”restdemoFlow”>
<http:listener config-ref=”HTTP_Listener_Configuration” path=”/*” doc:name=”HTTP”/>
<jersey:resources doc:name=”REST”>
<component>
<spring-object bean=”Bean”/>
</component>
</jersey:resources>
</flow>
</mule>

Test the Rest Service:

Deploy the Flow to Mule runtime from AnyPoint studio, Make sure the flow deployed properly

Open browser and hit the Url:  http://localhost:8081/users/sai

You can watch Response as Hi Sai in the browser:

Congragulations ! you configured simple Rest service in Mule Flow.