Simple Microservice Client Using J-Framework

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

Prerequisites:

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

  2. You should have a simple microservice running. (Click Here)

  3. You should create a Maven Project.

  4. 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-web</artifactId>
		<version>7.0.0-SNAPSHOT</version>
	</parent>
	<artifactId>j-framework-microservice-client-example</artifactId>
</project>
  1. Java Person class is located at src/main/java/com/app/Person.java that has the following content:

package com.app;

public class Person {

	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. Server URL Configuration file located at src/main/resources/config.properties which contains the following:

services.example.url=http://localhost:8080/app/example
  1. Main java class located at src/main/java/com/app/App.java which contains the following:

package com.app;

import com.jk.core.util.JK;
import com.jk.services.client.JKServiceClient;

public class App {
	public static void main(String[] args) {
		String url = "http://localhost:8080/app/example";
		JKServiceClient<Person> client = new JKServiceClient<>(url, Person.class);

		String response = client.callJsonAsString("/hello");
		JK.print(response);

		String response2 = client.callJsonAsString("/hello/Jalal");
		JK.print(response2);

		Person p = new Person();
		p.setName("Jalal");
		p.setAge(40);

		String response3 = client.callJsonWithPost("/hello", p);
		JK.print(response3);

		String response4 = client.callJsonWithPatch("/hello", p);
		JK.print(response4);
	}
}

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 and show you the result of the run in the console.

console output

Example Explanation

  1. The first output line the client receives is the result from the server sending their default greetings.

  2. The second output line is a custom greeting message that contains the client’s name in it.

  3. The final line of the output is a custom greeting message that contains both the client’s name and their age retrieved from the Person object’s attributes which represent the client.

  4. Check the main method to see how the client and server communicated to produce this output.