The sparking fame of the Spring Boot framework in the Java ecosystem dims the mighty but tiny microservice frameworks out there. There is no doubt that the Spring Boot is a well-known all-rounder. However, this article explains a lightweight microservice framework from the company named WSO2. As a former employee of WSO2, I can guarantee their expertise in different service-oriented solutions. The founder of WSO2: Sanjiva Weerawarana is a key contributor to Apache SOAP and Axis2 projects and his passion for web services also reflects on MSF4J.

Requirements

Create a New Hello World Project

Step 1:
Create a new Maven project in IntelliJ Idea with the group id: com.javahelps.helloworld and artifact id: msf4j-hello-world.

Step 2:
Add the following dependencies and repositories to the pom.xml file. Prior to Java 9, the MSF4J core jar itself was enough because until Java 8 some of the Java EE packages were part of Java SE. From Java 9, Java EE packages were stripped from the standard edition. Therefore the JAXB API binding dependency is required to run MSF4J if you are using Java 9 or later.

<dependency>
<groupId>org.wso2.msf4j</groupId>
<artifactId>msf4j-core</artifactId>
<version>${msf4j.version}</version>
</dependency>

<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>${jaxb.api.version}</version>
</dependency>
<repositories>
<repository>
<id>wso2.releases</id>
<name>WSO2 Internal Repository</name>
<url>https://maven.wso2.org/nexus/content/repositories/releases/</url>
<releases>
<enabled>true</enabled>
<updatePolicy>daily</updatePolicy>
<checksumPolicy>fail</checksumPolicy>
</releases>
</repository>

<repository>
<id>wso2-maven2-repository</id>
<name>WSO2 Maven2 Repository</name>
<url>https://dist.wso2.org/maven2</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
<releases>
<enabled>true</enabled>
<updatePolicy>never</updatePolicy>
<checksumPolicy>fail</checksumPolicy>
</releases>
</repository>
</repositories>

After adding the dependencies, the pom.xml should look like this:

Not all newer versions of libraries guarantee backward compatibility with previous versions. Therefore, I recommend using the same version of the library as in the following pom.xml for your first run. Once you get the code working, search for the artifact ids in the public Maven Repository and use the latest version of the dependencies in your production code. Please note that older versions may have unpatched vulnerabilities and make your code vulnerable to attacks. I will try my best to keep the articles up to date but it is impossible for me to keep track of all libraries used in Java Helps. If you find any breaking changes in newer versions, please comment below.

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.javahelps.helloworld</groupId>
<artifactId>msf4j-hello-world</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<msf4j.version>2.8.7</msf4j.version>
<jaxb.api.version>2.3.1</jaxb.api.version>
</properties>

<dependencies>
<dependency>
<groupId>org.wso2.msf4j</groupId>
<artifactId>msf4j-core</artifactId>
<version>${msf4j.version}</version>
</dependency>

<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>${jaxb.api.version}</version>
</dependency>
</dependencies>

<!-- Use the WSO2 repositories -->
<repositories>
<repository>
<id>wso2.releases</id>
<name>WSO2 Internal Repository</name>
<url>https://maven.wso2.org/nexus/content/repositories/releases/</url>
<releases>
<enabled>true</enabled>
<updatePolicy>daily</updatePolicy>
<checksumPolicy>fail</checksumPolicy>
</releases>
</repository>

<repository>
<id>wso2-maven2-repository</id>
<name>WSO2 Maven2 Repository</name>
<url>https://dist.wso2.org/maven2</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
<releases>
<enabled>true</enabled>
<updatePolicy>never</updatePolicy>
<checksumPolicy>fail</checksumPolicy>
</releases>
</repository>
</repositories>
</project>
pom.xml

Implement an MSF4J Service

Step 1:
Create a new class named HelloService.java with the following code. I hope the code is self-explanatory.

package com.javahelps.helloworld;

import javax.ws.rs.*;

@Path("/service")
public class HelloService {

@GET
@Path("/")
public String get() {
System.out.println("GET invoked");
return "Hello world!";
}

@POST
@Path("/")
public void post() {
System.out.println("POST invoked");
}

@PUT
@Path("/")
public void put() {
System.out.println("PUT invoked");
}

@DELETE
@Path("/")
public void delete() {
System.out.println("DELETE invoked");
}
}

Step 2:
Create a MicroservicesRunner and deploy the HelloService in the main method of the Main class as shown below.

package com.javahelps.helloworld;

import org.wso2.msf4j.MicroservicesRunner;

public class Main {
public static void main(String[] args) {
new MicroservicesRunner()
.deploy(new HelloService())
.start();
}
}

Run the MSF4J Service

After saving all the changes run the Main class from IntelliJ IDEA. By default, the web service will run on port 9090.

Using the Postman or CURL, send an HTTP request to any of the endpoints defined in the HelloService class. The following CURL command sends an HTTP GET request to the service.

curl http://localhost:9090/service

I hope you find MSF4J simple enough and lightweight for your needs. Still, Spring Boot has its advantages due to the large community behind it and the unlimited list of features. For long-term projects, choosing a framework backed by a large community with frequent bug/security fixes is important. However, if you are looking for lightweight alternatives to Spring Boot for a valid reason, MSF4J is definitely something worth considering.

You can download the complete source code of this project along with all the resources from our Git Hub repository.

If you find this article useful, please share your thoughts below. If you have any questions or issues with getting MSF4J working, you can ask your questions in the comments. Java Helps community will try our best to answer your questions.

Share.
Exit mobile version