Last Updated: March 26, 2021
This section shows how to create a Mature REST Microservice service client in no-time.
Pre-Requisites:
-
Run the Microservice
Steps:
-
Create Maven project 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-mature-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 config.properties in src/main/resources, as the following:
app.services.persons.url=http://localhost:8080/app/persons
-
Create the Model class at src/main/java/com/app/person/Model.java, such as the following:
package com.app.person;
import java.io.Serializable;
import javax.persistence.*;
public class Model implements Serializable {
private Integer id;
private String nationalId;
private String name;
private String email;
private String address;
public void setId(Integer id) {
this.id = id;
}
public Integer getId() {
return this.id;
}
public void setNationalId(String nationalId) {
this.nationalId = nationalId;
}
public String getNationalId() {
return this.nationalId;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return this.name;
}
public void setEmail(String email) {
this.email = email;
}
public String getEmail() {
return this.email;
}
public void setAddress(String address) {
this.address = address;
}
public String getAddress() {
return this.address;
}
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
return this.getId().equals(((Model) obj).getId());
}
@Override
public int hashCode() {
if (this.id == null) {
return toString().hashCode();
}
return this.id.hashCode();
}
@Override
public String toString() {
StringBuffer buf = new StringBuffer();
buf.append(this.id).append(",");
buf.append(this.nationalId).append(",");
buf.append(this.name).append(",");
buf.append(this.email).append(",");
buf.append(this.address).append(",");
return buf.toString();
}
}
-
Create ServiceClient class at src/main/java/com/app/person/ServiceClient.java as follows:
package com.app.person;
import com.jk.web.services.client.JKMatureServiceClient;
public class ServiceClient extends JKMatureServiceClient<Model>{
@Override
public String getServiceUrlPropertyName() {
return "app.services.persons.url";
}
}
-
Create App class at src/main/java/com/app/App.java for quick testing:
package com.app;
import java.util.List;
import com.app.person.Model;
import com.app.person.ServiceClient;
import com.jk.util.JK;
public class App {
private static Model createdAccount;
public static void main(String[] args) {
ServiceClient client = new ServiceClient();
addRecord(client);
updateRecord(client);
printAll(client);
find(client);
delete(client);
// find(client); this will throw an exception with 404 page not found
}
private static void updateRecord(ServiceClient client) {
createdAccount.setName("Updated - Jalal");
client.update(createdAccount);
}
private static void delete(ServiceClient client) {
client.delete(createdAccount.getId());
}
private static void find(ServiceClient client) {
// Retrieve single account
Model account = client.find("/" + createdAccount.getId());
JK.printBlock(account);
}
private static void addRecord(ServiceClient client) {
Model account = new Model();
account.setNationalId("123456789");
account.setName("Jalal Kiswani");
account.setEmail("[email protected]");
account.setAddress("Reno, NV");
createdAccount= client.insert(account);
}
private static void printAll(ServiceClient client) {
// Retrieve all account
List<Model> list = client.getAll();
for (Model record : list) {
JK.print(record);
}
}
}
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-mature-client |