Spring ReST Webservice with MySQL
The following tutorial is for creating simple Spring ReST webservice with MySQL. We are going to use Spring framwork to create this simple rest webservice that get data from the database.
- What is the Goal?
- Application Use-Case
- Create maven project
- Convert the maven project to Eclipse WTP project.
- Adding Dependencies.
- Create the Device DTO(Data Transaction Object).
- Create the DAO (Data Access Object) interface
- Impletement the DAO.
- Create the JDBC Object Mapper
- Define the spring bean
- Define the DispatcherServlet
- Define the Spring rest-servlet
- Finally Create the service Controller
What is the Goal?
- Understand the Spring framwork.
- Understand the Spring web-framwork.
- Understand the Spring JDBC-framwork.
After this tutorial, the working principle of Spring MVC web framwork with JDBC web-framwork couple togerther to produce a ReST web service will be understanable..
Application Use-Case
Expose a ReST Web-Service that can get Device details with MAC address.
Get Device By MAC address
http://localhost:port/DeviceDetail/REST/getDeviceByMAC/{macaddress}
Expose a ReST Web-Service that can get Device with mac address.
Get Device By Manufacturer
http://localhost:port/DeviceDetail/REST/getDeviceByManufacturer/{manufacturer}
Expose a Expose a ReST Web-Service that can get List of Device with Manufacturer.
Create maven project
Directory structure that is created
Convert the maven project to Eclipse WTP project.
To convert to Eclipse IDE compatiable web project, issue the following cmd.
Import the project in eclipse.
- Open eclipse
- File > import > Existing Maven Project > Next
- Specify the Root Directory via browse and selecting the “DeviceDetail” directory.
Adding Dependencies.
Open the pom.xml in text editor.Add the following properties.
Add the following dependencies.
Add the maven compiler plugin
Now that the development setup is completed, head to the next part that discuss about the development. In this part, we will be discussing about how we could formulate the Spring ReST webservice with database using Spring framework.
Create the Device DTO(Data Transaction Object).
Data transfer object is an object that carries data between processes. The difference between data transfer objects and business objects or Data Access Objects is that a DTO does not have any behavior except for storage and retrieval of its own data (accessors and mutators). DTOs are simple objects that should not contain any business logic.
With that in mind, lets create a DTO for the Device as stated below code.
Create the DAO (Data Access Object) interface
Data Access Object (DAO) is an object that provides an abstract interface to some type of database or other persistence mechanism. By mapping application calls to the persistence layer, DAO provide some specific data operations without exposing details of the database. It separates what data accesses the application needs, in terms of domain-specific objects and data types.
So lets create an interface that would suffix for out application.
Impletement the DAO.
Now lets implement our DAO. Here in this class, we are defining the behavior how the data are set and get as an Object. We will be using the dataSource
that is defined in the spring bean. This is describe in the next section.
Create the JDBC Object Mapper
The JDBC Object mapper class define how the data retrieved from the database is going to mapped to the Object defined in the DTO.
Define the spring bean
There are two beans defined for the DeviceDetail application. In general the bean id
defined the unique identity of the bean. The class
attribute defined Dependency Injection(DI) and the property
attribute defined the properties that would be used in the DI.
Here in this case, the bean dataSource
defined the database connection parameters. The deviceDAO
is the bean that is used to DI in the class defined that use the dataSource bean defined above.
Define the DispatcherServlet
DispatcherServlet is the central dispatcher for HTTP request handlers/controllers. DispatcherServlet looks for Handler Mappings. Spring MVC supports three different ways of mapping request URI’s to controllers : annotation, name conventions and explicit mappings. Handler Mappings section defined in the application context file, tells DispatcherServlet which strategy to use to find controllers based on the incoming request.
More at Spring Dispatcher.
This is defined in the web.xml
Define the Spring rest-servlet
Now lets create the ‘rest-servlet.xml’ that defined how the framework would work. With ComponentScan
tag, Spring auto scans all elements in the provided base package and all its child packages for Controller servlet. And the <mvc:annotation-driven>
tag instead of ViewResolver, with which we can directly send response data from the controller.
Finally Create the service Controller
Now lets create the Service Controller where the Dispatcher Sevlet scan and let it control the Request/Response dispatcher. But before that lets go through some important annotation that is used.
@RequestMapping
annotation is used for defining incoming request urls for class and method levels. In this case two uri’s in the format <root-url>/REST/getdevicebymac/{mac}/
and <root-url>/REST/getdevicebymanufacturer/{manufacturer}
will be routed to this Controller respective method annotated . With @RequestMapping
annotation, we can only define generic uri mappings. For dynamic uri mappings in case of passing variables along with the uri, @PathVariable
is used. Here in this case, we pass a variable mac
along with the uri such as, <root-url>/REST/getdevicebymac/123456/
. Here the last parameter (123456) in the uri is retrieved using @PathVariable
.
Using <mvc:annotation-config>
tag instead of view resolver, we use @ResponseBody
annotation to return response data directly from controller. But in the above code, we have not used @ResponseBody
. This is because, in Spring MVC 4.0, @RestController is introduced such that we need not use @ResponseBody
tag in each and every method. @RestController
will handle all of that at the type level. This annotation simplifies the controller and it has @Controller
and @ResponseBody
annotated within itself.
More at Spring @RestController Spring @RequestMapping Spring @PathVariable
Build it with mvn clean install
and deploy the war to the tomcat container.