SpringBoot中常用注解@Controller/@RestController/@RequestMapping的區別


@Controller 處理http請求

@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 組合注解


免責聲明!

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



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