1、在進行控制器編寫的時候,也會有以下兩種運行模式。
第一種、控制器跳轉模式:可以使用@Controller注解定義,如果要實現Restful顯示,也可以聯合@ResponseBody注解一起使用。
第二種、Restful顯示:可以使用@RestController注解,里面所有路徑訪問的信息都以Restful形式展示。在控制器里面一旦使用了@RestController注解,則意味着所有方法都將以Restful風格展示。
2、由於SpringBoot支持Restful風格處理,所以參數的接收可以采用路徑參數的形式完成,但是需要在控制器方法的參數聲明上使用@PathVariable注解與訪問路徑的參數進行關聯。
1 package org.springboot.tentent.controller; 2 3 import org.springframework.web.bind.annotation.PathVariable; 4 import org.springframework.web.bind.annotation.RequestMapping; 5 import org.springframework.web.bind.annotation.RestController; 6 7 @RestController 8 public class SampleController { 9 10 @RequestMapping(value = "hello") 11 public String hello() { 12 return "hello springboot!!!"; 13 } 14 15 @RequestMapping(value = "/hello/{msg}") 16 public String hello(@PathVariable(value = "msg") String msg) { 17 return "hello springboot!!!" + msg; 18 } 19 20 }
訪問方式,如下所示:
注意:在Restful架構中請求路徑受多類語法支持,開發者可以結合HTTP請求模式(GET、POST、PUT、DELETE等)與路徑,實現多種組合,以處理不同類型的用戶請求。參數的傳遞模式可以由開發者自行定義。