Build a rest service from the command line, as simple as “every request has a response.”
Following with Handmade Series with Java, this is the 3rd chapter. I’ll make a rest service, as always, step by step, without IDEs.
I’ll use the Spark library to getting a “pong” to every “ping” request.
I declare a simple class called PingPongGame with main method inside.
Inside of the main method, call a static method named get , provided by Spark library, that contains:
- A url path
- Receiving a
resobject from request.
- Which finally return a response, a simple string “pong”.
I add the current import from Spark library
spark.Spark.get, getting the code above:
Before executing it, download the spark library, from the maven repository.
After downloading it, place it inside PingPongGame class folder, it runs the next instruction through the command line
Cause Spark library by itself can’t run this instance, it depends on others libraries. The required jar libraries list:
After downloading them, run the long command line above:
So, at this point, I need to verify if the rest service is “up & running” by executing this command:
Again, simple and effective, perhaps not too simple cause of the amount of added libraries, but still isn’t to worry taking in count it made without IDEs.
- Pong to my ping needed.
- Creating a standar rest services.
- Using a method of a knowing library.
- Add it and test it.
- Problem happened(missing libraries).
- Adding missing libraries.
- Problem fixed.
- I got my pong trought rest service.
Command and options dictionary:
javarun .class and .java files.
-cpindicate compiled folders directory(classpath).
curlcommand to execute transfer to or from a server.
- Java 11.
- Windows 10.
Spark-java is a “microframework” as springboot but only based on functional programming with a simple implementation, I invited you to take in count in your read list the next article, to a deeper understanding.