Java REST web service with Gradle
12 Dec 2017In 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.
REST
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.
- https://blog.mwaysolutions.com/2014/06/05/10-best-practices-for-better-restful-api/
- https://hackernoon.com/restful-api-designing-guidelines-the-best-practices-60e1d954e7c9
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
- Configuration file: It will reside under
yourRootModuleFolder/src/main/webapp/WEB-INF/web.xml - Java source codes: They are normally put under
yourRootModuleFolder/src/main/java/
Gradle plugins
WARplugin: It is used to build and generate.warfile (WAR = Web application Archive) which includes JAR, JavaServer Pages, Java Servlets, XML, HTML, and other resource files. The.warfile later will be deployed in web server.apply plugin: "war"- Gretty: A handy plugin to run web apps on embedded servlet container. It supports Tomcat and Jetty.
Java REST framework
Jersey simplifies REST web service development and abstracts low-level implementation details of the client-server communication.
Build
- Test and build
./gradlew build - Run web app:
./gradlew appRunIt is now successfully served under url
localhost:8080/api/notes. You can use a browser to check it out or you can test REST API using Postman which use can test bothGETandPOSTAPIs.
Deployment
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.
- Download and install
- Copy
.warfile inside project’sbuild/folder - Paste it into
webapps/folder oftomcatorjetty - Run server. Go inside
bin/folder oftomcatorjettyand execute either following script.- Tomcat:
$./catalina.sh - Jetty:
$./jetty.sh start
- Tomcat: