Spring MVC

@RequestParam Annotation in Spring MVC with Example

<div dir&equals;"ltr" style&equals;"text-align&colon; justify&semi;" trbidi&equals;"on">&NewLine;<div class&equals;"separator" style&equals;"clear&colon; both&semi; text-align&colon; center&semi;"><img border&equals;"0" src&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-content&sol;uploads&sol;2017&sol;02&sol;requestparam-annotation-in-spring&period;jpg" &sol;><&sol;div>&NewLine;<p>&NewLine;In spring the &commat;RequestParam annotation is used to bind request parameter values to the handler method arguments in controller&period; Let&&num;8217&semi;s see use of it with example in this article&period;<&sol;p>&NewLine;<pre class&equals;"highlight">&commat;Target&lpar;value&equals;PARAMETER&rpar; &NewLine;&commat;Retention&lpar;value&equals;RUNTIME&rpar; &NewLine;&commat;Documented &NewLine;public &commat;interface RequestParam &NewLine;<&sol;pre>&NewLine;<div align&equals;"center" id&equals;"ads-id"><&sol;div>&NewLine;<ul style&equals;"text-align&colon; left&semi;">&NewLine;<li><b>defaultValue<&sol;b>&&num;8211&semi; 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&period;<&sol;li>&NewLine;<li><b>name<&sol;b>&&num;8211&semi; It is String type attribute and name of the request parameter to bind to&period;<&sol;li>&NewLine;<li><b>required<&sol;b>&&num;8211&semi; It is Boolean type attribute whether the parameter is required&period;<&sol;li>&NewLine;<li><b>value<&sol;b>&&num;8211&semi; It is String type attribute and it is alias for name attribute&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<div style&equals;"background-color&colon; &num;f2f9fc&semi; border&colon;1px solid &num;c9e6f2&semi;border-radius&colon;3px&semi;padding&colon;16px&semi;line-height&colon;1&period;45&semi;">&NewLine;<span style&equals;"color&colon; red&semi;font-size&colon; x-large&semi;text-align&colon; center&semi;"><b>Popular Tutorials<&sol;b><&sol;span><&sol;p>&NewLine;<ul style&equals;"text-align&colon; left&semi;">&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-tutorial&sol;"><em><strong>Spring Tutorial<&sol;strong> <&sol;em><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-web-mvc-framework-chapter-38&sol;"><strong><em>Spring MVC Web Tutorial <&sol;em><&sol;strong><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;introduction-to-spring-boot-a-spring-boot-complete-guide&sol;"><strong>Spring Boot Tutorial<&sol;strong> <&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-security-take-baby-step-to-secure&sol;"><em>Spring Security Tutorial<&sol;em><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-aop-tutorial-with-example-aspect-advice-pointcut-joinpoint&sol;"><em>Spring AOP Tutorial<&sol;em><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;using-spring-jdbc-framework-chapter-32&sol;"><em>Spring JDBC Tutorial<&sol;em><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-hateoas-hypermedia-driven-restful-web-service&sol;"><em><strong>Spring HATEOAS <&sol;strong><&sol;em><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;microservices-with-spring-boot&sol;"><em><strong>Microservices with Spring Boot<&sol;strong><&sol;em><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;jax-rs-web-service-tutorial&sol;"><strong><em>REST Webservice<&sol;em> <&sol;strong><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;core-java-baby-step-to-be-best-java-ian&sol;"><em><strong>Core Java <&sol;strong><&sol;em><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;hibernate-3-on-baby-steps&sol;"><em><strong>Hibernate Tutorial<&sol;strong><&sol;em><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-batch-process-with-example&sol;"><strong><em>Spring Batch<&sol;em> <&sol;strong><&sol;a><&sol;b><&sol;li>&NewLine;<&sol;ul>&NewLine;<&sol;div>&NewLine;<p>&NewLine;<b>The &&num;8216&semi;defaultValue&&num;8217&semi; attribute of &commat;RequestParam<&sol;b><br &sol;>&NewLine;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&period; Providing a default value implicitly sets &&num;8216&semi;required&&num;8217&semi; to false&period;<&sol;p>&NewLine;<pre class&equals;"highlight">&commat;RequestParam&lpar;defaultValue&equals;"dineshonjava&period;com"&rpar; String siteName &NewLine;<&sol;pre>&NewLine;<p><b>The &&num;8216&semi;required&&num;8217&semi; attribute of &commat;RequestParam<&sol;b><br &sol;>&NewLine;It is Boolean type attribute whether the parameter is required&period; The default is true&period; If parameter is missing in the request then it will be returned status code 400&period; We can override this to false if the parameter is not present in the request&period;<&sol;p>&NewLine;<p>Following will be mapped with both <b><i>&sol;tutorials&sol;bookmark&quest;siteName&equals;dineshonjava<&sol;i> <&sol;b>and <b><i>&sol;tutorials&sol;bookmark<&sol;i><&sol;b><br &sol;>&NewLine;<b><br &sol;>&NewLine;<&sol;b> <&sol;p>&NewLine;<pre class&equals;"highlight">&commat;Controller &NewLine;public class TutorialController &lbrace; &NewLine; &NewLine; &commat;RequestMapping&lpar;value &equals; "&sol;tutorials&sol;bookmark"&rpar; &NewLine; public String bookMarkTutorial &lpar; &NewLine; &commat;RequestParam&lpar;required &equals; false&rpar; String siteName&comma; &NewLine; Model model&rpar; &lbrace; &NewLine; model&period;addAttribute&lpar;"I have bookmarked tutorial &colon; " &plus; siteName&rpar;&semi; &NewLine; return "success"&semi; &NewLine; &rcub; &NewLine;&rcub; &NewLine;<&sol;pre>&NewLine;<p>&NewLine;<b>Using &commat;RequestParam with value and without value attribute<&sol;b><&sol;p>&NewLine;<p><b>Using &&num;8216&semi;value&&num;8217&semi; element of &commat;RequestParam<&sol;b><br &sol;>&NewLine;It is String type attribute and it is alias for name attribute&period; &&num;8216&semi;value&&num;8217&semi; attribute of &commat;RequestParam is used to specify URL query param name&period; Following handler method will be mapped with the request <i><b>&sol;tutorials&sol;bookmark&quest;site&equals;dineshonjava<&sol;b> <&sol;i>&colon;<&sol;p>&NewLine;<pre class&equals;"highlight">Controller &NewLine;public class TutorialController &lbrace; &NewLine; &NewLine; &commat;RequestMapping&lpar;value &equals; "&sol;tutorials&sol;bookmark"&rpar; &NewLine; public String bookMarkTutorial &lpar; &NewLine; &commat;RequestParam&lpar;"site"&rpar; String siteName&comma;Model map&rpar; &lbrace; &NewLine; model&period;addAttribute&lpar;"I have bookmarked tutorial &colon; " &plus; siteName&rpar;&semi; &NewLine; return "success"&semi; &NewLine; &rcub; &NewLine;&rcub; &NewLine;<&sol;pre>&NewLine;<p>&NewLine;<b>&commat;RequestParam without &&num;8216&semi;value&&num;8217&semi; element<&sol;b><br &sol;>&NewLine;We could skip the &&num;8216&semi;value&&num;8217&semi; attribute of &commat;RequestParam annotation if variable name of handler method is same as request parameter name&period;<br &sol;>&NewLine;Following handler will be mapped with <b><i>&sol;tutorials&sol;bookmark&quest;siteName&equals;dineshonjava<&sol;i><&sol;b>&colon;<&sol;p>&NewLine;<pre class&equals;"highlight">&commat;Controller &NewLine;public class TutorialController &lbrace; &NewLine; &NewLine; &commat;RequestMapping&lpar;value &equals; "&sol;tutorials&sol;bookmark"&rpar; &NewLine; public String bookMarkTutorial &lpar; &NewLine; &commat;RequestParam String siteName&comma; &NewLine; Model model&rpar; &lbrace; &NewLine; model&period;addAttribute&lpar;"I have bookmarked tutorial &colon; " &plus; siteName&rpar;&semi; &NewLine; return "success"&semi; &NewLine; &rcub; &NewLine;&rcub; &NewLine;<&sol;pre>&NewLine;<p>&NewLine;<b>Using multiple &commat;RequestParam annotations<&sol;b><br &sol;>&NewLine;A handler method can have any number of &commat;RequestParam annotations&comma; it depended on the request parameters we are passing to the handler method&period; Following handler will be mapped with<br &sol;>&NewLine;<b><i>&sol;tutorials&sol;bookmark&quest;siteName&equals;dineshonjava&amp&semi;author&equals;dinesh<&sol;i><&sol;b>&colon;<&sol;p>&NewLine;<pre class&equals;"highlight">&commat;Controller &NewLine;public class TutorialController &lbrace; &NewLine; &NewLine; &commat;RequestMapping&lpar;value &equals; "&sol;tutorials&sol;bookmark"&rpar; &NewLine; public String bookMarkTutorial &lpar; &NewLine; &commat;RequestParam String siteName&comma; &NewLine; &commat;RequestParam String author&comma; &NewLine; Model model&rpar; &lbrace; &NewLine; model&period;addAttribute&lpar;"I have bookmarked tutorial &colon; " &plus; siteName&plus;" Author &colon; "&plus;author&rpar;&semi; &NewLine; return "success"&semi; &NewLine; &rcub; &NewLine;&rcub; &NewLine;<&sol;pre>&NewLine;<p>&NewLine;<b>Using Map with &commat;RequestParam for multiple params<&sol;b><br &sol;>&NewLine;Spring provide support for bind the request parameters to the Map and MultiValueMap&period; All query string names and values are populated to the Map&period;&nbsp&semi;Following will be mapped with <i><b>&sol;tutorials&sol;bookmark&quest;siteName&equals;dineshonjava&amp&semi;author&equals;dinesh&colon; <&sol;b><&sol;i><&sol;p>&NewLine;<p><i><b><br &sol;>&NewLine;<&sol;b><&sol;i> <&sol;p>&NewLine;<pre class&equals;"highlight">&commat;Controller &NewLine;public class TutorialController &lbrace; &NewLine; &NewLine; &commat;RequestMapping&lpar;value &equals; "&sol;tutorials&sol;bookmark"&rpar; &NewLine; public String bookMarkTutorial &lpar; &NewLine; &commat;RequestParam Map queryMap &NewLine; Model model&rpar; &lbrace; &NewLine; model&period;addAttribute&lpar;"I have bookmarked tutorial &colon; " &plus; queryMap&period;get&lpar;"siteName"&rpar;&plus; &NewLine; " Author &colon; "&plus;queryMap&period;get&lpar;"author"&rpar;&rpar;&semi; &NewLine; return "success"&semi; &NewLine; &rcub; &NewLine;&rcub; &NewLine;<&sol;pre>&NewLine;<p>&NewLine;<b> Summary<&sol;b><br &sol;>&NewLine;In Spring &commat;RequestParam annotation we can use for bind the request parameters to the handler method arguments&period; It provides auto-type conversion by default for parameters like int&comma; float&comma; String&comma; Date etc&period; We can also override the default behavior of this annotation like required attribute set false to non mandatory request parameters&period;<&sol;p>&NewLine;<p><&sol;p>&NewLine;<div style&equals;"background-color&colon; &num;f2f9fc&semi; border&colon;1px solid &num;c9e6f2&semi;border-radius&colon;3px&semi;padding&colon;16px&semi;line-height&colon;1&period;45&semi;">&NewLine;<span style&equals;"color&colon; red&semi;font-size&colon; x-large&semi;text-align&colon; center&semi;"><b>Spring MVC Related Posts<&sol;b><&sol;span><&sol;p>&NewLine;<ol style&equals;"text-align&colon; left&semi;"><span style&equals;"color&colon; &num;274e13&semi;"><&sol;p>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-web-mvc-framework-chapter-38&sol;"><span style&equals;"color&colon; red&semi;">Spring MVC Web Tutorial<&sol;span><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-mvc-interview-questions-and-answers&sol;"><span style&equals;"color&colon; red&semi;">Spring MVC Interview Questions<&sol;span><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;introduction-to-mvc&sol;"><span style&equals;"color&colon; red&semi;">MVC Design Pattern<&sol;span><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;what-is-dispatcherservlet-in-spring-and-its-uses&sol;"><span style&equals;"color&colon; red&semi;">Spring MVC DispatcherServlet <&sol;span><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;difference-between-applicationcontext-webapplicationcontext-in-spring-mvc&sol;"><span style&equals;"color&colon; red&semi;">Spring MVC WebApplicationContext and Root Application Context<&sol;span><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;stereotype-annotations-in-spring&sol;"><span style&equals;"color&colon; red&semi;">Spring MVC &commat;Controller Annotation<&sol;span><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;requestmapping-annotation-in-spring-mvc&sol;"><span style&equals;"color&colon; red&semi;">Spring MVC &commat;RequestMapping Annotation<&sol;span><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;requestparam-annotation-in-spring-mvc-with-example&sol;"><span style&equals;"color&colon; red&semi;">Spring MVC &commat;RequestParam Annotation<&sol;span><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;difference-between-applicationcontext-webapplicationcontext-in-spring-mvc&sol;"><span style&equals;"color&colon; red&semi;">Spring MVC ContextLoaderListener<&sol;span><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;requestparam-vs-pathvariable-annotations-in-spring-mvc&sol;"><span style&equals;"color&colon; red&semi;">Spring MVC &commat;RequestParam and &commat;PathVariable annotations<&sol;span><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-30-mvc-hello-world-example&sol;"><span style&equals;"color&colon; red&semi;">Spring MVC Hello World Example<&sol;span><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-exception-handling-example&sol;"><span style&equals;"color&colon; red&semi;">Spring MVC Exception Handling Example<&sol;span><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-mvc-with-hibernate-crud-example&sol;"><span style&equals;"color&colon; red&semi;">Spring MVC with Hibernate CRUD Example<&sol;span><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-3-mvc-tiles-plugin-with-example&sol;"><span style&equals;"color&colon; red&semi;">Spring MVC Tiles Plugin with Example<&sol;span><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-3-mvc-and-interceptor-with&sol;"><span style&equals;"color&colon; red&semi;">Spring MVC Interceptor with example<&sol;span><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;integration-spring-mvc3-and-mongodb&sol;"><span style&equals;"color&colon; red&semi;">Spring MVC with MongoDB CRUD Example<&sol;span><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-3-mvc-internationalization&sol;"><span style&equals;"color&colon; red&semi;">Spring MVC Internationalization &&num;038&semi; Localization with Example<&sol;span><&sol;a><&sol;b><&sol;li>&NewLine;<p><&sol;span><&sol;ol>&NewLine;<&sol;div>&NewLine;<p><&sol;p>&NewLine;<div>&NewLine;<&sol;div>&NewLine;<div>&NewLine;<&sol;div>&NewLine;<&sol;div>&NewLine;<div class&equals;"wp-post-navigation"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <div class&equals;"wp-post-navigation-pre"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;difference-between-applicationcontext-webapplicationcontext-in-spring-mvc&sol;">Previous<&sol;a> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <&sol;div> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <div class&equals;"wp-post-navigation-next"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;requestparam-vs-pathvariable-annotations-in-spring-mvc&sol;">Next<&sol;a> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <&sol;div> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;<&sol;div>&NewLine;<script type&equals;"text&sol;javascript">&NewLine;jQuery&lpar;document&rpar;&period;ready&lpar;function&lpar;&dollar;&rpar; &lbrace;&NewLine; &dollar;&period;post&lpar;'https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-admin&sol;admin-ajax&period;php'&comma; &lbrace;action&colon; 'mts&lowbar;view&lowbar;count'&comma; id&colon; '78'&rcub;&rpar;&semi;&NewLine;&rcub;&rpar;&semi;&NewLine;<&sol;script>

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…

4 years ago

Decorator Pattern using Lambda

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

4 years ago

Delegating pattern using lambda

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

4 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;…

4 years ago

TOP 20 MongoDB INTERVIEW QUESTIONS 2022

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

4 years ago

Scheduler @Scheduled Annotation Spring Boot

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

4 years ago