Simple Microservice Using J-Framework

This example demonstrates the usage of the j-framework in creating a simple microservice.

Prerequisites:

  1. You should have JDK 17+ Installed. (Click Here).

  2. You should create a Maven Project.

  3. You should enable snapshot versions. (Click Here).

Project Content:

This section contains what you should add to your code structure for this example.

  1. Maven Project with pom.xml that has the following contents:

<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>
	<parent>
		<groupId>com.jalalkiswani</groupId>
		<artifactId>j-app-webstack</artifactId>
		<version>7.0.0-SNAPSHOT</version>
	</parent>
	<artifactId>j-framework-microservice-example</artifactId>
	<packaging>war</packaging>
</project>
  1. Java Model class is located at src/main/java/com/app/person/Model.java that has the following content:

package com.app.person;

public class Model {

	private String name;

	private int age;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

}
  1. Controller located at src/main/java/com/app/person/Controller.java which contains the following:

package com.app.person;

import jakarta.ws.rs.GET;
import jakarta.ws.rs.PATCH;
import jakarta.ws.rs.POST;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.PathParam;

import com.jk.services.server.JKAbstractRestController;

@Path("/example")
public class Controller extends JKAbstractRestController{

	@Path("hello")
	@GET
	public String sayHello() {
		return "Hello from uncle Jalal";
	}

	@GET
	@Path("/hello/{name}")
	public String sayHelloWithPathParam(@PathParam(value = "name") String name) {
		return "Hello, " + name;
	}

	@POST
	@Path("/hello")
	public String sayHelloWithBody(Model p) {
		return "Hello, " + p.getName() + ", your age is: " + p.getAge();
	}

	@PATCH
	@Path("/hello")
	public String sayHelloWithBodyPatch(Model p) {
		return "Hello from Patch, " + p.getName() + ", your age is: " + p.getAge();
	}
}
  1. Main java class located at src/main/java/com/app/App.java which contains the following:

package com.app;

import jakarta.ws.rs.ApplicationPath;

import com.jk.services.server.JKServiceConfig;
import com.jk.web.embedded.JKWebApplication;

@ApplicationPath("app")
public class App extends JKServiceConfig{
	public static void main(String[] args) {
		JKWebApplication.run(8080,false);
	}
}

Alternatively, you can clone or download the tutorial repository then import the project into your IDE.

How to run Project:

  1. Set up the project with the content shown above.

  2. Inside your IDE, go to the src/main/java/com/app/App.java class.

Main Class

  1. Next, run it as a Java Application.

Run as Java Application

  1. Your program will start running, making the simple microservice available, you can make sure it’s up properly by opening any browser and connecting to localhost through port 8080.

localhost:8080

  1. You can now connect to the microservice using Simple Microservice Client or a Web Client.