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

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

How does AI transform chaos engineering from an experiment into a critical capability? Learn how to effectively operationalize the chaos.

Data quality isn't just a technical issue: It impacts an organization's compliance, operational efficiency, and customer satisfaction.

Are you a front-end or full-stack developer frustrated by front-end distractions? Learn to move forward with tooling and clear boundaries.

Developer Experience: Demand to support engineering teams has risen, and there is a shift from traditional DevOps to workflow improvements.

Related

  • Designing a Java Connector for Software Integrations
  • Kafka JDBC Source Connector for Large Data
  • Make Your Integration Seamless By Using Ballerina Client Connectors
  • How to Integrate a Distributed Database With Event Streaming

Trending

  • Memory Leak Due to Uncleared ThreadLocal Variables
  • Create POM With LLM (GitHub Copilot) and Playwright MCP
  • Complete Guide: Managing Multiple GitHub Accounts on One System
  • Modernizing Financial Systems: The Critical Role of Cloud-Based Microservices Optimization

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.1K 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

  • Designing a Java Connector for Software Integrations
  • Kafka JDBC Source Connector for Large Data
  • Make Your Integration Seamless By Using Ballerina Client Connectors
  • How to Integrate a Distributed Database With Event Streaming

Partner Resources

×

Comments

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

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • [email protected]

Let's be friends: