Зміст курсу
Spring Boot Backend
Spring Boot Backend
Spring MVC
Why You Need Spring MVC
Spring MVC helps organize a web application by following the principle of separation of concerns, making it easier to develop, test, and maintain code. It provides a framework for creating web pages and handling user requests, as well as for integrating with business logic and databases.
We can integrate it into our project using the pom.xml
file, where we will add the dependency within the <dependencies></dependencies>
tags.
With it, we can receive requests from the client and process them as needed, and then return a response in the form of HTML or JSON.
Note
We have already included this dependency in the previous chapter, so it is likely already added to your project!
Key Annotations and Their Role
@Controller
Main
@Controller public class HomeController { @RequestMapping(value = "/", method = RequestMethod.GET) public String home() { // code } }
In this example, the HomeController
class handles requests to the root URL /
. This means that if we navigate to example.com/
, a request will be sent to the home()
method.
The @RequestMapping
annotation connects HTTP requests to controller methods. You can use it to specify URL patterns, HTTP methods, and other request parameters. Its attributes allow you to define the URL and the type of request (e.g., GET, POST, DELETE, etc.).
There are annotations that provide more precise mapping for the type of request method, such as @GetMapping
, @PostMapping
, @DeleteMapping
, and so on. In their attributes, you simply specify the URL that they can be invoked with.
Main
@Controller public class HomeController { @GetMapping("/home") public String home() { // code } }
In this example, if we access the URL /home
, we will be directed to the home()
method ONLY if it was a GET request. If a different type of request method is used, the server will respond with a 404 Not Found
error for that URL.
@RequestParam
What are URL request parameters? These are the parameters that appear after the question mark in the URL. For example, if we have the address -> example.com/home?name=Alex
, we can directly retrieve the parameter (name) in the welcomeUser()
method.
Main
@Controller public class WelcomeController { @GetMapping("/home") public String welcomeUser(@RequestParam(name = "name") String name) { // code } }
Here, the parameter name
is extracted from the URL (e.g., /home?name=John
) and passed to the welcomeUser()
method.
Note
You can also extract more than one parameter, not just a single one. All parameters in the URL should be separated by the
&
symbol. For example:example.com/home?name=Alex&age=20
.
@PathVariable
Main
@Controller public class UserController { @GetMapping("/user/{id}") public String getUser(@PathVariable("id") Long userId) { // code } }
In this example, id
is extracted from the URL (e.g., /user/123
) and passed to the getUser()
method, where it is assigned to the userId
parameter.
@ResponseBody
Main
@Controller public class DataController { @GetMapping("/data") @ResponseBody public User getUserData() { return new User("John", "Doe"); } }
Here, the User
object will be automatically converted to JSON and sent to the client as the response.
Note
If you want all methods to return JSON, you can use
@RestController
instead of@Controller
on the class level. This way, you won't need to add the@ResponseBody
annotation to each method.
These annotations help developers easily configure HTTP request handling, bind data to objects, and manage responses, enhancing code readability and maintainability.
Summary
Spring MVC simplifies web application development by separating request handling, application logic, and view rendering. Controllers, marked with the @Controller
or @RestController
annotation, handle requests and return data for display.
Все було зрозуміло?