Spring MVC

Differences between @RequestParam and @PathVariable in Spring MVC

Both annotations @RequestParam and @PathVariable in Spring MVC are used for fetching the values of request parameters. These annotations have similar purpose but some differences in use. The key difference between @RequestParam and @PathVariable is that @RequestParam used for accessing the values of the query parameters where as @PathVariable used for accessing the values from the URI template.

@RequestParam
 

It is used to get the request parameters. @RequestParam automatically binds the request parameters to the arguments of your handler method. It also provides auto type conversion for some standard type like int, long, float, string, date etc.

Look at the following request URL:

http://localhost:8080/tutorials/bookmark/?site=dineshonjava&id=200

In the above URL request, the values for site and id can be accessed as below:

@RequestMapping(value = "/tutorials/bookmark")
public String bookmark(
    @RequestParam(value="site", required=true) String site,
    @RequestParam(value="id", required=false) String id){
...
}
  • defaultValue– It is String type attribute and the default value to use as a fallback when the request parameter is not provided or has an empty value.
  • name– It is String type attribute and name of the request parameter to bind to.
  • required– It is Boolean type attribute whether the parameter is required.
  • value– It is String type attribute and it is alias for name attribute.

Read More about it.

@PathVariable

It is used to pass parameter along with the url, sometimes we need to pass parameters along with the url to get the data. Spring MVC provides support for customizing the URL in order to get data. To achieving this purpose @PathVariable annotation is used in Spring mvc framework.

Look at the following request URL:

http://localhost:8080/tutorials/bookmark/100?site=dineshonjava&id=200

In the above URL request, the values for site and id can be accessed as below:

@RequestMapping(value = "/tutorials/bookmark/{siteId}")
public String bookmark(
    @PathVariable(value="siteId") String siteId
    @RequestParam(value="site", required=true) String site,
    @RequestParam(value="id", required=false) String id){
...
}

value– It is String type attribute amd it is only one attribute of the @PathVariable annotation. It is allowed to use the multiple @PathVariable annotation in the single method. But, ensure that no more than one method has the same pattern.

Example of @PathVariable and @RequestParam
In this example we demonstrate the key difference between @PathVariable and @RequestParam annotations.

TutorialController.java

@Controller
package com.doj.webapp.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * @author Dinesh.Rajput
 *
 */
@Controller
public class TutorialController {

    @RequestMapping(value = "/tutorials/bookmark/{siteId}")
    public String bookMarkTutorial (
       @PathVariable(value="siteId") String siteId
       @RequestParam(value="site", required=true) String site,
       @RequestParam(value="id", required=false) String id
              Model model) {
        model.addAttribute("I have bookmarked tutorial : " + site);
        return "success";
    }
}

Application.java

@Controller
package com.doj.webapp.controller;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
/**
 * @author Dinesh.Rajput
 *
 */
@SpringBootApplication
public class Application extends WebMvcConfigurerAdapter{
 public static void main(String[] args) {
  SpringApplication.run(Application.class, args);
 } 
}
}

Summary

This tutorial we have explained you to understand the difference between @PathVariable and @RequestParam in the Spring MVC. Basically purpose of both annotation in the same but way of fetching the request parameter value is different. @RequestParam annotation fetch the value of request parameter in the form of passing request parameter with url but @PathVariable annotation fetching value of the parameter in the form request URI template with some placeholder.

Previous
Next
Dinesh Rajput

Dinesh Rajput is the chief editor of a website Dineshonjava, a technical blog dedicated to the Spring and Java technologies. It has a series of articles related to Java technologies. Dinesh has been a Spring enthusiast since 2008 and is a Pivotal Certified Spring Professional, an author of a book Spring 5 Design Pattern, and a blogger. He has more than 10 years of experience with different aspects of Spring and Java design and development. His core expertise lies in the latest version of Spring Framework, Spring Boot, Spring Security, creating REST APIs, Microservice Architecture, Reactive Pattern, Spring AOP, Design Patterns, Struts, Hibernate, Web Services, Spring Batch, Cassandra, MongoDB, and Web Application Design and Architecture. He is currently working as a technology manager at a leading product and web development company. He worked as a developer and tech lead at the Bennett, Coleman & Co. Ltd and was the first developer in his previous company, Paytm. Dinesh is passionate about the latest Java technologies and loves to write technical blogs related to it. He is a very active member of the Java and Spring community on different forums. When it comes to the Spring Framework and Java, Dinesh tops the list!

Share
Published by
Dinesh Rajput

Recent Posts

Strategy Design Patterns using Lambda

Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…

2 years ago

Decorator Pattern using Lambda

Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…

2 years ago

Delegating pattern using lambda

Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…

2 years ago

Spring Vs Django- Know The Difference Between The Two

Technology has emerged a lot in the last decade, and now we have artificial intelligence;…

2 years ago

TOP 20 MongoDB INTERVIEW QUESTIONS 2022

Managing a database is becoming increasingly complex now due to the vast amount of data…

2 years ago

Scheduler @Scheduled Annotation Spring Boot

Overview In this article, we will explore Spring Scheduler how we could use it by…

2 years ago