Spring Web MVC – Reading URI Path Variables

In this tutorial, you will learn how to read URI path variables in your Spring MVC Web application.

Define Path Variable

To define the path variable in the URI path, we use curly brackets. The @GetMapping annotation below specifies one path variable called userId. Notice that userId is surrounded by curly brackets.

@GetMapping(path="/users/{userId}")

@PathVariable Annotation

To read a URI path variable we use the @PathVariable annotation. The below code example uses the @PathVariable annotation to read the URI path parameter called userId and bind it to a method argument.

Notice that the name in the @PathVariable annotation matches the name of the path parameter userId in the @GetMapping annotation.

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class UsersController {

    @GetMapping(path="/users/{userId}")
    public ModelAndView getAlbum(@PathVariable("userId") String userId) {

        ModelAndView modelAndView = new ModelAndView("album");
        modelAndView.addObject("userId", userId);

        return modelAndView;
    }
}

Reading Multiple Path Variables

To read multiple path variables, we first define all path variables in the request mapping annotation. For example, the below request mapping annotation @GetMapping defines two path variables:

  • {userId}
  • {albumId}
@GetMapping(path="/users/{userId}/albums/{albumId}")

To read the userId and the albumId path variables, and bind their values to a method argument, we use the same @PathVariable annotation.

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class UsersController {

    @GetMapping(path="/users/{userId}/albums/{albumId}")
    public ModelAndView getAlbum(@PathVariable("userId") String userId,
                                @PathVariable("albumId") String albumId) {
        ModelAndView modelAndView = new ModelAndView("album");
        modelAndView.addObject("userId", userId);
        modelAndView.addObject("albumId", albumId);
        return modelAndView;
    }
}

Reading Path Variables into Map

You can also read all URI path variables and make them available to your method as key-values pairs in a Map. If the method argument is Map<String, String> then the map is populated with all path variable names and values.

Notice how method signature changes in the below code examples. Instead of reading each path variable separately, we use Map as a method argument.

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.servlet.ModelAndView;

import java.util.Map;

@Controller
public class UsersController {

    @GetMapping(path="/users/{userId}/albums/{albumId}")
    public ModelAndView getAlbum(@PathVariable Map<String, String> pathVariables) {

        ModelAndView modelAndView = new ModelAndView("album");
        modelAndView.addObject("userId", pathVariables.get("userId"));
        modelAndView.addObject("albumId", pathVariables.get("albumId"));

        return modelAndView;
    }
}

I hope this tutorial was helpful for you. To learn more about building Web applications with Spring Framework, please visit the Spring Web MVC category.  You will find many useful tutorials there.

Happy learning!


Leave a Reply

Your email address will not be published.