<div dir="ltr" style="text-align: justify;" trbidi="on">
<div class="separator" style="clear: both; text-align: center;"><img border="0" src="https://dineshonjava.com/wp-content/uploads/2017/02/requestparam-annotation-in-spring.jpg" /></div>
<p>
In spring the @RequestParam annotation is used to bind request parameter values to the handler method arguments in controller. Let&#8217;s see use of it with example in this article.</p>
<pre class="highlight">@Target(value=PARAMETER) 
@Retention(value=RUNTIME) 
@Documented 
public @interface RequestParam 
</pre>
<div align="center" id="ads-id"></div>
<ul style="text-align: left;">
<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>
<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>
<p>
<b>The &#8216;defaultValue&#8217; attribute of @RequestParam</b><br />
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. Providing a default value implicitly sets &#8216;required&#8217; to false.</p>
<pre class="highlight">@RequestParam(defaultValue="dineshonjava.com") String siteName 
</pre>
<p><b>The &#8216;required&#8217; attribute of @RequestParam</b><br />
It is Boolean type attribute whether the parameter is required. The default is true. If parameter is missing in the request then it will be returned status code 400. We can override this to false if the parameter is not present in the request.</p>
<p>Following will be mapped with both <b><i>/tutorials/bookmark?siteName=dineshonjava</i> </b>and <b><i>/tutorials/bookmark</i></b><br />
<b><br />
</b> </p>
<pre class="highlight">@Controller 
public class TutorialController { 
 
 @RequestMapping(value = "/tutorials/bookmark") 
 public String bookMarkTutorial ( 
 @RequestParam(required = false) String siteName, 
 Model model) { 
 model.addAttribute("I have bookmarked tutorial : " + siteName); 
 return "success"; 
 } 
} 
</pre>
<p>
<b>Using @RequestParam with value and without value attribute</b></p>
<p><b>Using &#8216;value&#8217; element of @RequestParam</b><br />
It is String type attribute and it is alias for name attribute. &#8216;value&#8217; attribute of @RequestParam is used to specify URL query param name. Following handler method will be mapped with the request <i><b>/tutorials/bookmark?site=dineshonjava</b> </i>:</p>
<pre class="highlight">Controller 
public class TutorialController { 
 
 @RequestMapping(value = "/tutorials/bookmark") 
 public String bookMarkTutorial ( 
 @RequestParam("site") String siteName,Model map) { 
 model.addAttribute("I have bookmarked tutorial : " + siteName); 
 return "success"; 
 } 
} 
</pre>
<p>
<b>@RequestParam without &#8216;value&#8217; element</b><br />
We could skip the &#8216;value&#8217; attribute of @RequestParam annotation if variable name of handler method is same as request parameter name.<br />
Following handler will be mapped with <b><i>/tutorials/bookmark?siteName=dineshonjava</i></b>:</p>
<pre class="highlight">@Controller 
public class TutorialController { 
 
 @RequestMapping(value = "/tutorials/bookmark") 
 public String bookMarkTutorial ( 
 @RequestParam String siteName, 
 Model model) { 
 model.addAttribute("I have bookmarked tutorial : " + siteName); 
 return "success"; 
 } 
} 
</pre>
<p>
<b>Using multiple @RequestParam annotations</b><br />
A handler method can have any number of @RequestParam annotations, it depended on the request parameters we are passing to the handler method. Following handler will be mapped with<br />
<b><i>/tutorials/bookmark?siteName=dineshonjava&;author=dinesh</i></b>:</p>
<pre class="highlight">@Controller 
public class TutorialController { 
 
 @RequestMapping(value = "/tutorials/bookmark") 
 public String bookMarkTutorial ( 
 @RequestParam String siteName, 
 @RequestParam String author, 
 Model model) { 
 model.addAttribute("I have bookmarked tutorial : " + siteName+" Author : "+author); 
 return "success"; 
 } 
} 
</pre>
<p>
<b>Using Map with @RequestParam for multiple params</b><br />
Spring provide support for bind the request parameters to the Map and MultiValueMap. All query string names and values are populated to the Map. ;Following will be mapped with <i><b>/tutorials/bookmark?siteName=dineshonjava&;author=dinesh: </b></i></p>
<p><i><b><br />
</b></i> </p>
<pre class="highlight">@Controller 
public class TutorialController { 
 
 @RequestMapping(value = "/tutorials/bookmark") 
 public String bookMarkTutorial ( 
 @RequestParam Map queryMap 
 Model model) { 
 model.addAttribute("I have bookmarked tutorial : " + queryMap.get("siteName")+ 
 " Author : "+queryMap.get("author")); 
 return "success"; 
 } 
} 
</pre>
<p>
<b> Summary</b><br />
In Spring @RequestParam annotation we can use for bind the request parameters to the handler method arguments. It provides auto-type conversion by default for parameters like int, float, String, Date etc. We can also override the default behavior of this annotation like required attribute set false to non mandatory request parameters.</p>
<p></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>
<ol style="text-align: left;"><span style="color: #274e13;"></p>
<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 &#038; Localization with Example</span></a></b></li>
<p></span></ol>
</div>
<p></p>
<div>
</div>
<div>
</div>
</div>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 <a href="https://dineshonjava.com/difference-between-applicationcontext-webapplicationcontext-in-spring-mvc/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/requestparam-vs-pathvariable-annotations-in-spring-mvc/">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: '78'});
});
</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…