Over a million developers have joined DZone.

Implementing REST Web Service Using Spring MVC

DZone's Guide to

Implementing REST Web Service Using Spring MVC

· Integration Zone ·
Free Resource

WSO2 is the only open source vendor to be named a leader in The Forrester Wave™: API Management Solutions, Q4 2018 Report. Download the report now or try out our product for free.

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 -->
  • Always remember to add the annotation @XmlRootElement to your object class which will be returned from  the rest controller method.
public class Order implements Serializable

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"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"



Also attaching a copy of the dispatcher servlet.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	                    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd

	<!--  We need to autowire the controllers -->
	<context:component-scan base-package="com.tutorial.rest.controllers"/>

	<!-- To switch on  default message converters -->


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.

Read the WSO2 Methodology for Agility to see how you can transform your integration projects from semi-agile to a scalable continuous agile approach.


Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}