The Spring Framework has in recent years emerged as a robust REST solution for Java
developers.
Introduction
In the second part of this series we are going to explore the Client-Server REST constraint. This constraint
postulates separation of concerns which allows the client and the server evolve independently. The client does not
need to worry about the server’s implementation, provided the server’s interface does not change.
We will re-implement the server using the Spring Frameworkbut maintaining the interfaces from the
previous post. The client will
continue to access it the same way without prior knowledge of the implementation.
For a basic introduction to rest, checkout the
first article in this series.
Project Structure
At the end of this guide our folder structure will look similar to the following:
Head over to the Spring Initializr website to generate a Spring project template:
Spring Initializr
Select Web and DevTools as dependencies and generate the project. DevTools is a handy tool to have during
development as it offers live reload when code changes.
Download and extract the template and let’s get to work .
Building Resources
Before we proceed, run the generated project to ensure it works:
If everything goes well, Tomcat should be started on port 8080 and wait for http requests.
Setting up dependencies
To build a RESTful webservice with Spring, and enable XML representation we can add the
jackson-datatype-xml to our pom.xml:
For a production ready application, you will normally connect to a database. For the purpose of this tutorial,
we will use a static field to initialize our list.
In the ResourceService we have specified the root context path (/api/v1.0/resources) we are going to access the
service.
In the same service class we have also created a GetMapping endpoint which returns a list of all resources available
on the server. The resource will be represented as JSON or XML identified by
produces = { MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE }.
The @RestController annotation indicate to Spring method return value should be
bound to the web response body.
Start the server by running the following command:
With the Tomcat server up and running, open another shell window and execute the following cURL command:
We can request for the same resource as XML representation:
Let us write the REST operation for getting a specific resource /api/v1.0/resources/{id}:
The @GetMapping annotation takes a variable (denoted by { and }) passed by the client, which is converted by
Spring to a Long using automatic type conversion.
The :[0-9]+ is a regular expression which constraint the client to use only positive whole numbers otherwise
the server returns 404 to the client.
If the client passes the path parameter in the format the server accepts, the id of the resource will be searched
from within the static resources field. If it exist return the response to the client or else return a 404.
Test this resource by running:
Now let us write our POST method that creates a new resource:
What is happening in the above snippet is a PostMapping request that returns a 201 (created) status code and a
Location header with the location of the newly created resource.
If the resource being created already exists on the server an error code of 409 (conflict) is returned by the
server.
For those not using Windows, you should omit the escape \.
The remaining two methods of our webservice is shown below:
To deploy this application to Heroku, we must ensure we have a Heroku account and Heroku CLI. Navigate to
the root directory of your application and execute the following command from your terminal:
This creates a heroku app called floating-gorge-84071. Heroku defines an environment variable $PORT which is
the HTTP port exposed over firewall for HTTP traffic.
Next create a Procfile. This is a plain text file called Procfile not procfile or
procfile.txt but just Procfile.
In this file we will create a web process type. The web process type is a special process type that listens for
HTTP traffic.
The application is ready to be deployed to Heroku with git add . && git push heroku master. But first let us
test the application locally to make sure everything works.
For those on Windows platform, create a file Procfile.windows for local testing.
The last command opens the heroku app in your default browser. If you get a 404, that is normal because you
haven’t mapped any resource to /. Just append the url with /api/v1.0/resources to start hacking.
You can view the logs for the application by running this command:
Conclusion
In this post we focused on the Client-Server constraint of REST. We learned how to implement REST with Spring.
We also learned how to deploy a Spring-Boot app to Heroku. In another post we will learn how to secure a RESTful
Web Service.
As usual you can find the full example to this guide in the github repository. Until the next post, keep doing cool things .