Spring MVC @RequestMapping example

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

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.

Simple scenario

Simple scenario
@RequestMapping(value="/departments")
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.

Request parameter binding

Request parameter binding
@RequestMapping(value="/departments")
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

Extracting path variables

Extracting path variables

@RequestMapping(value="/departments/{departmentId}")
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.

Extracting path variables alternative

Extracting path variables alternative

@RequestMapping(value="/departments/{departmentId}")
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.

Extracting multiple path variables

Extracting multiple path variables
@RequestMapping(value="/departments/{departmentId}/employees/{employeeId}")
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.

Extracting regular expressions

Extracting regular expressions
@RequestMapping(value="/{textualPart:[a-z-]+}.{numericPart:[\\d]+}")
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

Comments

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: