Standardise and accelerate development using reusable API Templates in Mule 4.x

Reading Time: 4 minutes

The MuleSoft template project is very useful for defining standards for designing Mule applications (xAPI, pAPI, sAPI) across your enterprise and used as a prototype to build similar applications with enterprise-defined standards, naming conventions, common reusable assets/dependencies and to share and reuse it across the organisation.

Key features

Defined enterprise-level standards across your API layers (xAPI, pAPI, sAPI):

a. JSON-Logger
b. Naming conventions
c. Property File structure (yaml/ properties) with predefined naming conventions/attributes

Usage of common artifacts/dependencies across all Mule applications

a. JSON-Logger
b. global-error-handler
c. repeated dependencies (ex: HTTP connector, socket connector, api-kit router etc.)
d. Domain projects (if anything for on-prem deployment)

Defined common configurations across API layer

a. Importing common properties ({mule.env}-dev.yaml , {mule.env}-secure.yaml)
b. Importing common HTTP requester configuration

Asset creation/configuration

1. Create a new project from Anypoint Studio
a. Project Name : mule-application-template-api

2. Define your artifactId, description in pom.xml
a. Define the below fields
i. groupId : << Your Org Business Grp Id >>
ii. artifactId and name as Project Name (ex: “mule-application-template-api”)
iii. Configuration — classifier in mule-maven-plugin as “mule-application-template”

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

<project xmlns=”http://maven.apache.org/POM/4.0.0″ xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xsi:schemaLocation=”http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd”>

<modelVersion>4.0.0</modelVersion>

<groupId>72eb07d7–54ae-5c71–8b44–083d4de63cb7</groupId>

<artifactId>mule-application-template-api</artifactId>

<version>1.0.0</version>

<packaging>mule-application</packaging>

<name>mule-application-template-api</name>

<properties>

<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

<app.runtime>4.4.0</app.runtime>

<mule.maven.plugin.version>3.8.2</mule.maven.plugin.version>

</properties>

<build>

<plugins>

<plugin>

<groupId>org.apache.maven.plugins</groupId>

<artifactId>maven-clean-plugin</artifactId>

<version>3.2.0</version>

</plugin>

<plugin>

<groupId>org.mule.tools.maven</groupId>

<artifactId>mule-maven-plugin</artifactId>

<version>${mule.maven.plugin.version}</version>

<extensions>true</extensions>

<configuration>

<classifier>mule-application-template</classifier>

<! — deployment configuration →

</configuration>

</plugin>

</plugins>

</build>

<dependencies>

<dependency>

<groupId>org.mule.connectors</groupId>

<artifactId>mule-http-connector</artifactId>

<version>1.7.3</version>

<classifier>mule-plugin</classifier>

</dependency>

<dependency>

<groupId>org.mule.connectors</groupId>

<artifactId>mule-sockets-connector</artifactId>

<version>1.2.2</version>

<classifier>mule-plugin</classifier>

</dependency>

<dependency>

<groupId>org.mule.modules</groupId>

<artifactId>mule-apikit-module</artifactId>

<version>1.8.2</version>

<classifier>mule-plugin</classifier>

</dependency>

<dependency>

<groupId>com.mulesoft.connectors</groupId>

<artifactId>mule-amqp-connector</artifactId>

<version>1.6.0</version>

<classifier>mule-plugin</classifier>

</dependency>

<dependency>

<groupId>com.mulesoft.modules</groupId>

<artifactId>mule-secure-configuration-property-module</artifactId>

<version>1.2.7</version>

<classifier>mule-plugin</classifier>

</dependency>

<dependency>

<groupId>org.mule.connectors</groupId>

<artifactId>mule-jms-connector</artifactId>

<version>1.8.4</version>

<classifier>mule-plugin</classifier>

</dependency>

<dependency>

<groupId>72eb07d7–54ae-5c71–8b44–083d4de63cb7</groupId>

<artifactId>json-logger</artifactId>

<version>2.1.0</version>

<classifier>mule-plugin</classifier>

</dependency>

<dependency>

<groupId>72eb07d7–54ae-5c71–8b44–083d4de63cb7</groupId>

<artifactId>global-error-handler</artifactId>

