天天看點

@RestController和@Controller的差別和使用技巧(Controller層注解詳解、@RestController、@Controller、@RequestMapping等)@RestController和@Controller的差別和使用技巧(Controller層注解詳解、@RestController、@Controller、@RequestMapping等)

@RestController和@Controller的差別和使用技巧(Controller層注解詳解、@RestController、@Controller、@RequestMapping等)

  • @RestController注解相當于@ResponseBody + @Controller合在一起的作用。
  • @controller傳回(return)的是視圖的名字,而@RestController傳回的則是return後面的資訊(接下來舉幾個例子)

@Controller可以實作傳回資訊(String)、傳回視圖、傳回JSON等,

@RestController也可以實作傳回資訊(String)、傳回視圖、傳回JSON等

一、@Controller的使用

使用@Controller注解傳回視圖

@Controller

@RestController和@Controller的差別和使用技巧(Controller層注解詳解、@RestController、@Controller、@RequestMapping等)@RestController和@Controller的差別和使用技巧(Controller層注解詳解、@RestController、@Controller、@RequestMapping等)
@RestController和@Controller的差別和使用技巧(Controller層注解詳解、@RestController、@Controller、@RequestMapping等)@RestController和@Controller的差別和使用技巧(Controller層注解詳解、@RestController、@Controller、@RequestMapping等)

使用@Controller傳回資訊(String)

@RestController和@Controller的差別和使用技巧(Controller層注解詳解、@RestController、@Controller、@RequestMapping等)@RestController和@Controller的差別和使用技巧(Controller層注解詳解、@RestController、@Controller、@RequestMapping等)
@RestController和@Controller的差別和使用技巧(Controller層注解詳解、@RestController、@Controller、@RequestMapping等)@RestController和@Controller的差別和使用技巧(Controller層注解詳解、@RestController、@Controller、@RequestMapping等)

使用@Controller傳回JSON資料

@RestController和@Controller的差別和使用技巧(Controller層注解詳解、@RestController、@Controller、@RequestMapping等)@RestController和@Controller的差別和使用技巧(Controller層注解詳解、@RestController、@Controller、@RequestMapping等)
@RestController和@Controller的差別和使用技巧(Controller層注解詳解、@RestController、@Controller、@RequestMapping等)@RestController和@Controller的差別和使用技巧(Controller層注解詳解、@RestController、@Controller、@RequestMapping等)

二、@RestController的使用

使用@RestController傳回資訊(String)

@RestController和@Controller的差別和使用技巧(Controller層注解詳解、@RestController、@Controller、@RequestMapping等)@RestController和@Controller的差別和使用技巧(Controller層注解詳解、@RestController、@Controller、@RequestMapping等)
@RestController和@Controller的差別和使用技巧(Controller層注解詳解、@RestController、@Controller、@RequestMapping等)@RestController和@Controller的差別和使用技巧(Controller層注解詳解、@RestController、@Controller、@RequestMapping等)

使用@RestController傳回JSON資料

@RestController和@Controller的差別和使用技巧(Controller層注解詳解、@RestController、@Controller、@RequestMapping等)@RestController和@Controller的差別和使用技巧(Controller層注解詳解、@RestController、@Controller、@RequestMapping等)
@RestController和@Controller的差別和使用技巧(Controller層注解詳解、@RestController、@Controller、@RequestMapping等)@RestController和@Controller的差別和使用技巧(Controller層注解詳解、@RestController、@Controller、@RequestMapping等)

使用@RestController傳回視圖

@RestController和@Controller的差別和使用技巧(Controller層注解詳解、@RestController、@Controller、@RequestMapping等)@RestController和@Controller的差別和使用技巧(Controller層注解詳解、@RestController、@Controller、@RequestMapping等)
@RestController和@Controller的差別和使用技巧(Controller層注解詳解、@RestController、@Controller、@RequestMapping等)@RestController和@Controller的差別和使用技巧(Controller層注解詳解、@RestController、@Controller、@RequestMapping等)

三、請求的注解(@RequestMapping、@GetMapping、@PostMapping)

首先顧名思義,@RequestMapping可以處理很多中請求類型,具體如下,而@GetMapping隻能接收Get請求,@PostMapping隻能接收Post請求

@RestController和@Controller的差別和使用技巧(Controller層注解詳解、@RestController、@Controller、@RequestMapping等)@RestController和@Controller的差別和使用技巧(Controller層注解詳解、@RestController、@Controller、@RequestMapping等)

@RequestMapping限制成get類型的情況

@RestController和@Controller的差別和使用技巧(Controller層注解詳解、@RestController、@Controller、@RequestMapping等)@RestController和@Controller的差別和使用技巧(Controller層注解詳解、@RestController、@Controller、@RequestMapping等)

@RequestMapping限制成post類型的情況

@RestController和@Controller的差別和使用技巧(Controller層注解詳解、@RestController、@Controller、@RequestMapping等)@RestController和@Controller的差別和使用技巧(Controller層注解詳解、@RestController、@Controller、@RequestMapping等)

拓展

@RestController和@Controller的差別和使用技巧(Controller層注解詳解、@RestController、@Controller、@RequestMapping等)@RestController和@Controller的差別和使用技巧(Controller層注解詳解、@RestController、@Controller、@RequestMapping等)

以上的類型都有對應的Mapping注解,一下不一 一講解

四、參數注解

@RequestParam注解

@RestController和@Controller的差別和使用技巧(Controller層注解詳解、@RestController、@Controller、@RequestMapping等)@RestController和@Controller的差別和使用技巧(Controller層注解詳解、@RestController、@Controller、@RequestMapping等)

注:name屬性和value是完全一樣的,都是對應前端元件的name值

  • required 屬性是指必填(預設true)
  • defaultValue屬性是指該參數的預設值,如果前端未輸入該參數的值,該值将會被 指派 預設值
  • 該注解可以不加,預設以參數名稱尋找對應的前端組價的name屬性名稱
@RestController和@Controller的差別和使用技巧(Controller層注解詳解、@RestController、@Controller、@RequestMapping等)@RestController和@Controller的差別和使用技巧(Controller層注解詳解、@RestController、@Controller、@RequestMapping等)
@RestController和@Controller的差別和使用技巧(Controller層注解詳解、@RestController、@Controller、@RequestMapping等)@RestController和@Controller的差別和使用技巧(Controller層注解詳解、@RestController、@Controller、@RequestMapping等)

五、@ModelAttribute

  • 該注解注釋的方法會在本類的任何請求執行前執行
  • 注意是僅僅本類中的請求
  • 在請求方法執行前執行(類似于攔截器)
    @RestController和@Controller的差別和使用技巧(Controller層注解詳解、@RestController、@Controller、@RequestMapping等)@RestController和@Controller的差別和使用技巧(Controller層注解詳解、@RestController、@Controller、@RequestMapping等)