Getting Started With WSO2 Microservices Framework for Java

Engineer, Tech Lead and Technopreneur

WSO2 Microservices Framework for Java (MSF4J) is lightweight, fast runtime and annotation-based programming model offers the best option to create microservices in Java.

To getting started with MSF4J is very easy. You need to have following software installed in  your machine to get started.

  • JDK 1.8
  • Maven 3

Open a terminal and run below command to create the project structure of your first MSF4J project

mvn archetype:generate -DarchetypeGroupId=org.wso2.msf4j \
-DarchetypeArtifactId=msf4j-microservice -DarchetypeVersion=1.0.0 \
-DgroupId=org.example -DartifactId=Hello-Service -Dversion=1.0.0-SNAPSHOT \
-Dpackage=org.example.service -DserviceClass=HelloService

Once project is created, open org.example.service.HelloService class and replace its contents with following


package org.example.service; 

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.QueryParam;

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

 @GET
 @Path("/{name}")
 public String hello(@PathParam("name") String name, @QueryParam("username") String username) {
    return "Hello " + username + " : from " + name;
 }

}

Open org.example.service.Application class and replace its contents with following


package org.example.service;

import org.wso2.msf4j.MicroservicesRunner;

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

Now go to the terminal and build the package

mvn package

Run your micro service project

java -jar target/Hello-Service-*.jar

That’s it. Open http://localhost:8080/hello/wso2?username=Dilan to test your micro service that you’ve written in Java

Awesome! You will see this response form your first micro service. Change the value of wso2 (path parameter) and Dilan (query parameter) of the above URL to get different outputs

To read more about MSF4J, visit http://wso2.com/products/microservices-framework-for-java/

2,149 total views, 1 views today

 

Feel Free to ask questions and share your thoughts