Implementing REST Web Service Using Spring MVC
Join the DZone community and get the full member experience.
Join For FreeUsing spring MVC it is very easy to enable a rest back-end for our web application. Rest is a representation based architecture style. Everything we access in rest is a representation and every representation has a unique resource identifier (URI). These are two important concepts we must remember in rest.
Spring MVC provide a very easy implementation on rest framework. Assuming you has a working spring MVC project. Please see below a spring rest controller.
There is couple of things that we need to keep in mind while writing this code.
- Always remember to put the annotation @ResponseBody on the method that will be accessed using rest URI. With Spring 4 we can avoid this annotation and add @RestController annotation to the controller class itself. It is a best practice to use this annotation if you are going to write a new spring rest controller.
- Always remember to switch on message converters. Spring by default provide many message converter, but we need to enable those by adding mvc: annotation driven to your dispatcher servlet. Message converters convert our domain objects into xml or json which will be used on the client side to display the data.
<!-- To switch on default message converters --> <mvc:annotation-driven/>
- Always remember to add the annotation @XmlRootElement to your object class which will be returned from the rest controller method.
@XmlRootElement
public class Order implements Serializable
{
@Id
private String orderId;
//Define all other fields
}
In the above code, we used couple of important annotations.
- @RequestMapping - This annotation maps web requests onto specific handler classes and/or handler methods.
- @PathVariable – Used to access the URI value and pass that value to corresponding method argument.
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> <servlet> <servlet-name>Dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>Dispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/application.xml</param-value> </context-param> </web-app>
Also attaching a copy of the dispatcher servlet.
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!-- We need to autowire the controllers --> <context:component-scan base-package="com.tutorial.rest.controllers"/> <!-- To switch on default message converters --> <mvc:annotation-driven/> </beans>
Once the controller is setup as explained here and application is deployed and started, we can issue http request on the browser and get xml or json representation of the order object.
Opinions expressed by DZone contributors are their own.
Trending
-
VPN Architecture for Internal Networks
-
Microservices Decoded: Unraveling the Benefits, Challenges, and Best Practices for APIs
-
Design Patterns for Microservices: Ambassador, Anti-Corruption Layer, and Backends for Frontends
-
Mastering Time Series Analysis: Techniques, Models, and Strategies
Comments