Spring 3 REST hello world example

In Spring 3, old RequestMapping class is enhanced to support RESTful features, which makes Spring developers easier to develop REST services in Spring MVC.

In this tutorial, we show you how to use Spring 3 MVC annotations to develop a RESTful style web application.

1. Project Directory

Review the project folder structure.

2. Project Dependency

To develop REST in Spring MVC, just include the core Spring and Spring MVC dependencies.

pom.xml

	<properties>
		<spring.version>3.0.5.RELEASE</spring.version>
	</properties>

	<dependencies>

		<!-- Spring 3 dependencies -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
			<version>${spring.version}</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-web</artifactId>
			<version>${spring.version}</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>${spring.version}</version>
		</dependency>

	</dependencies>

</project>

3. REST Controller

For Spring RESTful, you need PathVariable, RequestMapping and RequestMethod. Following code should be self-explanatory.

MovieController.java

package com.mkyong.common.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("/movie")
public class MovieController {

	@RequestMapping(value = "/{name}", method = RequestMethod.GET)
	public String getMovie(@PathVariable String name, ModelMap model) {

		model.addAttribute("movie", name);
		return "list";

	}

	@RequestMapping(value = "/", method = RequestMethod.GET)
	public String getDefaultMovie(ModelMap model) {

		model.addAttribute("movie", "this is default movie");
		return "list";

	}

}

4. JSP Views

A JSP page to display the value.

list.jsp

<html>
<body>
	<h1>Spring 3 MVC REST web service</h1>
	
	<h2>Movie Name : ${movie}</h2>	
</body>
</html>

5. Demo

See REST URLs demonstration.

URL : http://localhost:8080/SpringMVC/movie/ironMan

Spring MVC REST demo

URL : http://localhost:8080/SpringMVC/movie/SpiderMan4

spring mvc rest demo

Download Source Code

References

  1. http://en.wikipedia.org/wiki/Representational_State_Transfer
  2. http://blog.springsource.com/2009/03/08/rest-in-spring-3-mvc/

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

avatar
54 Comment threads
13 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
55 Comment authors
Thirumala NagarajuArpit MandliyaDeepak ssPuja BhattNSLabs Recent comment authors
newest oldest most voted
Nirajan
Guest
Nirajan

I don’t think it is a REST. The response of a RESTful web service is either in JSON or XML. The response should not be in jsp like you presented. And specially, this is just an Spring MVC Hello World.

Puja Bhatt
Guest
Puja Bhatt

This tutorial is very helpful. I learn alot from this. Thanks to author

trackback
Spring 3 MVC hello world example

[…] is widely adapted in everywhere. The @RequestMapping is available since 2.5, but now enhanced to support REST style URLs in Spring MVC.package com.mkyong.common.controller;   import org.springframework.stereotype.Controller; […]

trackback
Spring MVC Tutorial

[…] works.Spring 3 MVC hello world example what you need to develop Spring 3 MVC web application.Spring 3 REST hello world example Develop REST style web application in Spring 3 MVC.Spring 2.5.6 MVC hello world example XML-based […]

josesaid
Guest
josesaid

I downloaded it, but isn’t working fine…
http://localhost:8080/SpringMVC/movie/SpiderMan4 sends a 404 page error.

Adi
Guest
Adi

Great post!
Thanks!

Chandra
Guest
Chandra

I need to pass a URL as parameter, since URL has forward slashes it is interpreting wrongly. Below is the error message.

WARN org.springframework.web.servlet.PageNotFound – No mapping found for HTTP request with URI [/sbasvc/misc/linkcheck/url/http://www.abc.com/index.html] in DispatcherServlet with name ‘miscellaneous’
Can someone suggest me how to resolve this issue.

Triguna
Guest
Triguna

Thanks for a wonderful post. This really helped me speed up my development.

Thanks,
Triguna
Tejasco.

trackback
Google App Engine + Spring 3 MVC REST example

[…] 3.1.1JDK 1.6Eclipse 3.7 + Google Plugin for Eclipse Note This example is going to reuse this Spring 3 MVC REST example, modify it and integrate with Google App Engine, you may also interest to read this – GAE + […]

Shyam
Guest
Shyam

Hi Mkyong,

Thanks for your examples.

These are pretty nice.

Can you upload spring + Hibernate+ RestFull implementation please.

Thanks
Shyam

Arthur
Guest
Arthur

You are the best!
Simple, clear, works =)

