The Spring @Controller and @RestController Annotations – Spring的@Controller和@RestController注解

最后修改: 2018年 4月 2日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

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

在这个简短的教程中,我们将讨论Spring MVC中@Controller@RestController注解的区别。

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

我们可以将第一个注解用于传统的Spring控制器,而且它已经成为框架的一部分很长时间了。

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.

Spring 4.0引入了@RestController注解,以简化RESTful Web服务的创建。这是一个方便的注解,它结合了@Controller@ResponseBody,这样就不需要用@ResponseBody注解来注释控制器类的每个请求处理方法了。

2. Spring MVC @Controller

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.

我们可以用@Controller注解来注释经典控制器。这只是@Component类的一个特殊化,它允许我们通过classpath扫描自动检测实现类。

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

我们通常将 @Controller@RequestMapping注解结合使用,用于请求处理方法。

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

让我们看看Spring MVC控制器的一个快速例子。

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

我们用@ResponseBody注释了请求处理方法。这个注解使得返回对象能够自动序列化到HttpResponse中。

3. Spring MVC @RestController

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是控制器的一个专门版本。它包括@Controller@ResponseBody注解,因此,简化了控制器的实现。

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

控制器被注释为@RestController注释;因此,@ResponseBody不是必需的。

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

控制器类的每个请求处理方法都自动将返回对象序列化为HttpResponse

4. Conclusion

4.总结

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

在这篇文章中,我们研究了Spring框架中可用的经典和专门的REST控制器。

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.

示例的完整源代码可在GitHub项目中找到。这是一个Maven项目,因此可以按原样导入和使用。