JKFramework Service Client
Service client is a project that aims to simplify Microservices and REST-API clients development.
It consists of two clients clients:
-
Simple Client that communicates with traditional microservices.
-
Mature Service Client that communicates with Mature Microservices.
Mature Microservice is a maciroservice that uses HTTP protocol methods instead of API specific URL as a common protocol between servers and clients. |
Dependency
Most likely, you will NOT need to add the direct dependency of this project, since its already included in the parent projects or the other framework projects. However, if you need to, the dependency is
<!-- https://mvnrepository.com/artifact/com.jalalkiswani/jk-framework-core -->
<dependency>
<groupId>com.jalalkiswani</groupId>
<artifactId>jk-framework-service-client</artifactId>
<version>7.0.0-M6</version>
</dependency>
Traditional Service Client
To use it, declare ServiceClient
instance with the API URL base
and the common Model
as parameters,
then use that instance to call a the method of your need.
for example:
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);
The recommended approach is to:
-
Create a service client with the name of your microservice
-
It should extends the
ServiceClient
-
Override the
getBase()
method, and load the URL from the configuration. -
Provide a bushiness wrapper methods to all the end points in the Microservice.
Here is a full example:
package com.app;
import com.jk.core.config.JKConfig;
import com.jk.services.client.JKServiceClient;
public class DemoSeviceClient extends JKServiceClient<Person> {
@Override
public String getBase() {
return JKConfig.get().getProperty("example.service.url", "http://localhost:8080/app/example");
}
public String hello() {
return callJsonAsString("/hello");
}
public String hellowithParam(String name) {
return callJsonAsString("/hello/" + name);
}
public String helloWithModel(Person p) {
return callJsonWithPost("/hello", p);
}
}
and you use it with some thing like this:
DemoSeviceClient client=new DemoSeviceClient();
JK.print(client.hello());
JK.print(client.hellowithParam("Jalal"));
The service client and the needed models should be created by the Microservice developer him/her self,
and place it in a |
If you need to cast the response (which normally is a json object), you can use one of the following utility methods:
|
Mature Service Client
Mature Service clients is a client that is designed to communicate with Mature MicroService
. As explained,
mature microservice uses the standard HTTP methods as the standard application protocol between
an API and its client.
To use this client, you need to:
-
Class a client of your service that extends
JKMatureServiceClient
-
Provide your model as
Generic
parameter. -
Override
getBase
method.
Example:
package com.app.person;
import com.jk.core.config.JKConfig;
import com.jk.services.client.JKMatureServiceClient;
public class ExampleServiceClient extends JKMatureServiceClient<Model> {
@Override
public String getBase() {
return JKConfig.get().getProperty("example.service.url", "http://localhost:8080/app/example");
}
}
This client provide the following methods out of the box:
-
getAll()
which callsGET
method -
find(id)
which callsGET
method with query param; -
insert(model)
which callPOST
method -
update(model)
which callsPUT
method -
delete(id)
which callsDELETE
method. -