In this class, I will demonstrate the usage of all four HTTP methods. If you have previous experience with microservices, the material should be easily comprehensible.
Information
This code fetches player data from MySQL for Spigot via a GET request with UUID as parameter. The SQL query selects the player record and returns first result as player data. Additional functionality such as inserting records recommended.
Service files are usually nested in the /service(s)/* folder in your app. Files that end
with .service.ts, hold the logic for when they get requested from our Spigot server.
service/player.service.ts
import { Router } from'@spigotmc/microservice';import { connection } from'..';/** * Create a new router for this service. */constservice=newRouter();/** * This is only one example of four methods */service.get('/load', (query) => {/** * The UUID of the player gets passed through * the query because this is a GET request. */constuuid:string=query.uuid;/** * This is the query that gets executed. */constsql=`SELECT * FROM players WHERE uuid='${}'`;/** * Execute the query and wait for the result. */returnnewPromise((resolve, reject) => {connection.query(sql, (error, results) => {/** * Reject the promise if an error occured. */if (error) returnreject(error);/** * Resolve the promise with player's data. */resolve(); }); });});/** * Other endpoints like `/coin_update`, etc., * should be implemented in this same file. */export { service as playerService };
PlayerRepository.java
importmicro.api.service.*;importmicro.api.payload.*;publicclassPlayerRepositoryextendsServiceEndpoint { /** * This points to `http://localhost:5000/api/player` */publicPlayerRepository() { super("/player"); } /** * Example of GET request, get player's data. * @return a new PlayerClient object. */publicPlayerClientloadPlayer(UUID uuid) {Query query =newQuery();query.add("uuid",uuid.toString());return super.get("/load",PlayerClient.class, query); } /** * Example of POST request, update player's coins. * @return The updated PlayerClient object. */publicPlayerClientaddFriend(UUID uuid,UUID target) {Body body =newBody();body.add("uuid",uuid.toString());body.add("target",target.toString());return super.post("/friend_add",PlayerClient.class, body); } /** * Example of PUT request, update player's coins. * @return The updated PlayerClient object. */publicPlayerClientupdateCoins(UUID uuid,int amount) {Body body =newBody();body.add("uuid",uuid.toString());body.add("coins", amount);return super.put("/coin_update",PlayerClient.class, body); } /** * Example of DELETE request, delete player data. * @return The deleted PlayerClient object. */publicPlayerClientdeletePlayer(UUID uuid) {Query query =newQuery();query.add("uuid",uuid.toString());return super.delete("/delete",PlayerClient.class, query); }}
Once you made your first service file, you should import this in the /api router.
routes.ts
import { Router } from'@spigotmc/microservice';import { playerService } from'./services/player.service';/** * Create a new Router from the Microservice library. */constrouter=newRouter();/** * Add any sub-routes to the main router. */router.use('/player', playerService);/** * Export the router so that we can use * it in the main class where you decl- * ared the microservice. */exportdefault router;
PlayerClient.java
publicclassPlayerClient { /** * These fields will get initialized when * loading the player from the service. */privateString uuid;privateint level;private rank;privateint coins;privateList<String> friends;publicUUIDgetUUID() { returnUUID.fromString(uuid); }publicintgetLevel() {return level; }publicgetRank() {return rank; }publicintgetCoins() {return coins; }publicList<String> getFriends() {return friends; }}