Rudy
Guest
Rudy

I encountered the same problem :
no mapping found for HTTP Request with URI [/SpringMVC/movie/ironman] in DispatcherServlet with name ‘mvc-dispatcher’

Thurman
Guest
Thurman

Great Stuff. Whenever I need something explained in simple terms I look for your examples. Thanks

p
Guest
p

It doesnt seem to work for me, can you explain why? I have copied your example exactly but adding anything to the end of the URL results in a 404 error instead of calling the controller method.

I have also noticed that if I dont include the movie part at all, so just http://localhost:8080/SpringMVC/movie then that works but… the name is always returned as just index. Adding anything like Superman etc just results in a 404 error.

Robert Morschel
Guest
Robert Morschel

IG Group have recently open-sourced their RESTdoclet solution, which is specifically aimed at services built using Spring’s REST framework. We use this internally to automatically generate documentation for all our RESTful services and publish these via an internal web portal.

It includes a working sample application demonstrating several Spring 3 REST features.

More info here: http://ig-group.github.com/RESTdoclet/

Robert @ IG Group

Caleb Madrigal
Guest
Caleb Madrigal

Thanks, this is the best minimal Spring MVC app I have seen.

vijay
Guest
vijay

Hi Mkyong,

Thanks for the example. It is very simple. Can you please provide code for standalone client for the same example.

Thanks,

Vijay

lakshmi
Guest
lakshmi

In problem it gives a warning as org.eclipse.jdt.USER_LIBRARY/Spring3.0.5 will not be exported or published. Runtime ClassNotFoundExceptions may result.

its shows 404 error to me…

Richard
Guest
Richard

Gr8 tutorial. Gave me the project structure i needed to base my project off.

Sudharma
Guest
Sudharma

Hi, If I understand it correctly, its spring mvc , I do not see any REST service call. could you please upload correct examples.

Michael Sweeney
Guest
Michael Sweeney

Tried running
$ mvn
Got [ERROR] No goals have been specified for this build.

jonathan
Guest
jonathan

Nice little guide but you forgot to show and describle one of the most important parts of setting up a Restful web application and that is the mvc dispatcher and web.xml files.

Without this, the application wont work and the people here using this example as a starting point will strugle to play around and develope this app further.

Please update your post and include details about the web.xml file and teh dispatcher!

this is vital as a user can then tweak and add more controllers, more mappings and change the mapping patterns completely

trackback
How to create a Web Application Project with Maven

[…] Spring 3 REST Hello World Example […]

Preetam
Guest
Preetam

how can i use cxf with spring mvc can u provide a full article on that ?

Nitin Gupta
Guest
Nitin Gupta

I am newbie to Java and Spring, and i love Mkyong for for his wonderful tutorials and examples. It helps me a lot.

Amar
Guest
Amar

Nice articles…Y dont you make it available on github only…

Anand
Guest
Anand

I have a doubt. The example looks like normal web application but still serves as RESTful service. Here I see there is no reference implementation for JAX-RS is used. So, can I assume that JAX-RS standards are not required or irrelevant with Spring 3.0?

jakki
Guest
jakki

java.lang.illegalstateexception no parameter name specified for argument of type java.lang.string i got this exception

When i changed the code from this

@RequestMapping(value = “/{name}”, method = RequestMethod.GET)
public String getMovie(@PathVariable String name, ModelMap model) {

to this its working now have to add (@PathVariable(“name”))

@RequestMapping(value = “/{name}”, method = RequestMethod.GET)
public String getMovie(@PathVariable(“name”) String name, ModelMap model) {

jakki
Guest
jakki

java.lang.illegalstateexception no parameter name specified for argument of type java.lang.string i got this exception

When i changed the code from this

@RequestMapping(value = “/{name}”, method = RequestMethod.GET)
public String getMovie(@PathVariable String name, ModelMap model) {

to this its working now have to add (@PathVariable(“name”))

sean
Guest
sean

Thanks for the great example!

If I may, I have one observation regarding getDefaultMovie’s @RequestMapping; use an empty string instead of a single forward slash.

 
@RequestMapping(value = "", method = RequestMethod.GET)
	public String getDefaultMovie(ModelMap model) {

The reason is while “localhost:8080/SpringMVC/movie/” displays the default movie message “localhost:8080/SpringMVC/movie” returns 404.

When an empty string is used, both return default movie message.

There are a lot of comments, sorry if this was previously stated.