Last Updated: March 26, 2021
Pre-Requisites:
-
Run the Microservice
This section shows how to create a simple REST Microservice service client in no-time.
Steps:
-
Create Maven project with pom.xml such as the following:
<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>
<groupId>com.jalalkiswani.examples</groupId>
<artifactId>smart-cloud-microservice-client-example</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<properties>
<smart-cloud.version>4.0.8</smart-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>com.jalalkiswani</groupId>
<artifactId>smart-cloud-service-client</artifactId>
<version>${smart-cloud.version}</version>
</dependency>
<dependency>
<groupId>com.jalalkiswani</groupId>
<artifactId>smart-cloud-web</artifactId>
<version>${smart-cloud.version}</version>
</dependency>
<dependency>
<groupId>com.jalalkiswani</groupId>
<artifactId>smart-cloud-standalone</artifactId>
<version>${smart-cloud.version}</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<!-- http://maven.apache.org/plugins/maven-compiler-plugin/ -->
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.3</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build>
</project>
If you are using Eclipse, be sure to refresh your project (select your project→ right click→ Maven→ Update Project) |
-
Create Person class as a model class at src/main/java/com/app/Person.java, such as the following:
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;
}
}
-
Create App class at src/main/java/com/app.App.java for quick testing:
package com.app;
import com.jk.util.JK;
import com.jk.web.services.client.JKServiceClient;
public class App {
public static void main(String[] args) {
JKServiceClient<Person> client = new JKServiceClient<>("http://localhost:8080/app/example", 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);
}
}
Run your App class, you should see something like this in your console:

Full example source-code can be found at https://github.com/smartapi-com/smart-cloud-microservice-client |