寫在前面
本來,是不想寫這篇博客的,一是因為懶,二是因為之前學習過SPRing MVC,而Spring Boot中的大部分常用注解其實都是Spring MVC中的注解。不過,為了以后方便自己查閱和養成寫博客的習慣,我覺得我還是記錄下來吧。
@Controller
主要處理HTTP請求,Spring會將接收到的HTTP請求交給被@Controller所標記的類。現在強調前后臺分離,所以,該注解現在主要與@ResponseBody配合使用來返回json數據。
import org.springframework.stereotype.Controller; @Controller public class HelloController { }
@ResponseBody
作用: 該注解用于將Controller的方法返回的對象,通過適當的HttpMessageConverter轉換為指定格式后,寫入到Response對象的body數據區。 使用時機: 返回的數據不是html標簽的頁面,而是其他某種格式的數據時(如json、xml等)使用;
一般與@Controller配合使用來返回json數據。
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; @Controller public class HelloController { @Autowired //自動注入 private Person person; @ResponseBody @RequestMapping(value = {"/hello", "/hi"}, method = RequestMethod.GET) public Person hello() { return person; } }
@RestController
該注解是Spring4之后新加的注解,等同于@Controller和@ResponseBody的組合。
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloController { @Autowired private Person person; @RequestMapping(value = {"/hello", "/hi"}, method = RequestMethod.GET) public Person hello() { return person; } }
URL映射
每個Controller類,接收到HTTP請求,會通過請求的URL和請求方法(GET,POST…)來映射使用哪個控制器方法來執行請求。這種映射主要通過@RequestMapping注解來實現。
單個URL映射
一個控制器方法對應一個URL,注解@RequestMapping中,value的值即為所映射的URL。
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloController { /** * 啟動應用,瀏覽器打開http://localhost:8080/hello,會調用該方法,打印:Hello, Spring Boot. * @return */ @RequestMapping(value = "/hello", method = RequestMethod.GET) public String helloGet() { return "Hello, Spring Boot"; } }不同方法的映射
同一個URL,請求方法不同,也能對應不同的控制器方法。
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloController { /** * 啟動應用,瀏覽器打開http://localhost:8080/hello,會調用該方法,打印:Hello, Spring Boot.Request:GET. * @return */ @RequestMapping(value = "/hello", method = RequestMethod.GET) public String helloGet() { return "Hello, Spring Boot.Request:GET"; } /** * 啟動應用后,通過模擬Http軟件,以post方式請求http://localhost:8080/hello,會調用該方法,打印:Hello, Spring Boot.Request:POST. * HTTP模擬軟件推薦postman。多平臺支持。 * @return */ @RequestMapping(value = "/hello", method = RequestMethod.POST) public String helloPost() { return "Hello, Spring Boot.Request:POST"; } }映射的簡寫
針對不同的請求方法,Spring都提供了它的簡寫方式,如@GetMapping、@PostMapping。下面的代碼與上面的代碼實現的效果相同。
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloController { @GetMapping("/hello") //等同于@RequestMapping(value = "/hello", method = RequestMethod.GET) public String helloGet() { return "Hello, Spring Boot.Request.GET"; } @PostMapping("hello") //等同于@RequestMapping(value = "/hello", method = RequestMethod.POST) public String helloPost() { return "Hello, Spring Boot.Request:POST"; } }多個URL映射
一個控制器方法也可以對應多個URL,即value的值可以對應一個URL的集合。
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloController { /** * 此時value對應兩個URL,訪問/hello和/hi是一樣的效果 * @return */ @RequestMapping(value = {"/hello", "/hi"}, method = RequestMethod.GET) public String helloGet() { return "Hello, Spring Boot."; } }類級URL映射
@RequestMapping不只可以在方法上使用,也可以在一個控制器類上使用。在一個控制器類上使用該注解,那么類里的其它注解的URL,需要與該注解的URL相加進行訪問才可以。代碼如下:
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/howieli") //類級URL映射 public class HelloController { /** * 此時訪問該方法需要訪問/howieli/hello或者/howieli/hi * @return */ @RequestMapping(value = {"/hello", "/hi"}, method = RequestMethod.GET) public String helloGet() { return "Hello, Spring Boot."; } }
URL數據獲取
在實際開發中,參數肯定是不可或缺的一部分,那么,在控制器中應該怎么獲取呢?我們需要知道,在實際開發中,傳遞的數據主要是分為兩種的,第一種的直接通過URL傳遞,比如:http://localhost:8080/say/3,其中3為所傳遞數據。還有一種是通過參數傳遞,比如GET的傳參方式:http://localhost:8080/say?id=3。 針對第一種情況,Spring提供了@PathVariable注解,針對第二種情況,Spring也提供了@RequestParam注解。
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloController { /** * 在URL映射的注解中,我們可以看到URL中被大括號括起來的id,這就代表我們要傳遞的數據 * 在helloGet方法傳入參數時使用@PathVariable注解,表示將URL中的id所代表的數據作為參數傳入方法中 * name這個參數,是通過參數的方式傳入,此時可以使用@RequestParam注解,其實在這里注解可以省略不寫,因為我們變量名是一樣的 * 比如我們參數是?name=howieli中的name與方法參數name是相同的,就可以省略注解。 * @param id * @return */ @GetMapping(value = "/say/{id}") public String helloGet(@PathVariable("id") int id, @RequestParam("name") String name) { return "id: " + id + ",name:" + name; } } 啟動應用,訪問http://localhost:8080/say/5?name=howieli,即可打印id: 5,name:howieli。 這個部分寫的有點亂。
結語
這只是一些比較常用的注解,之后碰到其它重要注解會慢慢補充。 個人博客:https://www.howieli.cn 和個人CSDN博客: http://blog.csdn.net/howieli_1995。