上一篇文章我們已經學會了如何通過IDEA快速建立一個Spring Boot項目,還介紹了Spring Boot項目的結構,介紹了項目配置文件pom.xml的組成部分,并且撰寫了我們Spring Boot的第一個接口。接下來將會將會介紹使用Spring Boot開發Web應用的相關內容,其主要包括使用spring-boot-starter-web組件來實現Web應用開發、URL地址映射、參數傳遞、數據校驗規、統一數據返回和統一異常處理等等。
Web基礎
Spring Boot將傳統Web開發的mvc、json、validation、tomcat等框架整合,提供了spring-boot-starter-web組件,簡化了Web應用配置和開發的難度,將開發者從繁雜的配置項中拯救出來,專注于業務邏輯的開發。
正如上一篇文章所提到的,我們只需要在pom.xml文件中的dependencies中添加以下代碼就可以引入spring-boot-starter-web。其中的webmvc是Web開發的基礎框架,json是JSON數據解析組建,tomcat為自帶的容器依賴。
<dependency>
<groupId>org.springframework.boot<span class="hljs-name"groupId>
<artifactId>spring-boot-starter-web<span class="hljs-name"artifactId>
<span class="hljs-name"dependency>
@Controller和@RestController
Spring Boot提供了@Controller和@RestController兩種注解來標識此類負責接收和處理HTTP請求,如果請求的是頁面和數據,使用@Controller注解即可,如何只請求數據,則可以使用哦@RestController注解。
@Controller
@Controller主要主要用于頁面和數據的返回,如果在@Controller類中只返回數據到前臺頁面,則需要使用@ResponseBody注解,否則會報錯,其代碼如下:
@Controller
public class HelloController {
@RequestMapping("/hello")
@ResponseBody
public String hello() {
return "Hello @Spring Boot!!!";
}
}
@RestController
@RestController注解用于實現數據請求的處理,默認情況下@RestController注解會將返回的對象數據轉換為JSON格式,其代碼如下:
@RestController
@RequestMapping("/user")
public class UserController {
@RequestMapping("/getUser")
@ResponseBody
public User getUser() {
User u = new User();
u.setName("QStack");
u.setAge(20);
u.setPassword("123456");
return u;
}
}
在上述的例子中,定義/user/getUser接口返回JSON格式的User數據,近幾年前端框架越來越強大,前后端分離的RESTful架構成為主流,Spring Boot對RESTful也做了非常完善的支持,使用也特別簡單,使用@RestController注解自動返回JSON格式的數據,與此同時可以使用@GetMapping和@PostMapping等注解實現映射RESTful接口。
@ResponseBody
@ResponseBody注解主要用于定義數據的返回格式,作用在方法上,默認使用Json序列化成JSON字符串后返回給客戶端,如果是字符串則直接返回。在@Controller中有時需要返回數據體,則需要在方法上使用@Responsebody。
@RequestMapping與URL映射
注解@RequestMapping注解主要負責URL的路由映射,它可以添加在Controller類或具體的方法上,如果添加在Controller類上,則這個Controller中所有的路由映射都會加上此映射規則,如果添加在方法上則只對當前方法生效。@RequestMapping注解包含很多屬性參數來定義HTTP,具體屬性參數如下所示,與此相應的Spring Boot支持URL路徑匹配、HTTP Method匹配、params和header匹配等URL映射。
- value:請求URL的路徑,支持URL模版、正則表達式
- method:HTTP請求的方法
- consumes:允許的媒體類型,如consumes=“application/json”為HTTP的Content-Type
- produces:相應的媒體類型,如produces=“application/json”為HTTP的Accept字段
- params:請求參數
- headers:請求頭參數
URL路徑匹配
精確匹配
@RequestMapping的value屬性用于匹配URL映射,value支持簡單表達式。示例代碼如下,其中@PathVariable注解作用在方法參數中,用于表示參數的值來自于URL路徑。
@RequestMapping("/getUserById/{id}")
public String getUserById(@PathVariable("id") Long id) {
return "getUserById" + id;
}
如果URL中的參數名稱與方法中的參數名一致,則可以簡化為如下
@RequestMapping("/getUserById/{id}")
public String getUserById(@PathVariable Long id) {
return "getUserById" + id;
}
通配符匹配
@RequsetMapping支持使用通配符匹配URL,用于統一映射某些URL規則類似的請求,示例的代碼如下
@RequestMapping("/getJson/*.json")
public String getJson() {
return "get json data";
}
在上例中,無論請求/getJson/a.json還是請求/getJson/b.json都會匹配到getJson方法。
Method匹配
@RequestMapping注解提供了method參數指定請求的Mathod類型,包括RequestMethod.GET 、RequestMethod.POST、RequestMethod.DELETE、RequestMethod.PUT等值,分別對應HTTP請求的Method,以下是以GET方法為例說明。
@RequestMapping(value = "/getData", method = RequestMethod.GET)
public String getData() {
return "RequestMethod GET";
}
consumes和produces匹配
@RequestMapping注解提供了consumes和produces參數用于驗證HTTP請求的內容類型和返回類型。
- consumes表示請求的HTTP頭的Content-Type媒體類型與consumes的值匹配才可以調用方法。
- produces表示HTTP請求中Accept字段匹配成功才可以調用。下面的例子演示了consumes參數的用法。
@RequestMapping(value = "/content", method = RequestMethod.POST, consumes = "application/json")
public String Consumes(@RequestBody Map param){
return "Consumes POST Content-Type=application/json";
}
params和header匹配
@RequestMapping注解還提供header參數和params參數映射URL請求的能力,Spring Boot可以從請求參數或HTTP頭中提取參數,通過判斷參數如params=“action=save”是否通過來實現映射,代碼如下
@RequestMapping(value = "/testParam", params = "action=save")
public String testParam(@RequestBody Map param) {
return "param test";
}
@RequestMapping(value = "/testHead", headers = {"Host=localhost:8080"})
public String testHead() {
return "header test";
}
-
Web
+關注
關注
2文章
1269瀏覽量
69730 -
URL
+關注
關注
0文章
139瀏覽量
15477 -
spring
+關注
關注
0文章
340瀏覽量
14388 -
idea
+關注
關注
1文章
68瀏覽量
4317
發布評論請先 登錄
相關推薦
啟動Spring Boot項目應用的三種方法
談一談Spring Boot嵌入式Web容器
Spring Boot定時任務的重寫方法
Spring Boot從零入門1 詳述
「Spring認證」什么是Spring GraphQL?

學習Spring Boot 嵌入式服務器

Spring Boot特有的實踐
Spring Boot配置加載相關知識
Spring Boot Actuator快速入門
Spring Boot啟動 Eureka流程

Spring Boot的啟動原理

評論