J-Framework Service Client
Service client is a project that aims to simplify Microservices and REST-API client 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 microservice that uses HTTP protocol methods instead of API-specific URLs as a common protocol between servers and clients. |
Dependency
Most likely, you will NOT need to add the direct dependency of this project, since it’s already included in the parent projects or the other framework projects. However, if you need to, the dependency is added as follows.
<!-- https://mvnrepository.com/artifact/com.jalalkiswani/j-framework-core -->
<dependency>
<groupId>com.jalalkiswani</groupId>
<artifactId>j-framework-service-client</artifactId>
<version>7.0.0-SNAPSHOT</version>
</dependency>
Traditional Service Client
To use it, declare the ServiceClient
instance with the API URL base
and the common Model
as parameters,
then use that instance to call 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 extend the
ServiceClient
. -
Override the
getBase()
method, and load the URL from the configuration. -
Provide a business wrapper method to all the endpoints 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 something 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 developers themselves and placed 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 are clients that are designed to communicate with Mature Microservice
. As explained,
mature microservices use 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 a
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 provides the following methods out of the box:
-
getAll()
which callsGET
method -
find(id)
which callsGET
method with query param. -
insert(model)
which is called thePOST
method. -
update(model)
which calls thePUT
method. -
delete(id)
which is called theDELETE
method. -