DZone
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
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations

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
  1. DZone
  2. Coding
  3. Frameworks
  4. Implementing REST Web Service Using Spring MVC

Implementing REST Web Service Using Spring MVC

Roshan Thomas user avatar by
Roshan Thomas
·
Feb. 12, 15 · Interview
Like (0)
Save
Tweet
Share
8.02K 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.

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

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • 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

Let's be friends: