ysocket.com 泥瓦匠bysocket 希望转载,保留摘要,谢谢!
“怎样的人生才是没有遗憾的人生?我的体会是:(1)拥有健康;(2)创造“难忘时刻”;(3)尽力做好自己,不必改变世界;(4)活在当下。” – 《向死而生》李开复
基于上一篇《springboot 整合 mybatis 的完整 web 案例》,这边我们着重在 控制层 讲讲。讲讲如何在 springboot 实现 restful 服务,基于 http / json 传输。
1.数据库准备
a.创建数据库 springbootdb:
create database springbootdb;
b.创建表 city :(因为我喜欢徒步)
drop table if exists `city`;
create table `city` (
`id` int(10) unsigned not null auto_increment comment ‘城市编号’,
`province_id` int(10) unsigned not null comment ‘省份编号’,
`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.插入数据
insert city values (1 ,1,’温岭市’,’bysocket 的家在温岭。’);
2. springboot-restful 工程项目结构介绍
springboot-restful 工程项目结构如下图所示:
org.spring.springboot.controller – controller 层
org.spring.springboot.dao – 数据操作层 dao
org.spring.springboot.domain – 实体类
org.spring.springboot.service – 业务逻辑层
application – 应用启动类
application.properties – 应用配置文件,应用启动会自动读取配置
3.改数据库配置
打开 application.properties 文件, 修改相应的数据源配置,比如数据源地址、账号、密码等。(如果不是用 mysql,自行添加连接驱动 pom,然后修改驱动名配置。)
4.编译工程
在项目根目录 springboot-learning-example,运行 maven 指令:
mvn clean install
5.运行工程
右键运行 springboot-restful 工程 application 应用启动类的 main 函数。
用 postman 工具可以如下操作,
根据 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
1.什么是 rest?
rest 是属于 web 自身的一种架构风格,是在 http 1.1 规范下实现的。representational state transfer 全称翻译为表现层状态转化。resource:资源。比如 newsfeed;representational:表现形式,比如用json,富文本等;state transfer:状态变化。通过http 动作实现。
理解 rest ,要明白五个关键要素:
资源(resource) 资源的表述(representation) 状态转移(state transfer) 统一接口(uniform interface) 超文本驱动(hypertext driven)
6 个主要特性:
面向资源(resource oriented) 可寻址(addressability) 连通性(connectedness) 无状态(statelessness)
2.spring 对 rest 支持实现
cityrestcontroller.java 城市 controller 实现 restful http 服务
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<code>public</code> <code>class</code> <code>cityrestcontroller {</code>
<code> </code><code>@autowired</code>
<code> </code><code>private</code> <code>cityservice cityservice;</code>
<code> </code><code>@requestmapping</code><code>(value =</code><code>"/api/city/{id}"</code><code>, method = requestmethod.get)</code>
<code> </code><code>public</code> <code>city findonecity(</code><code>@pathvariable</code><code>(</code><code>"id"</code><code>) long id) {</code>
<code> </code><code>return</code> <code>cityservice.findcitybyid(id);</code>
<code> </code><code>}</code>
<code> </code><code>@requestmapping</code><code>(value =</code><code>"/api/city"</code><code>, method = requestmethod.get)</code>
<code> </code><code>public</code> <code>list<city> findallcity() {</code>
<code> </code><code>return</code> <code>cityservice.findallcity();</code>
<code> </code><code>@requestmapping</code><code>(value =</code><code>"/api/city"</code><code>, method = requestmethod.post)</code>
<code> </code><code>public</code> <code>void</code> <code>createcity(</code><code>@requestbody</code> <code>city city) {</code>
<code> </code><code>cityservice.savecity(city);</code>
<code> </code><code>@requestmapping</code><code>(value =</code><code>"/api/city"</code><code>, method = requestmethod.put)</code>
<code> </code><code>public</code> <code>void</code> <code>modifycity(</code><code>@requestbody</code> <code>city city) {</code>
<code> </code><code>cityservice.updatecity(city);</code>
<code> </code><code>@requestmapping</code><code>(value =</code><code>"/api/city/{id}"</code><code>, method = requestmethod.delete)</code>
<code> </code><code>public</code> <code>void</code> <code>modifycity(</code><code>@pathvariable</code><code>(</code><code>"id"</code><code>) long id) {</code>
<code> </code><code>cityservice.deletecity(id);</code>
<code>}</code>
代码详解:
@requestmapping 处理请求地址映射。
method – 指定请求的方法类型:post/get/delete/put 等
value – 指定实际的请求地址
consumes – 指定处理请求的提交内容类型,例如 content-type 头部设置application/json, text/html
produces – 指定返回的内容类型
@pathvariable url 映射时,用于绑定请求参数到方法参数
@requestbody 这里注解用于读取请求体 boy 的数据,通过 httpmessageconverter 解析绑定到对象中
3.http 知识补充
get 请求获取request-uri所标识的资源
post 在request-uri所标识的资源后附加新的数据
head 请求获取由request-uri所标识的资源的响应消息报头
put 请求服务器存储一个资源,并用request-uri作为其标识
delete 请求服务器删除request-uri所标识的资源
trace 请求服务器回送收到的请求信息,主要用于测试或诊断
connect 保留将来使用
options 请求查询服务器的性能,或者查询与资源相关的选项和需求
springboot 实现 restful 服务,基于 http / json 传输,适用于前后端分离。这只是个小demo,没有加入bean validation这种校验。还有各种业务场景。
欢迎扫一扫我的公众号关注 — 及时得到博客订阅哦!
— http://www.bysocket.com/ —
— https://github.com/jeffli1993 —