Spring MVC Flashcards

1
Q

Explain the MVC architecture and how HTTP requests are processed in the architecture

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is the role of the DispatcherServlet? What about the ViewResolver?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How would you declare which HTTP requests you’d like a controller to process?

A

By using the @GetMapping, @PostMapping, @DeleteMapping, and @PutMapping

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is the difference between @RequestMapping and @GetMapping?

A
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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How do you declare the data format your controller expects from requests or will create in responses?

A

@RequestMapping(value = “/hello”, method = RequestMethod.GET)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What annotation would you use to bypass the ViewResolver?

A

@ResponseBody

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How would you extract query and path parameters from a request URL in your controller?

A

@RequestParam: maps the query parameter to the corresponding method parameter
@PathVariable: Gives a variable passed in the URL.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What concerns is the controller layer supposed to handle vs the service layer?

A

The controller layer handles the HTTP requests and responses. While the service layer facilitates communication between the controller and repository layer.
which

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How would you specify HTTP status codes to return from your controller?

A

Using ResponseEntity(HttpStatus.[status])

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How do you handle exceptions thrown in your code from your controller? What happens if you don’t set up any exception handling?

A

The @ExceptionHandler is an annotation used to handle specific exceptions and sending the custom responses to the client.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is the difference between @Controller and @RestController?

A

@Controller: create a map of model object and find view

@RestController: simply returns the object and object data that is written into HTTP responses

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is a Spring MVC?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly