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

學(xué)習(xí)Spring Boot 嵌入式服務(wù)器

Spring Boot特有的實(shí)踐
簡述Spring Boot數(shù)據(jù)校驗(yàn)
Spring Boot配置加載相關(guān)知識
Spring Boot Actuator快速入門
Spring Boot啟動(dòng) Eureka流程

Spring Boot的啟動(dòng)原理

評論