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。

    关于作者 🌱

    我是来自山东烟台的一名开发者,有感兴趣的话题,或者软件开发需求,欢迎加微信 zhongwei 聊聊,或者关注我的个人公众号“大象工具”, 查看更多联系方式