Spring MVC Flashcards

1
Q

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

A

The Model-View-Controller (MVC) framework is an architectural pattern that separates an application into three main logical components Model, View, and Controller

A Controller handles all the HTTP requests and responses

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

The job of the DispatcherServlet is to take an incoming URI and find the right combination of handlers (generally methods on Controller classes) and views (generally JSPs) that combine to form the page or resource that’s supposed to be found at that location.

The ViewResolver provides a mapping between view names and actual views

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

List some stereotype annotations. What are the differences between these?

A

@Component: Indicates class is a parent

@Service: handle business logic in conjunction with a persistance layer

@Repository: handle CRUD operations against a DB

@Controller: handles web requests

@Configuration

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

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

A

By adding a certain path to the @RequestMapping in a controller method

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

What is the difference between @RequestMapping and @GetMapping?

A

@GetMapping is just a more specific @RequestMapping. Meaning you could also use @RequestMapping(method = RequestMethod.Get)

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

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

A

HttpHeaders responseHeaders = new HttpHeaders(); responseHeaders.add(“Content-Type”, “text/html; charset=utf-8”);

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

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

A

You would use @QueryParam in the method

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

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

A

A controller handles requests and then tells the service what it needs. A controller is like a manager and the service is like an employee.

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

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

A

@ResponseStatus above a method and it will only work when the method completes successfully otherwise it will send and html error page to the client

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

Use @ExceptionHandler or @ControllerAdvice in conjunction with @ResponseStatus

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

How would you consume an external web service using Spring?

A

Create a REST Template Bean
Use @GetMapping
Use getForObject

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

What are the advantages of using RestTemplate?

A

It simplifies the interaction with HTTP servers and enforces RESTful systems.

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