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
Please enter at least three characters to search
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

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

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

  • Securing the Future: Best Practices for Privacy and Data Governance in LLMOps
  • Is Big Data Dying?
  • Using Python Libraries in Java
  • Bridging UI, DevOps, and AI: A Full-Stack Engineer’s Approach to Resilient Systems

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
Oops! Something Went Wrong

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
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!