DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • The Real-time Data Transfer Magic of Doris Kafka Connector's "Data Package": Part 1
  • Designing a Java Connector for Software Integrations
  • Kafka JDBC Source Connector for Large Data
  • Make Your Integration Seamless By Using Ballerina Client Connectors

Trending

  • Tactical Domain-Driven Design: Bringing Strategy to Code
  • Top JavaScript/TypeScript Gen AI Frameworks for 2026
  • The Network Attach Problem Nobody Warns You About
  • Building an Image Classification Pipeline With Apache Camel and Deep Java Library (DJL)

How to Create Mule Connectors

After experimenting with all the connectors available in Mule, maybe you want to create your own. Let's see how to create the simplest connector possible.

By 
Anupam Gogoi user avatar
Anupam Gogoi
·
Jan. 12, 17 · Tutorial
Likes (14)
Comment
Save
Tweet
Share
12.3K Views

Join the DZone community and get the full member experience.

Join For Free

In this tutorial, we are going to create a simple WordPress connector to list out all the posts from a blog. To create Mule connectors, you need to have the Mule DevKit installed on your machine. For installation, please check the installation guide.

Step 1

After proper installation of the DevKit, let's create a simple Connector Project as shown in the diagram,

Create Connector Project

I have named the project rest-connector. In the next wizard window, please check the Anypoint Connector Project type as SDK-based. And that's it. Upon finishing, Anypoint Studio will take some time to download the necessary libraries, and then your project will be ready. Here is the project structure.

Project Structure

Step 2

If you notice in the project created in Step 1, the Anypoint Studio generates two classes automatically:

  1. RestConnector.java.

  2. ConnectorConfig.java.

And also generates some default codes. For simplicity I have removed the auto generated codes and kept it clean.

Step 3

WordPress exposes some very useful REST APIs. You can take a look here. On the right-hand side of the site, you can see a list of available REST services divided into categories. I chose the Posts category and picked up the "get a list of matching posts" service. The documentation explains clearly how to make calls to the service.

Step 4

Here is a small piece of Java code that demonstrates how to call the REST service. In this example, I am querying the title and URL of all the posts available on gocodeblog.wordpress.com. I am simply printing out the JSON response to the console. 

import org.springframework.web.client.RestTemplate;

public class Test {

public static void main(String[] a) {
new Test().getPosts("ID,title");

}

public void getPosts(String field) {
RestTemplate template = new RestTemplate();

String url = "https://public-api.wordpress.com/rest/v1.1/sites/";
url = url.concat("gocodeblog.wordpress.com").concat("/posts?fields=").concat(field);
String result = template.getForObject(url, String.class);
System.out.print(result);
}
}

This is what my custom connector is going to return as result.

Step 5

In this step, we are going to write the ConnectorConfig.java. Here, I have only one property, called site, which corresponds to the WordPress blog. Note that the property value must not contain http:// or https:// as a prefix. I just put a tooltip informing this. Here is the source code.

package com.anupam.rest.config;

import org.mule.api.annotations.Configurable;
import org.mule.api.annotations.components.Configuration;
import org.mule.api.annotations.display.Summary;

@Configuration(friendlyName = "Configuration")
public class ConnectorConfig {

@Configurable
@Summary(value="Please enter the blog url without the prefix http:// or https://")
    private String site;

public String getSite() {
return site;
}

public void setSite(String site) {
this.site = site;
}



}

Step 6

Now, we are going to write our connector class, i.e., RestConnector.java. The code is self-explanatory:

package com.anupam.rest;

import org.mule.api.annotations.Config;
import org.mule.api.annotations.Connector;
import org.mule.api.annotations.Processor;
import org.mule.api.annotations.display.Summary;
import org.springframework.web.client.RestTemplate;

import com.anupam.rest.config.ConnectorConfig;

@Connector(name = "rest", friendlyName = "WordPress")
public class RestConnector {

@Config
ConnectorConfig config;

@Processor
public String getPosts(
@Summary(value = "Enter coma separated parameters.Acceptable parameters are: ID,title,URL") String field) {
RestTemplate template = new RestTemplate();

String url = "https://public-api.wordpress.com/rest/v1.1/sites/";
url = url.concat(config.getSite()).concat("/posts?fields=").concat(field);
String result = template.getForObject(url, String.class);
return result;
}

public ConnectorConfig getConfig() {
return config;
}

public void setConfig(ConnectorConfig config) {
this.config = config;
}

}

Note: The friendlyName="WordPress" attribute in the @Connector annotation means that the connector will be available as WordPress in the palettes section.

That's it. Now, browse to the root of the project and execute mvn clean install. After that, right-click on the project and hit Anypoint Connector > Install or Update. The Anypoint Studio will automatically install the palette in your Mule Palettes section.

Using the WordPress Connector

Let's use this connector in a simple flow. The flow is shown below in the diagram:

Simple Flow

In the configuration of the WordPress connector, please specify the site property as shown in this diagram:

Image title

In the General tab, specify the query parameters separated by commas. I have put the itle and URL.

We're done!

Testing

Run the Mule flow and fire a request to http://localhost:8082/rest using your browser or PostMan. You should receive the JSON response containing the blog posts titles and URLs.

You can find the source code of the connector project projec here. 

Connector (mathematics)

Opinions expressed by DZone contributors are their own.

Related

  • The Real-time Data Transfer Magic of Doris Kafka Connector's "Data Package": Part 1
  • Designing a Java Connector for Software Integrations
  • Kafka JDBC Source Connector for Large Data
  • Make Your Integration Seamless By Using Ballerina Client Connectors

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook