Let's create a simple calculator with a couple of functions as an example of a WebService. The calculator will compute the squareRoot and square of any number. It will return a simple JSON response with the name of the action, the input and the output.
Before we start this is the Gradle configuration you will need:
This is the code for the Calculator:
The annotations determine the type of REST action to be applied to the method @GET, @PUT etc. The
In order to deploy our WebService we need a ServletContainer for which we will use Jersey and a WebServer into which we can drop the container for which we will use Jetty.
This is the code for the RestServer:
Once you have run the RestServer you will be able to test it with this URL.
A really nice way to run queries from IntelliJ is to use the inbuilt REST Client which can be found under the tools menu.
When you run the REST Client you will get this Response:
It's a really easy way to test a RESTful server.