Path Variables and Request (Aram
Path Variables and Request (Aram
com/2017/10/differences-between-requestparam-and-
pathvariable-annotations-spring-mvc.html#axzz6f79Knayu
The Spring MVC framework, one of the most popular frameworks for developing a
web application in Java world also provides several useful annotations to extract
data from the incoming request and mapping the request to the controller,
like @RequestMapping, @RequestParam, and @PathVariable. Even though
both @RequestParam and @PathVariable is used to extract values from the
HTTP request, there is a subtle difference between them, which makes them a
useful question from an interview and spring certification point of view. We'll examine
the subtle difference between @RequestParam and @PathVaraible in this article.
For example, if the incoming HTTP request to retrieve a book on topic "Java"
is http://localhost:8080/shop/order/1001/receipts?date=12-05-
2017, then you can use the @RequestParam annotation to retrieve the query
parameter date and you can use @PathVariable to extract the orderId i.e.
"1001" as shown below:
@RequestMapping(value="/order/{orderId}/receipts", method =
RequestMethod.GET)
public List listUsersInvoices(
@PathVariable("orderId") int order,
@RequestParam(value = "date", required = false) Date
dateOrNull) {
...
}
By the way, if you are new to Spring framework then I also suggest you join a
comprehensive and up-to-date course to learn Spring in depth. If you need
recommendations, I highly suggest you take a look at Spring Framework 5:
Beginner to Guru, one of the comprehensive and hands-on course to learn modern
Spring. It' also most up-to-date and covers Spring 5.
Request Parameters
Path Variables
Form inputs
For examples, suppose you have a web application which returns details of orders
and trades, and you have the following URLs:
http://localhost:8080/eportal/orders?id=1001
To accept the query parameters in the above URLs, you can use the following code
in the Spring MVC controller:
@RequestMapping("/orders")
public String showOrderDetails(@RequestParam("id") String
orderId, Model model){
model.addAttribute("orderId", orderId);
return "orderDetails";
}
If the name of the query parameter is the same as the name of the variable in
handler's @RequestParam annotated argument then you can simply
use @RequestParam without specifying the name of a query parameter, Spring will
automatically derive the value (see Introduction to Spring MVC).
URL: http://localhost:8080/eportal/trades?tradeId=2001
@RequestMapping("/trades")
public String showTradeDetails(@RequestParam String tradeId,
Model model){
model.addAttribute("tradeId", tradeId);
return "tradeDetails";
}
If you have worked in RESTful Web services, you might know that the REST
URIs contains values, e.g. a REST API to retrieve a book using ISBN number looks
like the following:
URL: http://localhost:8080/book/9783827319333
Now, to extract the value of ISBN number from the URI in your Spring MVC
Controller's handler method, you can use @PathVariable annotation as shown in
the following code:
@RequestMapping(value="/book/{ISBN}", method=
RequestMethod.GET)
public String showBookDetails(@PathVariable("ISBN") String id,
Model model){
model.addAttribute("ISBN", id);
return "bookDetails";
}
For example, you can rewrite the above code as shown below:
@RequestMapping(value="/book/{ISBN}", method=
RequestMethod.GET)
public String showBookDetails(@PathVariable String ISBN,
Model model){
model.addAttribute("ISBN", ISBN);
return "bookDetails";
}
Spring MVC provides several useful annotations to map and extract data from the
HTTP request, and as of Spring developer, you should be aware of these,
e.g. @RequestMapping, @RequestParam, and @PathVariable.
These concepts and annotations are fundamental from both Spring MVC interview
prospects as well as Spring certifications. You will always find a couple of questions
based on these concepts.
Btw, if you are preparing for Spring Professional Certifications, you can also check
out David Mayer's free Spring Mock Questions to get an idea about the level of
questions and format of questions you can expect on real Spring Web Certification.
If you want to learn more about how to effectively develop REST applications using
Spring, I suggest you join Eugen Paraschiv's REST with Spring course.
It's an online course that will teach you the nitty-gritty of developing both REST API
and services for real-world scenarios.
Eugen has extensive experience in developing RESTful web services using Spring,
and this course is a great way to tap into his knowledge.
This concept is essential for both traditional web application development as well as
developing RESTful Web Services using Spring, so you must spend some time to
understand it better.
Read more: https://javarevisited.blogspot.com/2017/10/differences-between-
requestparam-and-pathvariable-annotations-spring-mvc.html#ixzz6f7Uw3gCI