REST Service Using Spring

In this tutorial, we will be learning how a RESTful service can be built with Spring. We would be creating a simple Restful service using spring and running it in eclipse.

Tools

We will be using the following tools for development in this tutorial.

We will be providing step by step guide on how to create a REST service using Spring. We are going to create a Rest Service where GET, PUT, POST and DELETE requests are handled.

Spring Tool Suite

Spring Tool Suite(STS) is a tool for spring development. It is developed on top of Eclipse. STS can be downloaded as a separate software from the official site or can be added to existing Eclipse IDE via Eclipse Marketplace. While downloading from official STS download site, make sure that you download the correct version according to the OS [32 bit or 64 bit].

With the introduction of Spring Tool Suite(STS), we no longer need to manually write the whole set of configurations that are required for Spring Application development.

Building The Project Structure With STS

  • Open STS. The default perspective of the STS IDE will be Spring perspective.
  • Right click on package explorer. From the drop down, choose New -> Spring Legacy Project. Select Spring MVC Project as shown in the diagram below
Rest with spring Legacy
Rest with spring Legacy

 

After creation, your final project may look like the following in the package explorer.

 

package explorer
package explorer

Adding Required Dependencies in pom.xml

STS will add default dependencies to the project’s pom.xml. We have done slight modifications to the generated pom.xml so that our application uses the latest and updated dependencies. The changed versions are given in the properties tag.

pom.xml
pom.xml

 

Note the name of the artifactID at the start of project configuration in your pom.xml. After successful creation of a new service in the Spring MVC Application, the application can be deployed onto a web container and is usually accessed by following URL format:

http://localhost:8080/<artifactID>/<your-controller-or-method-path-value>

After required changes are made to pom.xml, run the following in command prompt

On the first run, this should download all the dependencies to the M2_HOME(system property) folder.  All the required libraries would be downloaded and added to the classpath of the eclipse project.

Gradle dependencies

If your organization or CI tool uses Gradle for building projects, you can generate the Gradle build file from pom.xml. For this purpose, navigate to the root directory of your project structure and run:

gradle init

This should create a build.gradle file that contains the dependencies per the maven and java settings in your system. The Gradle file created for sample project is given below for reference.

After successful creation of the first REST application, you may invoke Gradle build by the following command:

gradle build

Understanding the Configuration XMLs

web.xml

This is the deployment descriptor and entry point to all configurations for a Spring MVC project. In any J2EE application, the web.xml primarily contains two important tags: a. <servlet> tag that contains the servlet’s name and other parameters. B. <servlet-mapping> tag that maps the servlet to a URL pattern. In the case of Spring MVC application, we use only one servlet in most cases, that is, org.springframework.web.servlet.DispatcherServlet.

This servlet will delegate the incoming request to corresponding Java controller implementation according to the headers of the request.

Servlet-context.xml and root-context.xml

There are two types of application contexts in Spring: root-context and servlet-context for each servlet. The dispatcherServlet shares the root-context. The root-context is also called the spring parent context. Spring container on startup reads beans from the root-context. The servlet-context also contains bean configurations. While trying to locate a bean, Spring framework searches the servlet context at first, and if not found, it will search for the bean in root-context.

Note: In this tutorial, we are not going to modify these three XML files as that is out of scope. We will be focussing more on REST related concepts here.

Create POJO for your service

Create the following class

Create a REST Controller

In Spring MVC, HTTP requests are handled by controllers that are annotated with @Controller annotation. If we do not specify the MIME type using @Produces, the POJO will be converted to default JSON format and returned to respective REST calls.

The Constants used in above program are defined below:

Overview Of The Annotations Used

Annotations Description
@Controller Denotes that the particular class serves as a controller that an cater to an incoming request
@RequestMapping Used to map an entire controller or a method of the controller. This contains parameters like URI, Media types produced, media types consumed and the HTTP method which shall invoke it.
@ResponseBody Used to denote that the return value of the method will be what the response for HTTP request will comprise of.
@PathVariable Denotes the path parameter. Eg: http://localhost:8080/rest/accounts/1. Here, 1, which corresponds to account ID will be taken as path variable.

Running And Testing The Application

In STS IDE, right-click on the server and choose Add or Remove. Add your project and then start it.

On a web browser, type the following URL:

http://localhost:8080/rest/accounts/default

You should be able to see the following JSON as output:

{"id":0,"fullName":"John Smith","balance":0.0}

Testing CRUD operations

Download SOAPUI from the official download site and install it in your system. Give the URL: http://localhost:8080/rest/accounts and start the REST test.

You should be able to perform GET, POST, PUT and DELETE requests prop.A sample screenshot is given below for create (POST) operation:

 

SOAP UI example
SOAP UI example

 

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.