• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)

@RequestMapping Annotation in Spring MVC

March 22, 2016 //  by Krishna Srinivasan//  Leave a Comment

@RequestMapping annotation is used for mapping web requests to particular handler classes or handler methods. The classes or methods that are annotated with @RequestMapping will be scanned and registered as the URI mappings for that specific class or method. Any future web requests will be redirected to that class or method. This annotation is part of Spring MVC module.

Also Read:

  • Spring Boot Tutorials
  • Spring Data JPA Tutorials

This annotation can be used in class level or method level. If you are using at the class level, that will be set as the common relative path for all the methods inside the class. If you are using at the only method level, each will be acting as its own relative path from the application’s context root. This tutorial has examples to makes you understand the difference when you are annotating at the class level and method level.

RequestMapping in Spring MVC
RequestMapping in Spring MVC

RequestMapping Annotation Parameters

The following are the parameters accepted by this annotation:

  • consumes: This argument narrows down the acceptable media format from the request.
  • headers: The headers of the mapped request, narrowing the primary mapping.
  • method: This argument specifying the HTTP methods can be redirected to that method. More than one can be specified by separating using commas. The HTTP methods that are used are GET, POST, HEAD, OPTIONS, PUT, PATCH, DELETE, TRACE
  • name: This is simply a name to the mapping. This can be specified at the class level and the method level. If you specify at both the levels, then a combined name is derived by concatenation with “#” as a separator.
  • params: The parameters of the mapped request, narrowing the primary mapping.
  • path: This is an alias for the value argument. the path mapping URIs (e.g. “/myPath.do”). Ant-style path patterns are also supported (e.g. “/myPath/*.do”). At the method level, relative paths (e.g. “edit.do”) are supported within the primary mapping expressed at the type level.
  • produces: This argument narrows down the returned media format to the response.
  • value: This is the primary mapping used for mapping the web request to that method.

Class Level Mapping

If you declare @RequestMapping at the class level, the path will be applicable to all the methods in the class. Let’s look at the following example code snippet:

@Controller
@RequestMapping("/millets")
public class SpringMVCController {
	@RequestMapping(value="/foxtail")
	public String getFoxtail(){
		return "foxtail";
	}
	@RequestMapping(value="/finger")
	public String getFinger(){
		return "finger";
	}	
	public String getCommon(){
		return "common";
	}	
}

In the above code, the relative path /millets is enforced to all the methods inside the class. The method getFoxtail() will be invoked when user enter the URI /millets/foxtail. However, the last method getCommon() will not be invoked by any request because this method is not annotated with the @RequestMapping annotation.

Method Level Mapping

When there is no class level mapping and multiple method-level mappings are defined, each method would form a unique relative path. Let’s look at the below example code:

@Controller
public class SpringMVCController {
	@RequestMapping(value="/foxtail")
	public String getFoxtail(){
		return "foxtail";
	}
	@RequestMapping(value="/finger")
	public String getFinger(){
		return "finger";
	}	
}

In the above code, both the method have their individual relative paths /foxtail and /finger respectively.

HTTP Methods Mapping

One of the other ways to do the Request Mapping is to use the HTTP methods. You can specify in the @RequestMapping annotation that for which HTTP method request the method has to be invoked. Let’s look at the following code:

@Controller
public class SpringMVCController {
	@RequestMapping(value="/foxtail",method=RequestMethod.GET)
	public String getFoxtail(){
		return "foxtail";
	}
	@RequestMapping(value="/foxtail",method=RequestMethod.POST)
	public String getList(){
		return "foxtailList";
	}	
	@RequestMapping(value="/",method=RequestMethod.GET)
	public String getMillets(){
		return "millets";
	}	
}

In the above code, if you look at the first two methods of mapping to the same URI, but both have the different HTTP methods. The first method will be invoked when HTTP method GET is used and the second method is invoked when HTTP method POST is used. This kind of design is very useful for writing the REST APIs for your project.

Mapping using Params

Another very useful mapping technique is to use the parameters in the query string to filter the handler mappings. For example, you can restrict the handler mapping by configuring like if the URL query string has this parameter then go to this method or redirect to some other method.

Let’s look at this example snippet:

@RequestMapping(value="/millets",method=RequestMethod.GET,params="foxtail")
public String getFoxtail(){
	return "foxtail";
}

In the above example code, the request goes to this method only when the following criteria are satisfied:

  1. If the URL is /millets and
  2. If the HTTP method is GET and
  3. If the query request has the parameter with the name foxtail

Summary

In this tutorial, I have explained how to use the @RequestMapping annotation in your spring MVC application. The various combinations like class level mapping, method level mapping, params matching, HTTP methods matching, headers mapping and multiple paths URI mappings.

If you have any questions on how to use @RequestMapping annotation and any related spring mMVCissues, please feel free to drop a comment in this tutorial for any help.

Thank you for reading my tutorial!! Happy Learning!!

Category: Spring FrameworkTag: Spring Annotations

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.

Previous Post: «Spring Boot Configurations Firing Events at Spring Boot Startup
Next Post: Spring Boot Configurations Spring Boot Configurations»

Reader Interactions

Leave a Reply Cancel reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

New Features in Spring Boot 1.4

Difference Between @RequestParam and @PathVariable in Spring MVC

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Introductiion to Jakarta Struts

What’s new in Struts 2.0? – Struts 2.0 Framework

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact