REST Web Service with Mule ESB

In this tutorial I am going to show you how we can create REST web service in Mule ESB. We will use HTTP Connector as a request-response medium to interact with REST web service.

You can see also SOAP Web Service with Mule ESB and Send data to remote REST web application using Mule ESB

Prerequisites

Mule Studio 3.x(Anypoint Studio) (Download from https://www.mulesoft.com/platform/studio)
Maven 3.2.1 (Download from https://maven.apache.org/download.cgi?Preferred=ftp://mirror.reverse.net/pub/apache/)
JDK 1.7 (Download from http://www.oracle.com/technetwork/java/javase/downloads/index.html)
Configure JDK, Maven and Mule Studio

Step 1. First install JDK
Step 2. Add the Java_Home/bin directory to your system’s PATH.
Step 3. After downloading Maven, extract it to a drive
Step 4. Add the M2_Home/bin directory to your system’s PATH.
Step 5. Download and extract Mule Studio to a drive
Step 6. Now start Mule Studio by clicking on AnypointStudio exe icon in the folder <physical drive>/AnypointStudio
Step 7. Once started, close the startup page
Step 8. In Mule Studio, go to Window -> Preferences. Expand Java, then click on Installed JREs. Add JDK 1.7 and select it. In expanded Java, click on Compiler and select the compiler level as 1.7
Step 9. Now expand Anypoint Studio and click on Maven Settings. Then select appropriate Maven installation home directory using Browse button.
Step 10. If you want you can input Default groupId for new projects, it will save your time every time when you want to create a new project.

Create Mule project in Mule Studio
Now we will see how to create a new project in Mule Studio(Anypoint Studio).

Step 1. In Anypoint Studio, go to File -> New -> Mule Project
Step 2. Input Project Name: mule, Runtime is by default selected, tick on Use Maven; here the artifactId is automatically picked up from the Project Name:, the Group Id is picked up from the Default groupId for new projects and version is also a default value.
Step 3. Click Next and verify the JDK, mainly select Use default JRE(currently ‘jdk1.7.0_x’)
Step 4. Click on Next and click on Finish.

Example

Step 1. Create the rest-with-mule.xml file under src/main/app directory and put the below file connector. While you create the xml file you will see on red mark on each file connector. Do not worry, red mark will be disappeared once you modify the xml file as given in Step 3.


rest with mule

Step 2. Open the rest-with-mule.xml file and click on Configuration XML view in the Editor
Step 3. Modify the rest-with-mule.xml file as shown below

<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:core="http://www.mulesoft.org/schema/mule/core"
	xmlns:tracking="http://www.mulesoft.org/schema/mule/ee/tracking"
	xmlns:jersey="http://www.mulesoft.org/schema/mule/jersey" 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" version="EE-3.5.1"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd 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
http://www.mulesoft.org/schema/mule/ee/tracking http://www.mulesoft.org/schema/mule/ee/tracking/current/mule-tracking-ee.xsd">
	<flow name="rest-with-mule-flow" doc:name="rest-with-mule-flow">
		<http:inbound-endpoint exchange-pattern="request-response"
			host="localhost" port="8081" doc:name="HTTP" />
		<jersey:resources doc:name="REST">
			<component class="com.roytuts.mule.service.impl.BookServiceImpl" />
		</jersey:resources>
	</flow>
</mule>

In the above configuration we a Mule flow named as rest-with-mule-flow and inside this flow there is one http connector, which is acting as inboud-endpoint and handles request and response and there is another component called REST, which is used to publish RESTful web service via JAX-RS annotations using Jersey API.

Step 4. Create a below model class under directory src/main/java

package com.roytuts.mule.model;
import java.io.Serializable;
public class Book implements Serializable {
	private static final long serialVersionUID = 1L;
	private String title;
	private String author;
	private String isbn;
	public Book(String title, String author, String isbn) {
		this.title = title;
		this.author = author;
		this.isbn = isbn;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public String getAuthor() {
		return author;
	}
	public void setAuthor(String author) {
		this.author = author;
	}
	public String getIsbn() {
		return isbn;
	}
	public void setIsbn(String isbn) {
		this.isbn = isbn;
	}
}

Step 5. Create a below service interface under directory src/main/java

package com.roytuts.mule.service;
import java.util.List;
import com.roytuts.mule.model.Book;
public interface BookService {
	List<Book> findAll();
}

Step 6. Create a below service implementation class under src/main/java

package com.roytuts.mule.service.impl;
import java.util.ArrayList;
import java.util.List;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import com.roytuts.mule.model.Book;
import com.roytuts.mule.service.BookService;
@Path("books")
public class BookServiceImpl implements BookService {
	@Override
	@GET
	@Produces(MediaType.APPLICATION_JSON)
	public List<Book> findAll() {
		// create new books
		// ideally these book object should come from repository
		Book b1 = new Book("Java Programming", "Gosling", "j145781294");
		Book b2 = new Book("REST Web Services", "Jersey", "r857875292");
		Book b3 = new Book("SOAP Web Services", "Microsoft", "s178458365");
		List<Book> books = new ArrayList<>();
		books.add(b1);
		books.add(b2);
		books.add(b3);
		return books;
	}
}

Running the application

Now do a right-click on the rest-with-mule.xml file and click on Run As -> Mule Application. Then you will see something like below in Console when the application runs

INFO  2016-09-26 07:44:08,528 [main] org.mule.DefaultMuleContext:
**********************************************************************
* Application: mule                                                  *
* OS encoding: Cp1252, Mule encoding: UTF-8                          *
*                                                                    *
* Agents Running:                                                    *
*   DevKit Extension Information                                     *
*   Batch module default engine                                      *
*   Clustering Agent                                                 *
*   JMX Agent                                                        *
**********************************************************************
INFO  2016-09-26 07:44:08,528 [main] org.mule.module.launcher.MuleDeploymentService:
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+ Started app 'mule'                                       +
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
INFO  2016-09-26 07:44:08,549 [main] org.mule.module.launcher.DeploymentDirectoryWatcher:
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+ Mule is up and kicking (every 5000ms)                    +
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Once the application is up and running, try to hit the below URL either in the browser or from the REST client, you will come up with the below output as shown below in screen-shot


rest with mule


rest with mule

Thanks for reading.

Leave a Reply

Your email address will not be published. Required fields are marked *