DEV Community

Willian Ferreira Moya
Willian Ferreira Moya

Posted on • Originally published at springmasteryhub.com

How to Use @Controller in Spring

What is?

This is a class-level annotation that tells Spring that your class is a controller. A controller is an entry point for a web application. This allows you to define a path to communicate with your backend using REST methods or by serving and responding html forms.

This annotation is more general and allows your controller to serve REST endpoints and serve webpage content, which is very common in the MVC pattern.

Why is this important

This annotation is what allows your front end to communicate with the backend, allowing you to define routes, pages, dynamic content, etc. If you are working with web applications, this annotation is one of the most important ones.

How to use @Controller

Defining a controller class

import org.springframework.stereotype.Controller;

@Controller
public class MyController {
    // controller methods go here
}

Enter fullscreen mode Exit fullscreen mode

Defining an endpoint and its respective HTTP method

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class MyController {
    @GetMapping("/greeting")
    public String greeting() {
        return "greeting";  // resolves to a view named "greeting"
    }
}

Enter fullscreen mode Exit fullscreen mode

Add a page in the location src/main/resources/templates/greeting.html with the same name as the string returned by the controller to return an HTML page from the server

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8" />
    <title>Greeting</title>
</head>
<body>
<h1>Hello!</h1>
</body>
</html>

Enter fullscreen mode Exit fullscreen mode

Testing

Testing @Controller without REST

Using curl to call our controller

curl -i http://localhost:8080/greeting
Enter fullscreen mode Exit fullscreen mode

The result should look like this*:*

HTTP/1.1 200 
Content-Type: text/html;charset=UTF-8
Content-Language: pt-BR
Transfer-Encoding: chunked

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8" />
    <title>Greeting</title>
</head>
<body>
<h1>Hello!</h1>
</body>
</html>

Enter fullscreen mode Exit fullscreen mode

Creating a REST endpoint with @ResponseBody

import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class RestControllerExample {
    @GetMapping("/api/message")
    @ResponseBody
    public ResponseEntity<String> getMessage() {
        return ResponseEntity.ok("Hello, World!");
    }
}

Enter fullscreen mode Exit fullscreen mode

Testing @Controller with @ResponseBody

When calling with curl :


curl -i http://localhost:8080/api/message
Enter fullscreen mode Exit fullscreen mode

It should return like this:

HTTP/1.1 200 
Content-Type: text/plain;charset=UTF-8
Content-Length: 13

Hello, World!
Enter fullscreen mode Exit fullscreen mode

Conclusion

This is one of the most important and common annotations when it comes to creating web applications with Spring. The usage is straightforward, making it easier to add new controllers and endpoints to your application.

Willian Moya (@WillianFMoya) / X

Willian Ferreira Moya | LinkedIn

Top comments (0)