Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Spring MVC | Spring Boot Basics
Spring Boot Backend
course content

Course Content

Spring Boot Backend

Spring Boot Backend

1. Backend Development Basics
2. Spring Boot Basics
3. RESTful API
4. Working with Databases
5. Testing Backend Applications

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

java

Main

copy
12345678
@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.

java

Main

copy
12345678
@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.

java

Main

copy
12345678
@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

java

Main

copy
12345678
@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

java

Main

copy
123456789
@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.

1. What does the `@Controller` annotation do in Spring MVC?
2. Which annotation method is used to handle `GET requests` in Spring MVC?

What does the @Controller annotation do in Spring MVC?

Select the correct answer

Which annotation method is used to handle GET requests in Spring MVC?

Select the correct answer

Everything was clear?

Section 2. Chapter 4
We're sorry to hear that something went wrong. What happened?
some-alt