Spring MVC @RequestMapping example

16 February 2013
In this tutorial you will learn how to use @RequestMapping annotation to match URL patterns in different scenarios.

1. Introduction

Spring MVC @RequestMapping annotation used in Controllers allow us not only to implement URL pattern matching but also to extract variables from the request URL itself. We will go through different possible scenarios in the next section to provide a better understanding.

2. Simple scenario

Simple scenario
public String simplePattern(){

  System.out.println("simplePattern method was called");
  return "someResult";


Testing URL: /departments

In this simple scenario we are defining that simplePattern() method should be called when the URL matches /departments.

3. Request parameter binding

Request parameter binding
public String findDepatment(
  @RequestParam("departmentId") String departmentId){
    System.out.println("Find department with ID: " + departmentId);
    return "someResult";


Testing URL: /departments?departmentId=23

In this scenario we are fetching the parameter departmentId from the request URL - departmentId=23 - and mapping it into the String departmentId method parameter. The method will print: Find department with ID: 23

4. Extracting path variables

Extracting path variables

public String findDepatment(@PathVariable String departmentId){

  System.out.println("Find department with ID: " + departmentId);
  return "someResult";


Testing URL: /departments/23

In this scenario we are matching the method with the pattern /departments/{departmentId}. This means that the section between brackets - {departmentId} - is a variable. Spring automatically matches a path variable to a parameter that has the exact same name, so this method will print: Find department with ID: 23.

5. Extracting path variables alternative

Extracting path variables alternative

public String findDepatmentAlternative(
  @PathVariable("departmentId") String someDepartmentId){

    System.out.println("Find department with ID: " + someDepartmentId);
    return "someResult";


Testing URL: /departments/23

This scenario is very similar to the previous one, but in this case we are specifying that departmentId path variable should be bound to someDepartmentId parameter. This means that we can override the default binding mechanism explained in the previous scenario and define our own mechanism.

6. Extracting multiple path variables

Extracting multiple path variables
public String findEmployee(
  @PathVariable String departmentId,
  @PathVariable String employeeId){

    System.out.println("Find employee with ID: " + employeeId + 
      " from department: " + departmentId);
    return "someResult";


Testing URL: /departments/23/employees/51

This scenario now specifies multiple path variables in the same URL, each one being bound to a distinct method parameter. This method prints: Find employee with ID: 51 from department: 23.

7. Extracting regular expressions

Extracting regular expressions
public String regularExpression(
  @PathVariable String textualPart,
  @PathVariable String numericPart){

    System.out.println("Textual part: " + textualPart + 
      ", numeric part: " + numericPart);
    return "someResult";

Testing URL: /sometext.123

We may also specify regular expression based patterns. The syntax used in the pattern is {pathVar:regularExpression}. As usual the path variables are then bound to the parameters respectively. Given the example url - /sometext.123 - this method prints: Textual part: sometext, numeric part: 123.

Related Articles


Share this website
Subscribe and receive the latest tutorials by email
Popular tags
About the author
Gonçalo Marques is a Software Engineer with 8+ years of experience in software development and architecture design. During this period his main focus was delivering software solutions both in banking and telecommunications area. He created the Bytes Lounge website with one ultimate goal: share his knowledge with the software development community. His main area of expertise is Java and open source.

He is also the author of the WiFi File Browser Android application:

Google Play logo