SpringBoot 中常用註解@Controller/@RestController/@RequestMapping介紹
@Controller
//@ResponseBody
public class HelloController {
@RequestMapping(value="/hello",method= RequestMethod.GET)
public String sayHello(){
return "hello";
}
}
如果直接使用@Controller這個註解,當執行該SpringBoot專案後,在瀏覽器中輸入:local:8080/hello,會得到如下錯誤提示:
出現這種情況的原因在於:沒有使用模版。即@Controller 用來響應頁面,@Controller必須配合模版來使用。spring-boot 支援多種模版引擎包括: 1,FreeMarker 2,Groovy 3,Thymeleaf (Spring 官網使用這個) 4,Velocity 5,JSP (貌似Spring Boot官方不推薦,STS建立的專案會在src/main/resources 下有個templates 目錄,這裡就是讓我們放模版檔案的,然後並沒有生成諸如 SpringMVC 中的webapp目錄)
本文以Thymeleaf為例介紹使用模版,具體步驟如下:
第一步:在pom.xml檔案中新增如下模組依賴:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
第二步:修改控制器程式碼,具體為:
/** * Created by wuranghao on 2017/4/7. */ @Controller public class HelloController { @RequestMapping(value="/hello",method= RequestMethod.GET) public String sayHello(){ return "hello"; } }
第三步:在resources目錄的templates目錄下新增一個hello.html檔案,具體工程目錄結構如下:
其中,hello.html檔案中的內容為:
<h1>wojiushimogui</h1>
這樣,再次執行此專案之後,在瀏覽器中輸入:localhost:8080/hello
就可以看到hello.html中所呈現的內容了。
因此,我們就直接使用@RestController註解來處理http請求來,這樣簡單的多。
@RestController Spring4之後新加入的註解,原來返回json需要@ResponseBody和@Controller配合。
即@RestController是@ResponseBody和@Controller的組合註解。
@RestController
public class HelloController {
@RequestMapping(value="/hello",method= RequestMethod.GET)
public String sayHello(){
return "hello";
}
}
與下面的程式碼作用一樣
@Controller
@ResponseBody
public class HelloController {
@RequestMapping(value="/hello",method= RequestMethod.GET)
public String sayHello(){
return "hello";
}
}
@RequestMapping 配置url對映 @RequestMapping此註解即可以作用在控制器的某個方法上,也可以作用在此控制器類上。
當控制器在類級別上新增@RequestMapping註解時,這個註解會應用到控制器的所有處理器方法上。處理器方法上的@RequestMapping註解會對類級別上的@RequestMapping的宣告進行補充。
看兩個例子
例子一:@RequestMapping僅作用在處理器方法上
@RestController
public class HelloController {
@RequestMapping(value="/hello",method= RequestMethod.GET)
public String sayHello(){
return "hello";
}
}
以上程式碼sayHello所響應的url=localhost:8080/hello。
例子二:@RequestMapping僅作用在類級別上
/**
* Created by wuranghao on 2017/4/7.
*/
@Controller
@RequestMapping("/hello")
public class HelloController {
@RequestMapping(method= RequestMethod.GET)
public String sayHello(){
return "hello";
}
}
以上程式碼sayHello所響應的url=localhost:8080/hello,效果與例子一一樣,沒有改變任何功能。
例子三:@RequestMapping作用在類級別和處理器方法上
/**
* Created by wuranghao on 2017/4/7.
*/
@RestController
@RequestMapping("/hello")
public class HelloController {
@RequestMapping(value="/sayHello",method= RequestMethod.GET)
public String sayHello(){
return "hello";
}
@RequestMapping(value="/sayHi",method= RequestMethod.GET)
public String sayHi(){
return "hi";
}
}
這樣,以上程式碼中的sayHello所響應的url=localhost:8080/hello/sayHello。
sayHi所響應的url=localhost:8080/hello/sayHi。
從這兩個方法所響應的url可以回過頭來看這兩句話:當控制器在類級別上新增@RequestMapping註解時,這個註解會應用到控制器的所有處理器方法上。處理器方法上的@RequestMapping註解會對類級別上的@RequestMapping的宣告進行補充。
最後說一點的是@RequestMapping中的method引數有很多中選擇,一般使用get/post.
小結 本篇博文就介紹了下@Controller/@RestController/@RequestMappping幾種常用註解,下篇博文將介紹幾種如何處理url中的引數的註解@PathVaribale/@RequestParam/@GetMapping。
其中,各註解的作用為:
@PathVaribale 獲取url中的資料
@RequestParam 獲取請求引數的值
@GetMapping 組合註解
--------------------- 作者:HelloWorld_EE 來源:CSDN 原文:https://blog.csdn.net/u010412719/article/details/69710480