Home  >  Article  >  Java  >  Spring MVC Restaurant

Spring MVC Restaurant

DDD
DDDOriginal
2024-10-24 04:40:30275browse

Imagine that Spring MVC is not just a framework, but a kind of restaurant. You come to the restaurant (send an HTTP request), and then the magic begins, and this is how it happens in the world of Spring MVC:

You enter a restaurant - DispatcherServlet on guard
Imagine walking into the Spring MVC restaurant. But before you go to the table, you are greeted by the chief doorman - DispatcherServlet. He personifies the front-end officer and manages all orders. Your HTTP request is like your first greeting to the doorman: you want something tasty.

Ресторан Spring MVC

DispatcherServlet does not prepare the food itself, it simply passes the request to the desired “chef”, that is, your controller, who will prepare the dish. But more on that a little later. First, the doorman will check the menu to see which chef can prepare you what you want.

Handler looking for recipes - HandlerMapping
As soon as you say: “I want something tasty!”, the doorman (our valiant DispatcherServlet) does not immediately rush to the kitchen. No, first he opens HandlerMapping, where he has all the possible restaurant dishes and the chef who can prepare them.

You ask for, say, a cake. The doorman looks at his notes and finds that Chef John is making the cakes from the ChefController.

Ресторан Spring MVC

The doorman sends the order to the kitchen - HandlerAdapter
Once the doorman has found a suitable chef, he uses HandlerAdapter, his assistant, who helps package the order and transfer it to the kitchen to the chef. It’s as if the doorman is just whispering: “John, make the client a cake, come quickly!”

The chef prepares the dish – Controller

Ресторан Spring MVC
Now Chef John takes over. He is our Controller who does the cooking. Depending on the client's request, he can make different cakes. For example, if the request was GET, then John can simply show the ready-made cakes (pull them out of the refrigerator). And if the request is POST, then perhaps you asked for a fresh cream cake, and John will start cooking.

Example:

@GetMapping("/cakes")
public List getCakes() {
return List.of("Napoleon", "Tiramisu", "Cheesecake");
}
This chef is not one to hesitate, he immediately gives out a list of cakes!

Dish decoration - ViewResolver
Once the dish is ready, you need to present it beautifully. This is where ViewResolver comes into play. He is like a plate designer: he decides in what form the dish will be served to the client. You don't want to just see a bunch of ingredients, you want a beautifully decorated cake!

ViewResolver can decide that the cake should be served on a golden plate (JSP), or in a box (JSON) if you ordered it to go.

Voila! Dish on your table - HTTP Response
Finally, the dish is ready and served. Your client (browser) receives a response from the restaurant. Depending on your order, this could be an HTML page containing the cakes, or a JSON object if you ordered through a takeaway app.

For example, chef John decided to submit a list of cakes in JSON:

@GetMapping("/cakes")
@ResponseBody
public List getCakes() {
return List.of("Tiramisu", "Napoleon", "Eclair");
}
The client received JSON with cakes, and his life became better!

What if something went wrong? — Exception Handling

Ресторан Spring MVC
As with any restaurant, sometimes things go wrong. For example, suddenly Chef John dropped your cake on the floor! In the Spring MVC world, this is equivalent to an exception (e.g. the request could not be processed).

But don't worry, Spring Restaurant has a special waiter called ExceptionHandler who will quickly respond and bring you an apology or a new order.

@ExceptionHandler(NoCakesAvailableException.class)
public ResponseEntity handleNoCakes(NoCakesAvailableException e){
return new ResponseEntity<>("Sorry, all the cakes are out!", HttpStatus.NOT_FOUND);
}
This way, if suddenly we don’t have any cakes, they will tell you that everything is over and ask for forgiveness.

Conclusion
Now you understand that Spring MVC is like a restaurant with a clearly structured service system. Here the doorman (DispatcherServlet) takes your orders, finds the right chef (Controller) who prepares the dishes (handles requests), and the waiters (HandlerAdapter and ViewResolver) deliver the result beautifully presented. Even if something goes wrong, the ExceptionHandler is always ready to back up.

The above is the detailed content of Spring MVC Restaurant. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn