Spring MVC

Spring @RequestMapping Annotation Example with @RequestParam, @PathVariable

<div dir&equals;"ltr" style&equals;"text-align&colon; justify&semi;">&NewLine;<div dir&equals;"ltr" style&equals;"text-align&colon; justify&semi;">&NewLine;<div dir&equals;"ltr" style&equals;"text-align&colon; justify&semi;">&NewLine;<div class&equals;"separator" style&equals;"clear&colon; both&semi; text-align&colon; center&semi;"><img src&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-content&sol;uploads&sol;2016&sol;03&sol;spring-requestmapping-requestparam-pathvariable-example&period;jpg" border&equals;"0" &sol;><&sol;div>&NewLine;<p>In this article&comma; we are going discuss about one of the main annotation in Spring MVC &commat;RequestMapping – used to mapping a web request to Spring Controller&&num;8217&semi;s handler methods&period;<&sol;p>&NewLine;<p>All incoming requests are handled by the Dispatcher Servlet and it route them through Spring&period; When it receives a web request&comma; it determines which controllers should handle the incoming request&period;<&sol;p>&NewLine;<p>Dispatcher Servlet initially scans all the classes that are annotated with the &commat;Controller annotation&period; The dispatching process depends on the various &commat;RequestMapping annotations declared in a controller class and its handler methods&period;<&sol;p>&NewLine;<div id&equals;"ads-id" align&equals;"center"><&sol;div>&NewLine;<p>There are three levels of request mapping in Spring MVC<&sol;p>&NewLine;<ul>&NewLine;<li><b>&commat;RequestMapping annotations at only method level<&sol;b><&sol;li>&NewLine;<li><b>&commat;RequestMapping annotations at class level<&sol;b><&sol;li>&NewLine;<li><b>Mapping requests by request type<&sol;b><&sol;li>&NewLine;<&sol;ul>&NewLine;<h2><b>&commat;RequestMapping<&sol;b><&sol;h2>&NewLine;<p>It can be applied to the controller class as well as methods&period; This annotation for mapping web requests onto specific handler classes and&sol;or handler methods&period;<&sol;p>&NewLine;<div class&equals;"separator" style&equals;"clear&colon; both&semi; text-align&colon; center&semi;"><img src&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-content&sol;uploads&sol;2016&sol;03&sol;spring-mvc-request-mapping-annotation&period;jpg" border&equals;"0" &sol;><&sol;div>&NewLine;<p><b><i>&commat;ImageSource-SlideShare&period;net<&sol;i><&sol;b><br &sol;>&NewLine;<b><&sol;b><&sol;p>&NewLine;<h2><b>&commat;RequestMapping annotations at only method level<&sol;b><&sol;h2>&NewLine;<p>In this case we are using &commat;RequestMapping annotations is to declare the handler methods directly&period; The handler method is annotated with &commat;RequestMapping annotation containing a URL pattern&period; If this pattern of a handler &commat;RequestMapping annotation matches the request URL&comma; DispatcherServlet it dispatches the request to this handler for it to process the request&period;<br &sol;>&NewLine;<b><&sol;b><&sol;p>&NewLine;<h3><b>1&period; &commat;RequestMapping Basics<&sol;b><&sol;h3>&NewLine;<p>Let’s discuss with a simple example – mapping an HTTP request to a method using some straightforward criteria&period;<&sol;p>&NewLine;<h3><b>1&period;1 &commat;RequestMapping – by Path<&sol;b><&sol;h3>&NewLine;<pre class&equals;"highlight">&commat;RequestMapping&lpar;value &equals; "&sol;doj&sol;spring"&rpar; &NewLine; &commat;ResponseBody &NewLine; public String getDOJSpringCourse&lpar;&rpar;&lbrace; &NewLine; return "Get DOJ Course for Spring"&semi; &NewLine; &rcub; &NewLine;<&sol;pre>&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;"><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>For testing hit following URL on browser&colon;<&sol;p>&NewLine;<p><b><i>GET http&colon;&sol;&sol;localhost&colon;8080&sol;SpringApp&sol;doj&sol;spring<&sol;i><&sol;b><&sol;p>&NewLine;<p><i>&&num;8212&semi; response &&num;8212&semi;<&sol;i><br &sol;>&NewLine;<i>200 OK<&sol;i><br &sol;>&NewLine;<i>Server&colon; Apache-Coyote&sol;1&period;1<&sol;i><br &sol;>&NewLine;<i>Content-Type&colon; text&sol;html<&sol;i><br &sol;>&NewLine;<i>Content-Length&colon; 25<&sol;i><br &sol;>&NewLine;<i>Date&colon; Fri&comma; 18 Mar 2016 10&colon;38&colon;14 GMT<&sol;i><br &sol;>&NewLine;<i><br &sol;>&NewLine;<&sol;i> <i>Get DOJ Course for Spring<&sol;i><&sol;p>&NewLine;<h3><b>1&period;2  &commat;RequestMapping – the HTTP Method<&sol;b><&sol;h3>&NewLine;<p>There is no by default HTTP method mapped by a &commat;RequestMapping – so it maps to any type of HTTP request&semi; we sh to add the method &colon;<&sol;p>&NewLine;<pre class&equals;"highlight">&commat;RequestMapping&lpar;value &equals; "&sol;doj&sol;spring"&comma; method &equals; RequestMethod&period;POST&rpar; &NewLine; &commat;ResponseBody &NewLine; public String postDOJSpringCourse&lpar;&rpar;&lbrace; &NewLine; return "Post DOJ Course for Spring"&semi; &NewLine; &rcub; &NewLine;<&sol;pre>&NewLine;<p><b><i>POST http&colon;&sol;&sol;localhost&colon;8080&sol;SpringApp&sol;doj&sol;spring<&sol;i><&sol;b><br &sol;>&NewLine;<i>Content-Type&colon; application&sol;json<&sol;i><br &sol;>&NewLine;<i><br &sol;>&NewLine;<&sol;i> <i>&&num;8212&semi; response &&num;8212&semi;<&sol;i><br &sol;>&NewLine;<i>200 OK<&sol;i><br &sol;>&NewLine;<i>Server&colon; Apache-Coyote&sol;1&period;1<&sol;i><br &sol;>&NewLine;<i>Content-Type&colon; text&sol;html<&sol;i><br &sol;>&NewLine;<i>Content-Length&colon; 26<&sol;i><br &sol;>&NewLine;<i>Date&colon; Fri&comma; 18 Mar 2016 10&colon;38&colon;22 GMT<&sol;i><br &sol;>&NewLine;<i><br &sol;>&NewLine;<&sol;i> <i>Post DOJ Course for Spring<&sol;i><&sol;p>&NewLine;<h3><b>2&period; RequestMapping with HTTP Headers<&sol;b><&sol;h3>&NewLine;<h3><b>2&period;1&period; &commat;RequestMapping with the headers attribute<&sol;b><&sol;h3>&NewLine;<p>The mapping can be narrowed even further by specifying a header for the request&colon;<&sol;p>&NewLine;<pre class&equals;"highlight">&commat;RequestMapping&lpar;value &equals; "&sol;doj&sol;spring"&comma; headers &equals; "key&equals;val"&rpar; &NewLine; &commat;ResponseBody &NewLine; public String getDOJSpringCourseWithHeaders&lpar;&rpar;&lbrace; &NewLine; return "Get DOJ Course for Spring with headers"&semi; &NewLine; &rcub; &NewLine;<&sol;pre>&NewLine;<p>And even multiple headers via the header attribute of &commat;RequestMapping&colon;<&sol;p>&NewLine;<pre class&equals;"highlight">&commat;RequestMapping&lpar;value &equals; "&sol;doj&sol;spring"&comma; headers &equals; &lbrace;"key1&equals;val1"&comma; "key2&equals;val2"&rcub;&rpar; &NewLine; &commat;ResponseBody &NewLine; public String getDOJSpringCourseWithHeaders&lpar;&rpar;&lbrace; &NewLine; return "Get DOJ Course for Spring with headers"&semi; &NewLine; &rcub; &NewLine;<&sol;pre>&NewLine;<h3><b>2&period;2&period; &commat;RequestMapping Consumes and Produces<&sol;b><&sol;h3>&NewLine;<p>Mapping media types produced by a controller method is worth special attention – we can map a request based on its Accept header via the &commat;RequestMapping headers attribute introduced above&colon;<&sol;p>&NewLine;<pre class&equals;"highlight">&commat;RequestMapping&lpar;value &equals; "&sol;doj&sol;spring"&comma; headers &equals; "Accept&equals;application&sol;json"&rpar; &NewLine; &commat;ResponseBody &NewLine; public String getDOJSpringCourseJSON&lpar;&rpar;&lbrace; &NewLine; return "Get DOJ Course for Spring as JSON"&semi; &NewLine; &rcub; &NewLine;<&sol;pre>&NewLine;<p><b><i>GET http&colon;&sol;&sol;localhost&colon;8080&sol;SpringApp&sol;doj&sol;spring<&sol;i><&sol;b><br &sol;>&NewLine;<i>key&colon; value<&sol;i><br &sol;>&NewLine;<i><br &sol;>&NewLine;<&sol;i> <i>&&num;8212&semi; response &&num;8212&semi;<&sol;i><br &sol;>&NewLine;<i>200 OK<&sol;i><br &sol;>&NewLine;<i>Server&colon; Apache-Coyote&sol;1&period;1<&sol;i><br &sol;>&NewLine;<i>Content-Type&colon; application&sol;json<&sol;i><br &sol;>&NewLine;<i>Content-Length&colon; 33<&sol;i><br &sol;>&NewLine;<i>Date&colon; Fri&comma; 18 Mar 2016 10&colon;57&colon;03 GMT<&sol;i><br &sol;>&NewLine;<i><br &sol;>&NewLine;<&sol;i> <i>Get DOJ Course for Spring as JSON<&sol;i><&sol;p>&NewLine;<p>&commat;RequestMapping annotation now has a produces and a consumes attributes&comma; specifically for this purpose&colon;<&sol;p>&NewLine;<pre class&equals;"highlight">&commat;RequestMapping&lpar;value &equals; "&sol;doj&sol;spring"&comma; method &equals; RequestMethod&period;GET&comma; produces &equals; "application&sol;json"&rpar; &NewLine; &commat;ResponseBody &NewLine; public String getDOJSpringCourseJSONFromREST&lpar;&rpar;&lbrace; &NewLine; return "Get DOJ Course for Spring as JSON from REST"&semi; &NewLine; &rcub; &NewLine;<&sol;pre>&NewLine;<p><b><i>GET http&colon;&sol;&sol;localhost&colon;8080&sol;SpringApp&sol;doj&sol;spring<&sol;i><&sol;b><br &sol;>&NewLine;<i>key&colon; value<&sol;i><br &sol;>&NewLine;<i><br &sol;>&NewLine;<&sol;i> <i>&&num;8212&semi; response &&num;8212&semi;<&sol;i><br &sol;>&NewLine;<i>200 OK<&sol;i><br &sol;>&NewLine;<i>Server&colon; Apache-Coyote&sol;1&period;1<&sol;i><br &sol;>&NewLine;<i>Content-Type&colon; application&sol;json<&sol;i><br &sol;>&NewLine;<i>Content-Length&colon; 43<&sol;i><br &sol;>&NewLine;<i>Date&colon; Fri&comma; 18 Mar 2016 11&colon;00&colon;28 GMT<&sol;i><br &sol;>&NewLine;<i><br &sol;>&NewLine;<&sol;i> <i>Get DOJ Course for Spring as JSON from REST<&sol;i><&sol;p>&NewLine;<p>Additionally&comma; produces support multiple values as well&colon;<&sol;p>&NewLine;<pre class&equals;"highlight">&commat;RequestMapping&lpar;value &equals; "&sol;doj&sol;spring"&comma; produces &equals; &lbrace; "application&sol;json"&comma; "application&sol;xml" &rcub;&rpar; &NewLine;<&sol;pre>&NewLine;<p>A final note on the new produces and consumes mechanism – these behave differently from most other annotations&colon; when specified at type level&comma; the method level annotations do not complement but override the type level information&period;<&sol;p>&NewLine;<h2><b>3&period; RequestMapping with Path Variables<&sol;b><&sol;h2>&NewLine;<p>Parts of the mapping URI can be bound to variables via the &commat;PathVariable annotation&period;<&sol;p>&NewLine;<h3><b>3&period;1&period; Single &commat;PathVariable<&sol;b><&sol;h3>&NewLine;<&sol;div>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<pre class&equals;"highlight">&commat;RequestMapping&lpar;value &equals; "&sol;doj&sol;spring&sol;&lbrace;id&rcub;"&rpar; &NewLine; &commat;ResponseBody &NewLine; public String getDOJCourseWithPathVariable&lpar;&commat;PathVariable&lpar;"id"&rpar; int id&rpar; &lbrace; &NewLine; return "Get a DOJ Course with id&equals;" &plus; id&semi; &NewLine; &rcub; &NewLine;<&sol;pre>&NewLine;<p><i><b>GET http&colon;&sol;&sol;localhost&colon;8080&sol;SpringApp&sol;doj&sol;spring&sol;2000<&sol;b><&sol;i><br &sol;>&NewLine;<i><br &sol;>&NewLine;<&sol;i> <i>&&num;8212&semi; response &&num;8212&semi;<&sol;i><br &sol;>&NewLine;<i>200 OK<&sol;i><br &sol;>&NewLine;<i>Server&colon; Apache-Coyote&sol;1&period;1<&sol;i><br &sol;>&NewLine;<i>Content-Type&colon; text&sol;html<&sol;i><br &sol;>&NewLine;<i>Content-Length&colon; 29<&sol;i><br &sol;>&NewLine;<i>Date&colon; Fri&comma; 18 Mar 2016 11&colon;11&colon;40 GMT<&sol;i><br &sol;>&NewLine;<i><br &sol;>&NewLine;<&sol;i> <i>Get a DOJ Course with id&equals;2000<&sol;i><&sol;p>&NewLine;<p>If the name of the method argument matches the name of the path variable exactly&comma; then this can be simplified by using &commat;PathVariable with no value&colon;<&sol;p>&NewLine;<pre class&equals;"highlight">&commat;RequestMapping&lpar;value &equals; "&sol;doj&sol;spring&sol;&lbrace;id&rcub;"&rpar; &NewLine; &commat;ResponseBody &NewLine; public String getDOJCourseWithPathVariable&lpar;&commat;PathVariable int id&rpar; &lbrace; &NewLine; return "Get a DOJ Course with id&equals;" &plus; id&semi; &NewLine; &rcub; &NewLine;<&sol;pre>&NewLine;<h3><b>3&period;2&period; Multiple &commat;PathVariable<&sol;b><&sol;h3>&NewLine;<p>More complex URI may need to map multiple parts of the URI to multiple values&colon;<&sol;p>&NewLine;<pre class&equals;"highlight">&commat;RequestMapping&lpar;value &equals; "&sol;doj&sol;&lbrace;course&rcub;&sol;&lbrace;id&rcub;"&rpar; &NewLine; &commat;ResponseBody &NewLine; public String getDOJCourseWithPathVariable&lpar;&commat;PathVariable&lpar;"course"&rpar; String course &comma;&commat;PathVariable&lpar;"id"&rpar; long id&rpar; &lbrace; &NewLine; return "Get a DOJ Course with id&equals;" &plus; id&plus;" course name &equals;"&plus;course&semi; &NewLine; &rcub; &NewLine;<&sol;pre>&NewLine;<p><i><b>GET http&colon;&sol;&sol;localhost&colon;8080&sol;SpringApp&sol;doj&sol;spring&sol;2000<&sol;b><&sol;i><br &sol;>&NewLine;<i><br &sol;>&NewLine;<&sol;i> <i>&&num;8212&semi; response &&num;8212&semi;<&sol;i><br &sol;>&NewLine;<i>200 OK<&sol;i><br &sol;>&NewLine;<i>Server&colon; Apache-Coyote&sol;1&period;1<&sol;i><br &sol;>&NewLine;<i>Content-Type&colon; text&sol;html<&sol;i><br &sol;>&NewLine;<i>Content-Length&colon; 49<&sol;i><br &sol;>&NewLine;<i>Date&colon; Fri&comma; 18 Mar 2016 11&colon;16&colon;19 GMT<&sol;i><br &sol;>&NewLine;<i><br &sol;>&NewLine;<&sol;i> <i>Get a DOJ Course with id&equals;2000 course name &equals;spring<&sol;i><&sol;p>&NewLine;<h3><b>3&period;3&period; &commat;PathVariable with RegEx<&sol;b><&sol;h3>&NewLine;<p>Regular expressions can also be used when mapping the &commat;PathVariable&semi; for example&comma; we will restrict the mapping to only accept numerical values for the id&colon;<&sol;p>&NewLine;<pre class&equals;"highlight">&commat;RequestMapping&lpar;value &equals; "&sol;doj&sol;&lbrace;course&rcub;&sol;&lbrace;numericId&colon;&lbrack;d&rsqb;&plus;"&rpar; &NewLine; &commat;ResponseBody &NewLine; public String getDOJCourseWithPathVariable&lpar;&commat;PathVariable&lpar;"course"&rpar; String course &comma;&commat;PathVariable&lpar;"id"&rpar; long id&rpar; &lbrace; &NewLine; return "Get a DOJ Course with id&equals;" &plus; id&plus;" course name &equals;"&plus;course&semi; &NewLine; &rcub; &NewLine;<&sol;pre>&NewLine;<p>This will mean that the following URIs will match&colon;<br &sol;>&NewLine;<b><i>http&colon;&sol;&sol;localhost&colon;8080&sol;SpringApp&sol;doj&sol;spring&sol;2000<&sol;i><&sol;b><&sol;p>&NewLine;<p>But this will not&colon;<br &sol;>&NewLine;<b><i>http&colon;&sol;&sol;localhost&colon;8080&sol;SpringApp&sol;doj&sol;spring&sol;xyz<&sol;i><&sol;b><&sol;p>&NewLine;<h2><b>4&period; RequestMapping with Request Parameters<&sol;b><&sol;h2>&NewLine;<p>&commat;RequestMapping allows easy mapping of URL parameters with the &commat;RequestParam annotation&period;<&sol;p>&NewLine;<p>We are now mapping a request to an URI such as&colon;<&sol;p>&NewLine;<&sol;div>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<pre class&equals;"highlight">&commat;RequestMapping&lpar;value &equals; "&sol;doj&sol;spring"&rpar; &NewLine; &commat;ResponseBody &NewLine; public String getDOJCourseByRequestParam&lpar;&commat;RequestParam&lpar;"id"&rpar; long id&rpar; &lbrace; &NewLine; return "Get a DOJ Course with id&equals;" &plus; id&semi; &NewLine; &rcub; &NewLine;<&sol;pre>&NewLine;<p>We are then extracting the value of the id parameter using the &commat;RequestParam&lpar;&OpenCurlyDoubleQuote;id”&rpar; annotation in the controller method signature&period;<&sol;p>&NewLine;<p><b><i>GET http&colon;&sol;&sol;localhost&colon;8080&sol;SpringApp&sol;doj&sol;spring&quest;id&equals;2000<&sol;i><&sol;b><&sol;p>&NewLine;<p><i>&&num;8212&semi; response &&num;8212&semi;<&sol;i><br &sol;>&NewLine;<i>200 OK<&sol;i><br &sol;>&NewLine;<i>Server&colon; Apache-Coyote&sol;1&period;1<&sol;i><br &sol;>&NewLine;<i>Content-Type&colon; text&sol;html<&sol;i><br &sol;>&NewLine;<i>Content-Length&colon; 29<&sol;i><br &sol;>&NewLine;<i>Date&colon; Fri&comma; 18 Mar 2016 11&colon;29&colon;25 GMT<&sol;i><br &sol;>&NewLine;<i><br &sol;>&NewLine;<&sol;i> <i>Get a DOJ Course with id&equals;2000<&sol;i><&sol;p>&NewLine;<p>&commat;RequestMapping can optionally define the parameters – as yet another way of narrowing the request mapping&colon;<&sol;p>&NewLine;<pre class&equals;"highlight">&commat;RequestMapping&lpar;value &equals; "&sol;doj&sol;spring"&comma; params &equals; "id"&rpar; &NewLine; &commat;ResponseBody &NewLine; public String getDOJCourseByRequestParam&lpar;&commat;RequestParam&lpar;"id"&rpar; long id&rpar; &lbrace; &NewLine; return "Get a DOJ Course with id&equals;" &plus; id&semi; &NewLine; &rcub; &NewLine;<&sol;pre>&NewLine;<p>Even more flexible mappings are allowed – multiple params values can be defined&comma; and not all of them have to be used&colon;<&sol;p>&NewLine;<pre class&equals;"highlight">&commat;RequestMapping&lpar;value &equals; "&sol;doj&sol;spring"&comma; params &equals; &lbrace;"id"&comma;"course"&rcub;&rpar; &NewLine; &commat;ResponseBody &NewLine; public String getDOJCourseByRequestParam&lpar;&commat;RequestParam&lpar;"id"&rpar; long id&comma; &commat;RequestParam&lpar;"course"&rpar; String course&rpar; &lbrace; &NewLine; return "Get a DOJ Course with id&equals;" &plus; id&plus;" Course&equals;"&plus;course&semi; &NewLine; &rcub; &NewLine;<&sol;pre>&NewLine;<h2><b>5&period; RequestMapping Corner Cases<&sol;b><&sol;h2>&NewLine;<h3><b>5&period;1&period; &commat;RequestMapping – multiple paths mapped to the same controller method<&sol;b><&sol;h3>&NewLine;<p>Although a single &commat;RequestMapping path value is usually used for a single controller method&comma; this is just good practice&comma; not a hard and fast rule – there are some cases where mapping multiple requests to the same method may be necessary&period; For that case&comma; the value attribute of &commat;RequestMapping does accept multiple mappings&comma; not just a single one&colon;<&sol;p>&NewLine;<pre class&equals;"highlight">&commat;RequestMapping&lpar;value &equals; &lbrace;"&sol;doj&sol;spring&sol;index"&comma; "&sol;doj&sol;spring&sol;home"&rcub;&rpar; &NewLine; &commat;ResponseBody &NewLine; public String getDOJHome&lpar;&rpar; &lbrace; &NewLine; return "Get a DOJ Home Page"&semi; &NewLine; &rcub; &NewLine;<&sol;pre>&NewLine;<h3><b>5&period;2&period; &commat;RequestMapping – multiple HTTP request methods to the same controller method<&sol;b><&sol;h3>&NewLine;<p>Multiple requests using different HTTP verbs can be mapped to the same controller method&colon;<&sol;p>&NewLine;<pre class&equals;"highlight">&commat;RequestMapping&lpar;value &equals; &lbrace;"&sol;doj&sol;spring&sol;home"&rcub;&comma; method &equals; &lbrace;RequestMethod&period;GET&comma; RequestMethod&period;POST&rcub;&rpar; &NewLine; &commat;ResponseBody &NewLine; public String getDOJHome&lpar;&rpar; &lbrace; &NewLine; return "Get a DOJ Home Page"&semi; &NewLine; &rcub; &NewLine;<&sol;pre>&NewLine;<h3><b>5&period;3&period; &commat;RequestMapping – a fallback for all requests<&sol;b><&sol;h3>&NewLine;<p>To implement a simple fallback for all requests using a specific HTTP method&colon;<&sol;p>&NewLine;<pre class&equals;"highlight">&commat;RequestMapping&lpar;value &equals; &lbrace;"&ast;"&rcub;&rpar; &NewLine; &commat;ResponseBody &NewLine; public String getDOJDefaultPage&lpar;&rpar; &lbrace; &NewLine; return "Get a DOJ Default Page"&semi; &NewLine; &rcub; &NewLine;<&sol;pre>&NewLine;<h2><b>&commat;RequestMapping annotations at class level<&sol;b><&sol;h2>&NewLine;<p>In this case we are using &commat;RequestMapping annotations at the class level&period; It is used for filter all incoming request at class level first before forwarding to handler methods&period; If the incoming request matches the pattern defined in controller class&comma; then it search the controller methods mappings&period;<&sol;p>&NewLine;<pre class&equals;"highlight">&commat;Controller &NewLine;&commat;RequestMapping&lpar;"&sol;login&sol;"&rpar; &NewLine;public class Login&lbrace; &NewLine; &NewLine; &commat;RequestMapping&lpar;"doLogin"&rpar; &NewLine; public ModelAndView doLogin&lpar; &NewLine; &commat;RequestParam&lpar;value &equals; "username"&rpar; String username&comma; &NewLine; &commat;RequestParam&lpar;value &equals; "password"&rpar; String password&rpar; &lbrace; &NewLine; &sol;&sol;&period;&period;&period;&period; &NewLine; &sol;&sol;&period;&period;&period;&period; &NewLine; return null&semi; &NewLine; &rcub; &NewLine; &NewLine; &commat;RequestMapping&lpar;value&equals;&lbrace;"logout"&comma;"signout"&rcub;&rpar; &NewLine; public ModelAndView logout&lpar; &NewLine; &commat;RequestParam&lpar;value &equals; "username"&rpar; String username&rpar; &lbrace; &NewLine; &sol;&sol;&period;&period;&period;&period; &NewLine; &sol;&sol;&period;&period;&period;&period; &NewLine; &NewLine; return null&semi; &NewLine; &rcub; &NewLine;&rcub; &NewLine;<&sol;pre>&NewLine;<h2><b>Conclusion<&sol;b><&sol;h2>&NewLine;<p>This article focus on the &commat;RequestMapping annotation in Spring – discussing a simple usecase&comma; the mapping of HTTP headers&comma; binding parts of the URI with &commat;PathVariable and working with URI parameters and the &commat;RequestParam annotation&period;<&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;"><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;<ul>&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 &amp&semi; Localization with Example<&sol;span><&sol;a><&sol;b><&sol;li>&NewLine;<&sol;ul>&NewLine;<p>&nbsp&semi;<&sol;p>&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;spring-mvc-exception-handling-example-and-return-custom-404-error-pages&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;exception-handling-for-rest-with-spring-using-exceptionhandler-and-controlleradvice-annotation&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; '152'&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