寫在前面
本來,是不想寫這篇博客的,一是因爲懶,二是因爲之前學習過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。