- Specialization of @Component annotation, used to define a Controller class in terms of MVC pattern
- DispatcherServlet recognizes controller classes having this annotation,
- and maps incoming request to correct controller, by reading @RequestMapping accompanying annotation
How is an incoming request mapped to a controller and mapped to a method?
- Request reaches application server which calls DispatcherServlet
- DispatcherServlet uses HandlerMapping components to gather all controllers (HandlerMapping already has scanned all @Controllers from classpath, on init)
- DispatcherServlet takes in account the @RequestMapping's path among other params to define the correct class/method.
- DispatcherServlet uses HandlerAdapter to call controller method's execution
What is the difference between @RequestMapping and @GetMapping?
- @GetMapping annotation is a composed annotation equal to @RequestMapping(method = RequestMethod.GET)
- Other specializations of @RequestMapping: @[Put/Delete/Post]Mapping
- @GetMapping does not support attribute "consumes" as @RequestMapping does.
What is @RequestParam used for?
- @RequestParam is used for binding web request parameters to controller's method parameters
- Example: public String getAge (@RequestParam("age") int age, ...)
- Can use attributes like value, required, defaultValue, and Java 8's Optional
What are the differences between @RequestParam and @PathVariable?
- @PathVariable maps parts of URI with controller method parameters
- example: @GetMapping("book/orders/{orderId}")
public String getBookOrderById(@PathVariable("orderId") int orderId, .. , ..) - @RequestParam is used for binding web request parameters to controller's method parameters
- @RequestParam provides defaultValue, while @PathVariable does not
What are the ready-to-use argument types you can use in a controller method?
- WebRequest, NativeWebRequest
- javax.servlet.ServletRequest, javax.servlet.ServletResponse
- javax.servlet.http.HttpSession
- java.security.Principal
- @RequestBody
- @RequestHeader
- More: https://docs.spring.io/spring-framework/docs/current/reference/html/web.html#mvc-ann-methods
What are some of the valid return types of a controller method?
- @ResponseBody
- String (return name of view, handled by ViewResolver)
- View
- Map
- ModelAndView
- HttpEntity (construct full http response with headers and body)
- ResponseEntity (also add http error code)
- HttpHeaders (construct response with only headers)
- void
-----------------------------------------------------------------------------------------------------
Questions from EDU-1202 exam (2021)
Does / can Spring MVC use REST ?
- The way the HTTP response body is created is the only difference between a traditional Spring MVC controller and the RESTful controller. The traditional MVC controller is dependent on the View technology, but the RESTful controller returns the object as JSON inside the HTTP response
How do Controllers work under the hood?
- Controllers are based on Spring MVC which uses DispatcherServlet which in turn uses Servlet API to delegate requests to controllers.
Δεν υπάρχουν σχόλια:
Δημοσίευση σχολίου
What may be missing, or could get better?