Initialization
Before we begin making requests to our microservice, we need to initialize its address and port as well as the route, this is how it should be done.
Information
You can find a sample repo on GitHub that's using this library, here.
You can really see this as passing JSON object between your server and your microservice, but instead of passing raw JSON, we can serialize and deserialize it into useful objects actually used in your code.
Initialization of the API
Here an example of how to initialize the API on Spigot and how to setup your Node.js environment.
Parameter definitions
.host()
String
localhost
The microservice host
.route()
String
'/api'
The main API route
.port()
int
8080
The microservice port
.protocol
Protocol
Protocol.HTTP
The HTTP protocol
Creating a new Microservice object
To create a object, we use the ServiceBuilder class.
/**
* Initialize it with default values like this;
*/
new ServiceBuilder().build();
/**
* Initialize it with custom values like this;
*/
new ServiceBuilder()
/* To change default values, add .port(), .()... here. */
.host("customhost.com")
.protocol(Protocol.HTTPS)
.build();
Establish the host of the API in your Java plugin, which will be utilized for requests.
import micro.api.*;
public final class Plugin extends JavaPlugin {
public ServiceHost host;
public PlayerRepository repo;
@Override
public void onEnable() {
/**
* This gives `http://localhost:8080/api` according
* to the default values stated here above.
*/
host = new ServiceBuilder().build().getHost();
repo = new PlayerRepository(host);
}
}
With that, the API has been successfully initialized on both your Java plugin and Node.js platform.
Last updated