<version>1.0.0</version>

<classifier>mule-plugin</classifier>

</dependency>

</dependencies>

<repositories>

<repository>

<id>anypoint-exchange-v3</id>

<name>Anypoint Exchange</name>

<url>https://maven.anypoint.mulesoft.com/api/v3/maven</url>

<layout>default</layout>

</repository>

<repository>

<id>mulesoft-releases</id>

<name>MuleSoft Releases Repository</name>

<url>https://repository.mulesoft.org/releases/</url>

<layout>default</layout>

</repository>

</repositories>

<pluginRepositories>

<pluginRepository>

<id>mulesoft-releases</id>

<name>MuleSoft Releases Repository</name>

<layout>default</layout>

<url>https://repository.mulesoft.org/releases/</url>

<snapshots>

<enabled>false</enabled>

</snapshots>

</pluginRepository>

</pluginRepositories>

<distributionManagement>

<! — Target Anypoint Organization Repository →

<repository>

<id>anypoint-exchange-v3</id>

<name>Anypoint Exchange v3 Repository</name>

<url>https://maven.anypoint.mulesoft.com/api/v3/organizations/${project.groupId}/maven</url>

<layout>default</layout>

</repository>

</distributionManagement>

</project>

3. Define dependencies in pom.xml
a. JSON-logger
b. global-error-handler (if any)
c. Common libraries (ex: HTTP, socket etc.)

4. Define global configuration (ex: global-config.xml)

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

<mule xmlns:tls=”http://www.mulesoft.org/schema/mule/tls” xmlns:apikit=”http://www.mulesoft.org/schema/mule/mule-apikit

xmlns:secure-properties=”http://www.mulesoft.org/schema/mule/secure-properties

xmlns:api-gateway=”http://www.mulesoft.org/schema/mule/api-gateway” xmlns:error-handler-plugin=”http://www.mulesoft.org/schema/mule/error-handler-plugin” xmlns:json-logger=”http://www.mulesoft.org/schema/mule/json-logger” xmlns:os=”http://www.mulesoft.org/schema/mule/os” 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: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.mulesoft.org/schema/mule/http

http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd

http://www.mulesoft.org/schema/mule/os

http://www.mulesoft.org/schema/mule/os/current/mule-os.xsd

http://www.mulesoft.org/schema/mule/json-logger http://www.mulesoft.org/schema/mule/json-logger/current/mule-json-logger.xsd

http://www.mulesoft.org/schema/mule/error-handler-plugin http://www.mulesoft.org/schema/mule/error-handler-plugin/current/mule-error-handler-plugin.xsd

http://www.mulesoft.org/schema/mule/api-gateway http://www.mulesoft.org/schema/mule/api-gateway/current/mule-api-gateway.xsd

http://www.mulesoft.org/schema/mule/secure-properties http://www.mulesoft.org/schema/mule/secure-properties/current/mule-secure-properties.xsd

http://www.mulesoft.org/schema/mule/mule-apikit http://www.mulesoft.org/schema/mule/mule-apikit/current/mule-apikit.xsd

http://www.mulesoft.org/schema/mule/tls

http://www.mulesoft.org/schema/mule/tls http://www.mulesoft.org/schema/mule/tls/current/mule-tls.xsd“>

<http:listener-config name=”httpListenerConfig”>

<http:listener-connection host=”0.0.0.0” port=”8081“>

</http:listener-connection>

</http:listener-config>

<configuration-properties doc:name=”Configuration properties” doc:id=”a7eb7eac-877c-47dc-b489–8f5e4d9df3b2” file=”properties\mule-application-template-${mule.env}.yaml” />

<api-gateway:autodiscovery apiId=”${api.autodiscovery}” ignoreBasePath=”true” doc:name=”API Autodiscovery” doc:id=”6a911920–6a3b-491d-9159-c10ea843f504” flowRef=”mule-application-template-api-main”/>

<http:request-config name=”examplePrc_HTTP_Request_configuration” doc:name=”HTTP Request configuration” doc:id=”558d644d-e758–4c58–843d-61a1d11e5f60” >

<http:request-connection protocol=”${example.prc.api.protocol}” host=”${example.prc.api.host}” port=”${example.prc.api.port}”>

