rest web services

https://www.baeldung.com/spring-request-param

1. @RequestParam VS @PathVariable

Even though both @RequestParam and @ParthVariable is used to extract values from the HTTP request, there is a subtle difference between them.

  1. The @RequestParam is used to extract query parameters while @PathVariable is used to extract data right from the URI.

  2. @RequestParam is more useful on a traditional web application where data is mostly passed in the query abatements while @PathVariable is more suitable for RESTful web services where URL contains values e.g. http://localhost:8080/book/9783827319333, here data, which is ISBN number is part of URI.

  3. @RequestParam annotation can specify default values if a query parameter is not present or empty by using a defaultValue attribute, provided the required attribute is false.

  4. Spring MVC allows you to use multiple @PathVariable annotations in the same method, provided, no more than one argument has the same pattern.

2.@RequestMapping vs @GetMapping vs @PostMapping...

@RequestMapping(value="/orders", method = RequestMethod.GET)
public String getOrders(){
    return "All orders";
}
//equals
@GetMapping("/orders")

Last updated