REST Top

Get started with Spring 5 and Spring Boot 2, through the reference Learn Spring course:

>> CHECK OUT THE COURSE

1. Overview

In this brief tutorial, we’ll discuss the difference between @Controller and @RestController annotations in Spring MVC.

We can use the first annotation for traditional Spring controllers, and it has been part of the framework for a very long time.

Spring 4.0 introduced the @RestController annotation in order to simplify the creation of RESTful web services. It's a convenient annotation that combines @Controller and @ResponseBody, which eliminates the need to annotate every request handling method of the controller class with the @ResponseBody annotation.

Further reading:

Spring RequestMapping

Spring @RequestMapping - Basic Example, @RequestParam, @PathVariable, Header mapping

Spring @RequestParam Annotation

A detailed guide to Spring's @RequestParam annotation

2. Spring MVC @Controller

We can annotate classic controllers with the @Controller annotation. This is simply a specialization of the @Component class, which allows us to auto-detect implementation classes through the classpath scanning.

We typically use @Controller in combination with a @RequestMapping annotation for request handling methods.

Let's see a quick example of the Spring MVC controller:

@Controller
@RequestMapping("books")
public class SimpleBookController {

    @GetMapping("/{id}", produces = "application/json")
    public @ResponseBody Book getBook(@PathVariable int id) {
        return findBookById(id);
    }

    private Book findBookById(int id) {
        // ...
    }
}

We annotated the request handling method with @ResponseBody. This annotation enables automatic serialization of the return object into the HttpResponse.

3. Spring MVC @RestController

@RestController is a specialized version of the controller. It includes the @Controller and @ResponseBody annotations, and as a result, simplifies the controller implementation:

@RestController
@RequestMapping("books-rest")
public class SimpleBookRestController {
    
    @GetMapping("/{id}", produces = "application/json")
    public Book getBook(@PathVariable int id) {
        return findBookById(id);
    }

    private Book findBookById(int id) {
        // ...
    }
}

The controller is annotated with the @RestController annotation; therefore, the @ResponseBody isn't required.

Every request handling method of the controller class automatically serializes return objects into HttpResponse.

4. Conclusion

In this article, we examined the classic and specialized REST controllers available in the Spring Framework.

The complete source code for the examples is available in the GitHub project. This is a Maven project, so it can be imported and used as is.

REST bottom

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course :

>> CHECK OUT THE COURSE
Comments are closed on this article!