DZone
Integration Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Integration Zone > Implementing REST Web Service Using Spring MVC

Implementing REST Web Service Using Spring MVC

Roshan Thomas user avatar by
Roshan Thomas
·
Feb. 12, 15 · Integration Zone · Interview
Like (0)
Save
Tweet
7.77K Views

Join the DZone community and get the full member experience.

Join 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 -->
	<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.

Spring Framework Web Service REST Web Protocols

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • 3 Pieces of Bad Advice on How to Keep Your IT Job
  • Creating Event-Based Architecture on Top of Existing API Ecosystem
  • What Is HttpSession in Servlets?
  • Adaptive Change Management: A DevOps Approach to Change Management

Comments

Integration Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo