Spring 中注解 RestController 与 Controller 的区别

文章目录

    RestController

    A convenience annotation that is itself annotated with @Controller and @ResponseBody.

    即,RestController 注解是 Controller 和 ResponseBody 的组合写法。

    所以,下面两种写法是等同的。

    @Controller
    @ResponseBody
    public class MyController1 {}
    
    @RestController
    public class MyController2 {}
    

    ResponseBody

    ResponseBody 注解可以将 spring controller 的返回对象,自动转换成 json 或者是 xml 格式;而不会走 view 模板。

    RestController 中的 Rest 是什么的缩写

    Rest 大概是 Restful 的缩写吧。

    毕竟 RestController 的使用场景更接近于 Restful API。