Java REST web service with Gradle

In this blog post, we are just walking through fundamental steps of developing and running a REST web service by using Java Servlet and Gradle build tool.


What is REST?

Basically, REST stands for Representational State Transfer which is an architectural style based on HTTP protocol. REST concept is to separate the API structure into logical resources. HTTP methods (GET, POST, PUT, and DELETE) are used to operate with the resources.
About RESTful API design, you can learn more about the guidelines via following articles.

Java Servlet

A servlet is a Web component that is managed by a container and generates dynamic content. Servlets are Java classes that are compiled to byte code that can be loaded dynamically into and run by a Java technology-enabled Web server or Servlet container.

Servlets run in a servlet container which handles the networking side. So, they are usually used to implement web applications which can respond to a particular type of network request, such as HTTP request.

So, we use it to build a web service which will run on an application server like Tomcat or Jetty.

Get our hands dirty

It is time to show me your codes. All sample codes can be found at Github.

Project structure

Gradle plugins

Java REST framework

Jersey simplifies REST web service development and abstracts low-level implementation details of the client-server communication.



In development, we use Gretty plugin which automatically downloads web server and deploys web application for us. If we want to deploy in our production environment, we need to do it manually. Fortunately, it requires a few basic steps.
While Gretty only supports Tomcat and Jetty, you can choose a suitable sever for your need from this list of open-source web servers. The following steps of deployment process are general but I use Tomcat and Jetty as examples.

  1. Download and install
  2. Copy .war file inside project’s build/ folder
  3. Paste it into webapps/ folder of tomcat or jetty
  4. Run server. Go inside bin/ folder of tomcat or jetty and execute either following script.
    • Tomcat: $./
    • Jetty: $./ start