Spring 4 REST Example

This tutorial provides a simple example for writing the Spring MVC REST services using the Spring 4 APIs. We have written several articles about the spring 4 features in our previous articles. The REST implementation on Spriing 4 also very much similiar to the previous releases. Here I have provided the sample source code what I have used to implement the REST services. If you have any questions, please write it in the comments section.

Also Read:

SpringMVC4HelloWorld.java

package javabeat.net.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("/springmvc4")
public class SpringMVC4HelloWorld {

	@RequestMapping(value = "/hello", method = RequestMethod.GET)
	public String hello(ModelMap model) {
		model.addAttribute("msg", "JavaBeat Hello World!");
		return "helloWorld";
	}

	@RequestMapping(value = "/greetings/{msg}", method = RequestMethod.GET)
	public String greetingsMsg(@PathVariable String msg, ModelMap model) {
		model.addAttribute("msg", msg);
		return "helloWorld";
	}

}

helloWorld.jsp

<html>
<body>
	<h1>Spring MVC 4.0 REST Example</h1>

	<h3>Your Message : ${msg}</h3>
</body>
</html>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>Spring MVC 4.0 Web Application</display-name>
	<servlet>
		<servlet-name>spring4-mvc-dispatcher</servlet-name>
		<servlet-class>
			org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
		<load-on-startup>1</load-on-startup>
	</servlet>

	<servlet-mapping>
		<servlet-name>spring4-mvc-dispatcher</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>
</web-app>

spring4-mvc-dispatcher-servlet.xml

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation=" http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
	http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<context:component-scan base-package="javabeat.net.controller" />
	<bean
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix">
			<value>/WEB-INF/jsp/</value>
		</property>
		<property name="suffix">
			<value>.jsp</value>
		</property>
	</bean>

</beans>

Comments

comments

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

  • Seetesh Hindlekar

    You have not mentioned how to run the example by typing what in the URL if deployed on Jboss 7.x

  • http://www.javabeat.net Krishna Srinivasan

    You can access the application using this URL:
    /springmvc4/hello
    /springmvc4/greetings/message

    • Seetesh Hindlekar

      I had replaced the code with @RequestMapping(“/springrest”) but still the application did not run and gives 404

      • http://www.javabeat.net Krishna Srinivasan

        Did you check all other configurations?

  • Seetesh

    org.springframework.web.servlet.PageNotFound] (http–127.0.0.1-8081-1) No mapping found for HTTP request with URI [/springmvc4/helloseetesh/] in DispatcherServlet with name ‘dispatcher’

    this deployment is on jboss 7.1.1

    Normal mvc code works fine

  • Seetesh

    tried renaming to dispatcher-servlet.xml too

    dispatcher
    org.springframework.web.servlet.DispatcherServlet

    1

    dispatcher
    /

    contextConfigLocation
    /WEB-INF/dispatcher-servlet.xml

    org.springframework.web.context.ContextLoaderListener

  • Seetesh

    servlet-name dispatcher
    servlet-class org.springframework.web.servlet.DispatcherServlet
    url-pattern /
    contextConfigLocation /WEB-INF/dispatcher-servlet.xml
    listener-class org.springframework.web.context.ContextLoaderListener

    Am I missing something?

  • Seetesh Hindlekar

    I had to comment out the @RequestMapping(“/springrestless”) to get rid of the “No Mapping found” error.

    Still some issues in getting the JSP being displayed. The JSP resource is lookedup in the context folder too with the message displayed on the browser
    “The requested resource (/springrestless/WEB-INF/views/hello.jsp)is not available.”

    I have used build.xml to create the war file to be deployed on Jboss 7.1.1. But I guess the old fashion of creating a war file by Exporting it in Eclipse works just fine.