“怎樣的人生才是沒(méi)有遺憾的人生?我的體會(huì)是:(1)擁有健康;(2)創(chuàng)造“難忘時(shí)刻”;(3)盡力做好自己,不必改變世界;(4)活在當(dāng)下?!?/div>– 《向死而生》李開(kāi)復(fù)Spring Boot 系列文章:《Spring Boot 那些事》
基于上一篇《Springboot 整合 Mybatis 的完整 Web 案例》,這邊我們著重在 控制層 講講。講講如何在 Springboot 實(shí)現(xiàn) Restful 服務(wù),基于 HTTP / JSON 傳輸。
一、運(yùn)行 springboot-restful 工程
git clone 下載工程 springboot-learning-example ,項(xiàng)目地址見(jiàn) GitHub – https://github.com/JeffLi1993/springboot-learning-example。下面開(kāi)始運(yùn)行工程步驟(Quick Start):
1.數(shù)據(jù)庫(kù)準(zhǔn)備
a.創(chuàng)建數(shù)據(jù)庫(kù) springbootdb:
CREATE DATABASE springbootdb;
b.創(chuàng)建表 city :(因?yàn)槲蚁矚g徒步)
DROP TABLE IF EXISTS `city`; CREATE TABLE `city` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT'城市編號(hào)', `province_id` int(10) unsigned NOT NULL COMMENT'省份編號(hào)', `city_name` varchar(25) DEFAULT NULL COMMENT'城市名稱(chēng)', `description` varchar(25) DEFAULT NULL COMMENT'描述', PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
c.插入數(shù)據(jù)
INSERT cityVALUES (1 ,1,'溫嶺市','BYSocket 的家在溫嶺。');
2. springboot-restful 工程項(xiàng)目結(jié)構(gòu)介紹
springboot-restful 工程項(xiàng)目結(jié)構(gòu)如下圖所示:org.spring.springboot.controller – Controller 層org.spring.springboot.dao – 數(shù)據(jù)操作層 DAOorg.spring.springboot.domain – 實(shí)體類(lèi)org.spring.springboot.service – 業(yè)務(wù)邏輯層Application – 應(yīng)用啟動(dòng)類(lèi)application.properties – 應(yīng)用配置文件,應(yīng)用啟動(dòng)會(huì)自動(dòng)讀取配置3.改數(shù)據(jù)庫(kù)配置打開(kāi) application.properties 文件, 修改相應(yīng)的數(shù)據(jù)源配置,比如數(shù)據(jù)源地址、賬號(hào)、密碼等。(如果不是用 MySQL,自行添加連接驅(qū)動(dòng) pom,然后修改驅(qū)動(dòng)名配置。)
4.編譯工程在項(xiàng)目根目錄 springboot-learning-example,運(yùn)行 maven 指令:?mvn clean install
?
5.運(yùn)行工程右鍵運(yùn)行 springboot-restful 工程 Application 應(yīng)用啟動(dòng)類(lèi)的 main 函數(shù)。用 postman 工具可以如下操作,根據(jù) ID,獲取城市信息GET http://127.0.0.1:8080/api/city/1
獲取城市列表GET http://127.0.0.1:8080/api/city
新增城市信息POST http://127.0.0.1:8080/api/city
更新城市信息PUT http://127.0.0.1:8080/api/city
刪除城市信息DELETE http://127.0.0.1:8080/api/city/2![]()
二、springboot-restful 工程控制層實(shí)現(xiàn)詳解
1.什么是 REST?REST 是屬于 WEB 自身的一種架構(gòu)風(fēng)格,是在 HTTP 1.1 規(guī)范下實(shí)現(xiàn)的。Representational State Transfer 全稱(chēng)翻譯為表現(xiàn)層狀態(tài)轉(zhuǎn)化。Resource:資源。比如 newsfeed;Representational:表現(xiàn)形式,比如用JSON,富文本等;State Transfer:狀態(tài)變化。通過(guò)HTTP 動(dòng)作實(shí)現(xiàn)。理解 REST ,要明白五個(gè)關(guān)鍵要素:資源(Resource)資源的表述(Representation)狀態(tài)轉(zhuǎn)移(State Transfer)統(tǒng)一接口(Uniform Interface)超文本驅(qū)動(dòng)(Hypertext Driven)6 個(gè)主要特性:面向資源(Resource Oriented)可尋址(Addressability)連通性(Connectedness)無(wú)狀態(tài)(Statelessness)統(tǒng)一接口(Uniform Interface)超文本驅(qū)動(dòng)(Hypertext Driven)具體這里就不一一展開(kāi),詳見(jiàn) http://www.infoq.com/cn/articles/understanding-restful-style
2.Spring 對(duì) REST 支持實(shí)現(xiàn)CityRestController.java 城市 Controller 實(shí)現(xiàn) Restful HTTP 服務(wù)
public class CityRestController { @Autowired private CityService cityService; @RequestMapping(value ="/api/city/{id}", method = RequestMethod.GET) public City findOneCity(@PathVariable("id") Long id) { return cityService.findCityById(id); } @RequestMapping(value ="/api/city", method = RequestMethod.GET) public List<City> findAllCity() { return cityService.findAllCity(); } @RequestMapping(value ="/api/city", method = RequestMethod.POST) public void createCity(@RequestBody City city) { cityService.saveCity(city); } @RequestMapping(value ="/api/city", method = RequestMethod.PUT) public void modifyCity(@RequestBody City city) { cityService.updateCity(city); } @RequestMapping(value ="/api/city/{id}", method = RequestMethod.DELETE) public void modifyCity(@PathVariable("id") Long id) { cityService.deleteCity(id); } }
具體 Service 、dao 層實(shí)現(xiàn)看代碼GitHub https://github.com/JeffLi1993/springboot-learning-example/tree/master/springboot-restful
代碼詳解:@RequestMapping 處理請(qǐng)求地址映射。method – 指定請(qǐng)求的方法類(lèi)型:POST/GET/DELETE/PUT 等value – 指定實(shí)際的請(qǐng)求地址consumes – 指定處理請(qǐng)求的提交內(nèi)容類(lèi)型,例如 Content-Type 頭部設(shè)置application/json, text/htmlproduces – 指定返回的內(nèi)容類(lèi)型@PathVariable URL 映射時(shí),用于綁定請(qǐng)求參數(shù)到方法參數(shù)@RequestBody 這里注解用于讀取請(qǐng)求體 body 的數(shù)據(jù),通過(guò) HttpMessageConverter 解析綁定到對(duì)象中
3.HTTP 知識(shí)補(bǔ)充GET 請(qǐng)求獲取Request-URI所標(biāo)識(shí)的資源POST 在Request-URI所標(biāo)識(shí)的資源后附加新的數(shù)據(jù)HEAD 請(qǐng)求獲取由Request-URI所標(biāo)識(shí)的資源的響應(yīng)消息報(bào)頭PUT 請(qǐng)求服務(wù)器存儲(chǔ)一個(gè)資源,并用Request-URI作為其標(biāo)識(shí)DELETE 請(qǐng)求服務(wù)器刪除Request-URI所標(biāo)識(shí)的資源TRACE 請(qǐng)求服務(wù)器回送收到的請(qǐng)求信息,主要用于測(cè)試或診斷CONNECT 保留將來(lái)使用OPTIONS 請(qǐng)求查詢(xún)服務(wù)器的性能,或者查詢(xún)與資源相關(guān)的選項(xiàng)和需求詳情請(qǐng)看《JavaEE 要懂的小事:一、圖解Http協(xié)議》
三、小結(jié)
Springboot 實(shí)現(xiàn) Restful 服務(wù),基于 HTTP / JSON 傳輸,適用于前后端分離。這只是個(gè)小demo,沒(méi)有加入bean validation這種校驗(yàn)。還有各種業(yè)務(wù)場(chǎng)景。歡迎掃一掃我的公眾號(hào)關(guān)注 — 及時(shí)得到博客訂閱哦!— http://www.bysocket.com/ —— https://github.com/JeffLi1993 —![]()
更多建議: