Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Implementing REST Web Service Using Spring MVC

DZone's Guide to

Implementing REST Web Service Using Spring MVC

· Integration Zone
Free Resource

Build APIs from SQL and NoSQL or Salesforce data sources in seconds. Read the Creating REST APIs white paper, brought to you in partnership with CA Technologies.

Using 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. 
Please see below the web.xml file used
<?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.

The Integration Zone is brought to you in partnership with CA Technologies.  Use CA Live API Creator to quickly create complete application backends, with secure APIs and robust application logic, in an easy to use interface.

Topics:

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}