Microservices Framework for Java (MSF4J) - HelloWorld!

In a recent article: Microservices in a minute, I have introduced a lightweight microservice framework: WSO2 MSF4J. That tutorial shows you how to create a microservice in minutes using the Maven archetype. However, the libraries available in the public Maven repositories are bit older and there are new releases after MSF4J 2.0.1 which are available in WSO2's Maven repository. This article shows you how to create a microservice using the latest stable release of MS4J Framework.

Requirements:
  • Java Development Kit 1.8
  • Apache Maven 3.x
  • Postman or CURL
  • Eclipse IDE for Java EE Developers / Any IDEs with Maven support
Step 1:
Create a new Maven project in Eclipse using the following properties:
Group Id:   com.javahelps
Artifcat Id: msf4j-helloworld

Step 2:
Modify the pom.xml as shown below:
<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</groupId>
    <artifactId>msf4j-helloworld</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <!-- Define Java version and the MSF4J version -->
    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <msf4j.version>2.2.2</msf4j.version>
    </properties>

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

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


    <!-- Optionally deine the main class -->
    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.6.0</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>java</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <mainClass>com.javahelps.service.Application</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>
Note that the properties tag defines the Java version which must be 1.8 or greater and the MSF4J version which is 2.2.2 (the latest stable release at the time of writing). The repository containing the latest MSF4J builds is included using the repositories tag and the msf4j-core is added as the dependency. Finally the build plugin is used to define the main class which is optionaly if you do not want to create the JAR file.

If you are using Eclipse, save all the changes and right click on the project, goto Maven → Update Project... and click OK on the apperaed dialog to import the changes we have done in the pom file.
Step 3:
Create a new package 'com.javahelps.service' in src/main/java directory.

Step 4:
Create a new service class named HelloService.java with the following code.
package com.javahelps.service;

import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;

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

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

    @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");
    }
}
This code defines the base URL '/service' at the begining of the class definition using @Path annotation and the Java methods to handle the HTTP methods: GET, POST, PUT and DELETE are defined.

Step 5:
Create another class Application.java inside the same package. Remember this is the main class defined in the pom file.
package com.javahelps.service;

import org.wso2.msf4j.MicroservicesRunner;

public class Application {

    public static void main(String[] args) {
        new MicroservicesRunner()
        .deploy(new HelloService())
        .start();
    }
}
This class creates an instance of the HelloSevice class and deply it as a service at the default port: 8080. If you want to change the default port, you can pass it as a parameter to the MicroservicesRunner constructor.

Microservices Framework for Java (MSF4J) - HelloWorld

Step 6:
Now, this is the time to run our microservice. You can simply right-click on the Application.java and run it as a Java application but I prefer to use Apache Maven, so the Maven command to build the JAR file and execute the microservice is given below:
mvn clean package exec:java

Step 7:
To test the service, send a request from POSTMAN or any other tools. This time I use curl to send a GET request to the service.
curl http://localhost:8080/service

Microservices Framework for Java (MSF4J) - HelloWorld

I hope you have enjoyed the simplicity of MSF4J. Feel free to comment below if you have any questions or doubts.

Find the project @ GitHub.
Previous
Next Post »

Contact Form

Name

Email *

Message *