Spring中@Controller和@RestController之間的區別


web開發

spring boot web開發非常的簡單,其中包括常用的json輸出、filters、property、log等

json 接口開發

在以前的spring 開發的時候需要我們提供json接口的時候需要做那些配置呢

  1. 添加 jackjson 等相關jar包
  2. 配置spring controller掃描
  3. 對接的方法添加@ResponseBody

就這樣我們會經常由於配置錯誤,導致406錯誤等等,spring boot如何做呢,只需要類添加 @RestController 即可,默認類中的方法都會以json的格式返回

 
 
1. Controller, RestController的共同點

     都是用來表示Spring某個類的是否可以接收HTTP請求

2.  Controller, RestController的不同點

     @Controller標識一個Spring類是Spring MVC controller處理器

     @RestController:  a convenience annotation that does nothing more than adding the@Controller and @ResponseBody annotations。  @RestController是@Controller和@ResponseBody的結合體,兩個標注合並起來的作用。

示例如下:

  1. @Controller  
  2. @ResponseBody  
  3. public class MyController { }  
  4.   
  5. @RestController  
  6. public class MyRestController { } 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM