Τετάρτη 6 Οκτωβρίου 2021

Spring Certification: Spring MVC Basics

What is the @Controller annotation used for?
  • 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)


How can this method return data as JSON?


  • Add @ResponseBody above method
  • Tip: Generally study return types and cases (no "null" return is correct, but Void, Model, View, ModelAndView, HttpResponse, ...)


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












Δεν υπάρχουν σχόλια:

Δημοσίευση σχολίου

What may be missing, or could get better?