<tls:context >

<tls:trust-store insecure=”true” />

</tls:context>

</http:request-connection>

<http:default-headers >

<http:default-header key=”client_id” value=”${secure::example.prc.api.client_id}” />

<http:default-header key=”client_secret” value=”${secure::example.prc.api.client_secret}” />

</http:default-headers>

</http:request-config>

<http:request-config name=”exampleSys_HTTP_Request_configuration” doc:name=”HTTP Request configuration” doc:id=”1e0e7fc9-f776–4e38–91e7–8ec7f7b81ff7” >

<http:request-connection protocol=”${example.sys.api.protocol}” host=”${example.sys.api.host}” port=”${example.sys.api.port}”>

<tls:context >

<tls:trust-store insecure=”true” />

</tls:context>

</http:request-connection>

<http:default-headers >

<http:default-header key=”client_id” value=”${secure::example.sys.api.client_id}” />

<http:default-header key=”client_secret” value=”${secure::example.sys.api.client_secret}” />

</http:default-headers>

</http:request-config>

<import doc:name=”Import” doc:id=”9bf9de97-e41d-4659-b100-d6b439f35be4” file=”global-error-handler.xml” />

<json-logger:config name=”JSON_Logger_Config” doc:name=”JSON Logger Config” doc:id=”4f8da8f2–6658–4cfd-9392–632e3a8f23b0” environment=”${mule.env}” />

<secure-properties:config name=”Secure_Properties_Config” doc:name=”Secure Properties Config” doc:id=”c23cd335-c0da-4d73-a07f-1c042c9731a9” file=”properties\mule-application-template-api-secure-${mule.env}.yaml” key=”${enc.key}” />

<configuration-properties doc:name=”Configuration properties” doc:id=”1b1a2b63-fb65–4aae-b51d-6f6fe105d146” file=”properties\common.yaml” />

</mule>

5. Environment related files: {mule.env}.yaml and {mule.env}-secure.yaml file-related configurations
a. mule-application-template-dev.yaml

json.logger:

application:

version: “v1”

name: “example-exp-api-dev”

Ex: Application owner can define protocol, host, port, etc.,

b. mule-application-template-secure-dev.yaml

Example.prc.api:

client_id: “![]”

client_secret: “![]”

example.sys.api:

client_id: “![]”

client_secret: “![]”

Ex: The application owner can define headers in a secure mode. Client id/Client Secret

6. Mule application template — skeleton : pictorial view

Skeleton view of the Mule application template.

Asset publish activity

mvn clean deploy

Asset Status in the Anypoint Exchange (Asset is published in the Anypoint Exchange).

Status of Mule Application Template in Anypoint Exchange.

We will now use this Mule template project to create a new Mule project (Mule Application: example-exp-api).

Project creation/configuration

a. Open Anypoint Studio

b. Click New > File > project from Template.

Create a new project from Template.

Note: It may ask for your organisation’s login credentials for the Anypoint Platform if not logged in.

c. Application owner can search the existing mule template (ex: mule-application-template-api)

Mule Application Template API in Anypoint Exchange.

d. Click Open, and the Project will appear in your Anypoint Studio.

e. Mule application owner can rename the project and files as per requirements.

Mule Application after renaming it from the Mule application template.

f. Save the current project (ex: example-exp-api) and perform deployment for further testing.

Deployment Status.

g. Application owner can directly test /healthcheck endpoint from POSTMAN or Internet browser. Note: please ensure you are using the correct URL for your test.

API Console View.
Test result of /health check endpoint from API Console.
Test: /healthcheck endpoint from POSTMAN.

Conclusion

This will be useful for green field implementation or re-designing your APIs to standardise application format, consistency, adhering to naming standards, reducing efforts for API governance, quick implementation, and Demo/PoC for API-led connectivity on the MuleSoft Anypoint Platform.

Mule Upgrade Tech Guide

Grab your copy

IT Managed Services

Lets get started

Let’s get started.
Reach out to us
for a conversation!

Fill in the form below and we will get back to you

Don’t Miss Out!

Stay in the loop on the latest in the wild world of technology and AI. Subscribe for updates on our newest articles and exclusive events, it just Makes Sense!