Spring MVC Flashcards
Explain the MVC architecture and how HTTP requests are processed in the architecture
Request driven structure, designed around a central servlet
Dispatches requests to controllers
Offers functionality to support the web application
Models: represent the data
View: user interface, displays model data
Controller: handles requests between model
What is the role of the DispatcherServlet? What about the ViewResolver?
DispatcherServlet routes requests to configurable handlers, view resolutions and provides support for uploading files.
A servlet that inherits functionality from HttpServlet base class
Declared in the web.xml file
ViewResolver enable you to render models in the browser without tying the implementation to a specific view technology.
The ViewResolver maps view names to actual views.
How would you declare which HTTP requests you’d like a controller to process?
By using the @GetMapping, @PostMapping, @DeleteMapping, and @PutMapping
What is the difference between @RequestMapping and @GetMapping?
The @RequestMapping annotation is used to map a URL to either an entire class or a particular handler method. @GetMapping we can apply only on method level and @RequestMapping annotation we can apply on class level and as well as on method level
How do you declare the data format your controller expects from requests or will create in responses?
@RequestMapping(value = “/hello”, method = RequestMethod.GET)
What annotation would you use to bypass the ViewResolver?
@ResponseBody
How would you extract query and path parameters from a request URL in your controller?
@RequestParam: maps the query parameter to the corresponding method parameter
@PathVariable: Gives a variable passed in the URL.
What concerns is the controller layer supposed to handle vs the service layer?
The controller layer handles the HTTP requests and responses. While the service layer facilitates communication between the controller and repository layer.
which
How would you specify HTTP status codes to return from your controller?
Using ResponseEntity(HttpStatus.[status])
How do you handle exceptions thrown in your code from your controller? What happens if you don’t set up any exception handling?
The @ExceptionHandler is an annotation used to handle specific exceptions and sending the custom responses to the client.
What is the difference between @Controller and @RestController?
@Controller: create a map of model object and find view
@RestController: simply returns the object and object data that is written into HTTP responses
What is a Spring MVC?
A Spring MVC is a Java framework which is used to build web applications. It follows the Model-View-Controller design pattern. It implements all the basic features of a core spring framework like Inversion of Control, Dependency Injection.