????????????????????????????????????????Springboot 實(shí)現(xiàn) Restful 服務(wù),基于 HTTP / JSON 傳輸
主目錄:http://www.spring4all.com/article/246
轉(zhuǎn)載于:http://www.spring4all.com/article/268
運(yùn)行 springboot-restful 工程
git clone 下載工程?springboot-learning-example?读第,項(xiàng)目地址見 GitHub -https://github.com/JeffLi1993/springboot-learning-example。下面開始運(yùn)行工程步驟(Quick Start):
1.數(shù)據(jù)庫準(zhǔn)備
a.創(chuàng)建數(shù)據(jù)庫 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 '城市名稱',
? `description` varchar(25) DEFAULT NULL COMMENT '描述',
? PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
c.插入數(shù)據(jù)
INSERT city VALUES (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ù)操作層 DAO
org.spring.springboot.domain - 實(shí)體類
org.spring.springboot.service - 業(yè)務(wù)邏輯層
Application - 應(yīng)用啟動(dòng)類
application.properties - 應(yīng)用配置文件卦方,應(yīng)用啟動(dòng)會(huì)自動(dòng)讀取配置
3.改數(shù)據(jù)庫配置
打開 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)類的 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)詳解
什么是 REST?
REST 是屬于 WEB 自身的一種架構(gòu)風(fēng)格徽惋,是在 HTTP 1.1 規(guī)范下實(shí)現(xiàn)的。Representational State Transfer 全稱翻譯為表現(xiàn)層狀態(tài)轉(zhuǎn)化座韵。Resource:資源险绘。比如 newsfeed踢京;Representational:表現(xiàn)形式,比如用JSON宦棺,富文本等瓣距;State Transfer:狀態(tài)變化。通過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)
無狀態(tài)(Statelessness)
統(tǒng)一接口(Uniform Interface)
超文本驅(qū)動(dòng)(Hypertext Driven)
詳見?http://www.infoq.com/cn/articles/understanding-restful-style
Spring 對(duì) REST 支持實(shí)現(xiàn)
CityRestController.java?城市 Controller 實(shí)現(xiàn) Restful HTTP 服務(wù)
具體 Service 蹈丸、dao 層實(shí)現(xiàn)看代碼GitHub?https://github.com/JeffLi1993/springboot-learning-example/tree/master/springboot-restful
代碼詳解:
@RequestMapping?處理請(qǐng)求地址映射
3.HTTP 知識(shí)補(bǔ)充
詳情請(qǐng)看《JavaEE 要懂的小事:一、圖解Http協(xié)議》,地址為:https://www.bysocket.com/?p=282
Springboot 實(shí)現(xiàn) Restful 服務(wù)呐芥,基于 HTTP / JSON 傳輸逻杖,適用于前后端分離。這只是個(gè)小demo贩耐,沒有加入bean validation這種校驗(yàn)弧腥。還有各種業(yè)務(wù)場(chǎng)景。
推薦:《?Springboot 整合 Mybatis 的完整 Web 案例》,地址為:https://www.bysocket.com/?p=1610