Understanding Spring MVC Annotations: @RequestMapping, @PathVariable, @RequestParam, @CookieValue and RESTful Request Handling
This article explains how Spring MVC uses annotations such as @RequestMapping, @PathVariable, @RequestParam and @CookieValue to map URLs, retrieve parameters, implement RESTful CRUD operations via hidden method filters, and provides complete code examples for each scenario.
Spring MVC provides a rich set of annotations that simplify request handling. @RequestMapping maps URLs to controller classes or methods and supports pattern matching with ?, *, and ** for flexible path definitions.
@RequestMapping("/testRequestMapping")
public String testRequestMapping(){
System.out.println("testRequestMapping");
return SUCCESS;
}When placed on a class, the annotation defines a common prefix for all methods in that class.
@RequestMapping("/springmvc")
@Controller
public class SpringMVCTest {
// ...
}The @PathVariable annotation binds URL placeholders to method parameters, enabling extraction of values directly from the path.
@RequestMapping("/testPathVariable/{id}")
public String testPathVariable(@PathVariable(value="id") Integer id){
System.out.println("testPathVariable:" + id);
return SUCCESS;
}In the view, a link such as <a href="springmvc/testPathVariable/1">testPathVariable</a> triggers the method and passes the value 1 to the parameter.
The @RequestParam annotation retrieves query‑string parameters. It can specify default values and whether the parameter is required.
@RequestMapping(value="/testRequestParam")
public String testRequestParam(@RequestParam(value="username") String username,
@RequestParam(value="age", required=false, defaultValue="0") int age){
System.out.println("testRequestParam username:" + username + " age:" + age);
return SUCCESS;
}A corresponding link in the JSP like <a href="springmvc/testRequestParam?username=jackie&age=12">testRequestParam</a> passes the parameters to the method.
To support RESTful CRUD operations, Spring MVC can handle PUT and DELETE methods by using the HiddenHttpMethodFilter . The filter converts a POST request that contains a hidden input named _method into the desired HTTP method.
@RequestMapping(value="/testRest/{id}", method=RequestMethod.PUT)
public String testRestPut(@PathVariable(value="id") Integer id){
System.out.println("test put:" + id);
return SUCCESS;
}
@RequestMapping(value="/testRest/{id}", method=RequestMethod.DELETE)
public String testRestDelete(@PathVariable(value="id") Integer id){
System.out.println("test delete:" + id);
return SUCCESS;
}
@RequestMapping(value="/testRest", method=RequestMethod.POST)
public String testRest(){
System.out.println("test post");
return SUCCESS;
}
@RequestMapping(value="/testRest/{id}", method=RequestMethod.GET)
public String testRest(@PathVariable(value="id") Integer id){
System.out.println("test get:" + id);
return SUCCESS;
}In the JSP, forms use a hidden field to indicate the intended method:
<form action="springmvc/testRest/1" method="post">
<input type="hidden" name="_method" value="PUT"/>
<input type="submit" value="testRestPut"/>
</form>
<form action="springmvc/testRest/1" method="post">
<input type="hidden" name="_method" value="DELETE"/>
<input type="submit" value="TestRest DELETE"/>
</form>
<form action="springmvc/testRest" method="post">
<input type="submit" value="testRestPost">
</form>
<a href="springmvc/testRest/1">testRest</a>The required filter is declared in web.xml :
<!-- Enable HiddenHttpMethodFilter to convert POST to DELETE/PUT -->
<filter>
<filter-name>HiddenHttpMethodFilter</filter-name>
<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>HiddenHttpMethodFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>The @CookieValue annotation binds a specific cookie value to a method parameter, allowing easy access to cookies such as JSESSIONID .
@RequestMapping(value="/testCookieValue")
public String testCookieValue(@CookieValue("JSESSIONID") String cookieValue){
System.out.println("testCookieValue: " + cookieValue);
return SUCCESS;
}A link like <a href="springmvc/testCookieValue">testCookieValue</a> triggers the method and prints the cookie value.
In summary, the article covers:
Usage of @RequestMapping for URL mapping.
Retrieving request data with @PathVariable and @RequestParam.
Implementing RESTful CRUD operations using HiddenHttpMethodFilter to simulate PUT and DELETE.
Accessing cookie values via @CookieValue.
Java Captain
Focused on Java technologies: SSM, the Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading; occasionally covers DevOps tools like Jenkins, Nexus, Docker, ELK; shares practical tech insights and is dedicated to full‑stack Java development.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.