Spring MVC RESTFul GET POST PUT DELETE

Spring MVC RESTFul GET POST PUT DELETE

To build a Java enterprise application spring is well known framework. Today we will build RESTFul web service using Spring MVC framework. As we you know RESTFul web service produces multiple representation based on the request from client. In this demo you will learn how to process all representation of RESTFul web service i.e. GET, POST, PUT and DELETE and also how to use spring annotation and mapping.

  • Spring MVC maven project structure:

Spring MVC RESTFul GET POST PUT DELETE

  • pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.javahonk</groupId>
	<artifactId>SpringMVCRESTFulExample</artifactId>
	<packaging>war</packaging>
	<version>0.0.1-SNAPSHOT</version>
	<name>SpringMVCRESTFulExample Maven Webapp</name>
	<url>http://maven.apache.org</url>

	<properties>
		<SpringVersion>4.0.6.RELEASE</SpringVersion>
		<jackson.version>1.9.10</jackson.version>
	</properties>
	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>3.8.1</version>
			<scope>test</scope>
		</dependency>
		<!-- Spring dependencies -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
			<version>${SpringVersion}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-web</artifactId>
			<version>${SpringVersion}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>${SpringVersion}</version>
		</dependency>

		<!-- Jackson JSON Mapper -->
		<dependency>
			<groupId>org.codehaus.jackson</groupId>
			<artifactId>jackson-mapper-asl</artifactId>
			<version>${jackson.version}</version>
		</dependency>
	</dependencies>
	<build>
		<finalName>SpringMVCRESTFulExample</finalName>
	</build>
</project>
  • 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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
  <display-name>Archetype Created Web Application</display-name>
  <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>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
  </context-param>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>
  • 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="com.javahonk.controller" />
	<mvc:annotation-driven />

	<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>
  • SpringMVCRESTFulController.java:
package com.javahonk.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class SpringMVCRESTFulController {
	
	@RequestMapping(value = "/populateDataFromServer/{name}",  method=RequestMethod.GET, produces={"application/json"})
	public @ResponseBody List<Integer> populateActivePSwapBasketGET(@PathVariable String name) {		
		
		if (name.equalsIgnoreCase("JavaHonk")) {
			return returnDataList();
		}else {
			List<Integer> list = new ArrayList<Integer>();
			list.add(12345);
			return list;
		}
		
	}

	@RequestMapping(value = "/populateDataFromServer", method=RequestMethod.POST, produces={"application/json"})
	public @ResponseBody List<Integer> populateActivePSwapBasketPOST(@RequestParam String name) {		
		
		if (name.equalsIgnoreCase("JavaHonk")) {
			return returnDataList();
		}else {
			List<Integer> list = new ArrayList<Integer>();
			list.add(12345);
			return list;
		}
	}
	
	@RequestMapping(value = "/populateDataFromServer/{name}", method=RequestMethod.PUT, produces={"application/json"})
	public @ResponseBody List<Integer> populateActivePSwapBasketPUT(@PathVariable String name) {		
		
		if (name.equalsIgnoreCase("JavaHonk")) {
			return returnDataList();
		}else {
			List<Integer> list = new ArrayList<Integer>();
			list.add(12345);
			return list;
		}
	}
	
	@RequestMapping(value = "/populateDataFromServer/{name}", method=RequestMethod.DELETE, produces={"application/json"})
	public @ResponseBody List<Integer> populateActivePSwapBasketDelete(@PathVariable String name) {		
		
		if (name.equalsIgnoreCase("JavaHonk")) {
			return returnDataList();
		}else {
			List<Integer> list = new ArrayList<Integer>();
			list.add(12345);
			return list;
		}
	}
	
	private List<Integer> returnDataList() {
		Random rand = new Random();
	    Integer randomNum = rand.nextInt();		
		List<Integer> list = new ArrayList<Integer>();
		list.add(randomNum);		
		return list;
	}

}
  • Download complete project from below link. You can deploy and test on any server but for demo purpose I have used tomcat 7.
  • To test this RESTFul web service install Postman – REST Client chrome extension. Once its installed open in chrome use below URL to send request to the server:

GET Request:

Spring MVC RESTFul GET POST PUT DELETE

POST Request:

Spring MVC RESTFul GET POST PUT DELETE

PUT Request:

Spring MVC RESTFul GET POST PUT DELETE

DELETE Request:

Spring MVC RESTFul GET POST PUT DELETE

Note: You can also refer different type of request mapping, processing using Spring MVC tutorial which had been written before below:

For more details on Spring MVC RESTFul web service please visit spring official tutorial here

download  Download project:  SpringMVCRESTFulExample

5 thoughts on “Spring MVC RESTFul GET POST PUT DELETE”
  1. Hello Sir,
    Iam very much thank you for your tutorials. it is very much helping me learning.
    When I try this tutorial, GET,PUT,DELETE working, but the POST method did not working, the below error coming, please help sir
    org.springframework.web.servlet.PageNotFound handleHttpRequestMethodNotSupported
    WARNING: Request method ‘POST’ not supported

    expecting your help

Leave a Reply

Your email address will not be published. Required fields are marked *