<div dir="ltr" style="text-align: justify;">
<div class="separator" style="clear: both; text-align: center;"><img src="https://dineshonjava.com/wp-content/uploads/2017/02/spring-requestparam-pathvariable.png" border="0" /></div>
<p>Both annotations @RequestParam and @PathVariable in <a href="https://dineshonjava.com/spring-web-mvc-framework-chapter-38/">Spring MVC</a> 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.</p>
<div id="ads-id" align="center"></div>
<div style="background-color: #f2f9fc; border: 1px solid #c9e6f2; border-radius: 3px; padding: 16px; line-height: 1.45;"><span style="color: red; font-size: x-large; text-align: center;"><b>Popular Tutorials</b></span></p>
<ul style="text-align: left;">
<li><b><a href="https://dineshonjava.com/spring-tutorial/"><em><strong>Spring Tutorial</strong> </em></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-web-mvc-framework-chapter-38/"><strong><em>Spring MVC Web Tutorial </em></strong></a></b></li>
<li><b><a href="https://dineshonjava.com/introduction-to-spring-boot-a-spring-boot-complete-guide/"><strong>Spring Boot Tutorial</strong> </a></b></li>
<li><b><a href="https://dineshonjava.com/spring-security-take-baby-step-to-secure/"><em>Spring Security Tutorial</em></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-aop-tutorial-with-example-aspect-advice-pointcut-joinpoint/"><em>Spring AOP Tutorial</em></a></b></li>
<li><b><a href="https://dineshonjava.com/using-spring-jdbc-framework-chapter-32/"><em>Spring JDBC Tutorial</em></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-hateoas-hypermedia-driven-restful-web-service/"><em><strong>Spring HATEOAS </strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/microservices-with-spring-boot/"><em><strong>Microservices with Spring Boot</strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/jax-rs-web-service-tutorial/"><strong><em>REST Webservice</em> </strong></a></b></li>
<li><b><a href="https://dineshonjava.com/core-java-baby-step-to-be-best-java-ian/"><em><strong>Core Java </strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/hibernate-3-on-baby-steps/"><em><strong>Hibernate Tutorial</strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-batch-process-with-example/"><strong><em>Spring Batch</em> </strong></a></b></li>
</ul>
</div>
<div class="separator" style="clear: both; text-align: center;"><a href="https://certification-questions.com/buy-mock-exams.html?affiliateCode=4f5a35d8-0022-4c9c-b8d3-265ed0b825a2&;utm_source=Dineshh&;utm_medium=affiliate&;utm_campaign=affiliate" target="_blank" rel="noopener"><img src="https://dineshonjava.com/wp-content/uploads/2017/02/spring-certification-dumps.png" border="0" /></a></div>
<h2><b><a href="https://dineshonjava.com/requestparam-annotation-in-spring-mvc-with-example/">@RequestParam</a></b><br />
<b> </b></h2>
<p>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.</p>
<p><b>Look at the following request URL:</b></p>
<p><b><i>http://localhost:8080/tutorials/bookmark/?site=dineshonjava&;id=200</i></b></p>
<p>In the above URL request, the values for site and id can be accessed as below:</p>
<pre class="highlight" style="text-align: left;">@RequestMapping(value = "/tutorials/bookmark") 
public String bookmark( 
 @RequestParam(value="site", required=true) String site, 
 @RequestParam(value="id", required=false) String id){ 
... 
} 
</pre>
<div style="text-align: left;"></div>
<ul>
<li><b>defaultValue</b>&#8211; 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.</li>
<li><b>name</b>&#8211; It is String type attribute and name of the request parameter to bind to.</li>
<li><b>required</b>&#8211; It is Boolean type attribute whether the parameter is required.</li>
<li><b>value</b>&#8211; It is String type attribute and it is alias for name attribute.</li>
</ul>
<p><a href="https://dineshonjava.com/requestparam-annotation-in-spring-mvc-with-example/"><b>Read More about it.</b></a></p>
<h2><b>@PathVariable</b></h2>
<p>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.</p>
<p><b>Look at the following request URL:</b><br />
<b><br />
</b> <b><i>http://localhost:8080/tutorials/bookmark/100?site=dineshonjava&;id=200</i></b></p>
<p>In the above URL request, the values for site and id can be accessed as below:</p>
<pre class="highlight">@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){ 
... 
} 
</pre>
<p><b>value</b>&#8211; 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.</p>
<p><b>Example of @PathVariable and @RequestParam</b><br />
In this example we demonstrate the key difference between @PathVariable and @RequestParam annotations.</p>
<p><b>TutorialController.java</b></p>
<pre class="highlight">@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"; 
 } 
} 
</pre>
<p><b>Application.java</b></p>
<pre class="highlight">@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); 
 } 
} 
} 
</pre>
<h2><b>Summary</b></h2>
<p>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.</p>
<div style="background-color: #f2f9fc; border: 1px solid #c9e6f2; border-radius: 3px; padding: 16px; line-height: 1.45;"><span style="color: red; font-size: x-large; text-align: center;"><b>Spring MVC Related Posts</b></span></p>
<ul>
<li><b><a href="https://dineshonjava.com/spring-web-mvc-framework-chapter-38/"><span style="color: red;">Spring MVC Web Tutorial</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-mvc-interview-questions-and-answers/"><span style="color: red;">Spring MVC Interview Questions</span></a></b></li>
<li><b><a href="https://dineshonjava.com/introduction-to-mvc/"><span style="color: red;">MVC Design Pattern</span></a></b></li>
<li><b><a href="https://dineshonjava.com/what-is-dispatcherservlet-in-spring-and-its-uses/"><span style="color: red;">Spring MVC DispatcherServlet </span></a></b></li>
<li><b><a href="https://dineshonjava.com/difference-between-applicationcontext-webapplicationcontext-in-spring-mvc/"><span style="color: red;">Spring MVC WebApplicationContext and Root Application Context</span></a></b></li>
<li><b><a href="https://dineshonjava.com/stereotype-annotations-in-spring/"><span style="color: red;">Spring MVC @Controller Annotation</span></a></b></li>
<li><b><a href="https://dineshonjava.com/requestmapping-annotation-in-spring-mvc/"><span style="color: red;">Spring MVC @RequestMapping Annotation</span></a></b></li>
<li><b><a href="https://dineshonjava.com/requestparam-annotation-in-spring-mvc-with-example/"><span style="color: red;">Spring MVC @RequestParam Annotation</span></a></b></li>
<li><b><a href="https://dineshonjava.com/difference-between-applicationcontext-webapplicationcontext-in-spring-mvc/"><span style="color: red;">Spring MVC ContextLoaderListener</span></a></b></li>
<li><b><a href="https://dineshonjava.com/requestparam-vs-pathvariable-annotations-in-spring-mvc/"><span style="color: red;">Spring MVC @RequestParam and @PathVariable annotations</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-30-mvc-hello-world-example/"><span style="color: red;">Spring MVC Hello World Example</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-exception-handling-example/"><span style="color: red;">Spring MVC Exception Handling Example</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-mvc-with-hibernate-crud-example/"><span style="color: red;">Spring MVC with Hibernate CRUD Example</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-3-mvc-tiles-plugin-with-example/"><span style="color: red;">Spring MVC Tiles Plugin with Example</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-3-mvc-and-interceptor-with/"><span style="color: red;">Spring MVC Interceptor with example</span></a></b></li>
<li><b><a href="https://dineshonjava.com/integration-spring-mvc3-and-mongodb/"><span style="color: red;">Spring MVC with MongoDB CRUD Example</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-3-mvc-internationalization/"><span style="color: red;">Spring MVC Internationalization &; Localization with Example</span></a></b></li>
</ul>
<p> ;</p>
</div>
</div>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 <a href="https://dineshonjava.com/requestparam-annotation-in-spring-mvc-with-example/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/spring-mvc-interview-questions-and-answers/">Next</a> 
									 </div> 
									</div>
<script type="text/javascript">
jQuery(document).ready(function($) {
 $.post('https://dineshonjava.com/wp-admin/admin-ajax.php', {action: 'mts_view_count', id: '77'});
});
</script>
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…