1、什么是RestFul

经常上网的同学会发现,现代软件的一个重要趋势就是互联网化,几乎没有一款软件是纯粹的单机版了。通常的情况下,软件管理着服务器的资源以及这些资源的状态变化,用户通过在浏览器输入http地址,能够对服务器的资源进行各种处理。这就要求软件提供一组访问接口,使得软件能够和互联网的http访问格式完美对接。REST对这种借口的一种约束标准。(这里我们所说的软件,最常见的形式就是一个网站系统)

REST 是 Representational state transfer 的缩写,翻译过来的意思是表达性状态转换,REST 这个词是2000 年 Roy Fielding 在其博士论文中创造出来的。REST 是一种架构风格,它包含了一个分布式超文本系统中对于组件、连接器和数据的约束。REST 是作为互联网自身架构的抽象而出现的,其关键在于所定义的架构上的各种约束。只有满足这些约束,才能称之为符合 REST 架构风格,即所谓的“RESTful”服务。

这里我们不对这些约束进行描述。详细内容可以参考IBM的一篇文章(http://www.ibm.com/developerworks/cn/java/j-lo-SpringHATEOAS/)

2、HATEOAS

HATEOAS(Hypermedia as the engine of application state)是 REST 架构风格中最复杂的约束,也是构建成熟 REST 服务的核心。它的重要性在于打破了客户端和服务器之间严格的契约,使得客户端可以更加智能和自适应,而 REST 服务本身的演化和更新也变得更加容易。

上面这段话其实很难理解。首先hateoas这个单词就很难记住,我的方法是先记住hate(恨之入骨),然后再加上oas。经过一点实践,我认为这里的自适应,指得是服务器返回给客户端的信息中,会自动包含了客户端能够进行的操作,这样二者之间就不用通过建立一份额外的文档来约定访问的格式了。

3、Spring HATEOAS

Spring Hateoas,是Spring的一个子项目,Spring HATEOAS 的主要功能在于提供了简单的机制来创建这些满足HATEOAS要求的链接,并与 Spring MVC 框架有很好的结合。在实践中,我不会单独使用这个子项目,而是采用下面的方法。

4、Spring Data REST

Spring Data Rest也是Spring的一个子项目,它的主要功能就是把你的Spring Data Repositories以满足HATEOAS的格式暴露出去,因此你的JPA资源Repository可以用一种通用的http访问格式对你的Respository进行CRUD,而且可以省去大部分controller和services的逻辑,因为Spring Data REST已经为你都做好了,你只需要保证你的http访问格式正确即可,是不是很酷?(关于JPA,也是一个比较常见的概念,我会在另外一篇博客里面讲一下我的理解)

还有更酷的一点,我们可以对Repositories对外暴露的格式和内容进行个性化的定制,这属于稍微高级一点的内容,我们这里不涉及。

现在我们就可以看看默认的Spring Data Test对访问格式是如何定义的,当然,最权威的还是官方的文档,地址在这里(http://docs.spring.io/spring-data/rest/docs/2.4.0.RELEASE/reference/html/)

下面是我用Spring Data Rest提供的工具The HAL Browser得到一些结果(关于该工具的使用,请参考http://docs.spring.io/spring-data/rest/docs/2.4.0.RELEASE/reference/html/#_the_hal_browser)。

再补充一下:我对默认的配置做了一点改动,这样我们在返回的json格式的结构体中会包含id。

public class AppConfig extends RepositoryRestMvcConfiguration {
@Override
public RepositoryRestConfiguration config() {
RepositoryRestConfiguration config = super.config();
config.setDefaultPageSize(6);/*设置默认页大小*/
//设置返回的json Body中显示id
config.exposeIdsFor(User.class);
     //config.setReturnBodyOnCreate(true);设置创建成果后返回创建成果的结果,实际上这样做没有什么意义
return config;
}
}

4.1获取集合资源

General
Remote Address:[::1]:8080
Request URL:http://localhost:8080/traveller/users?page=0
Request Method:GET
Status Code: OK Response Headers
Content-Type:application/json;charset=UTF-8
Date:Wed, 30 Sep 2015 17:27:07 GMT
Server:Apache-Coyote/1.1
Transfer-Encoding:chunked //下面是返回的JSON格式体
{
  "_links": {
"self": {
"href": "http://localhost:8080/traveller/users{?page,size,sort}",
"templated": true
},
"next": {
"href": "http://localhost:8080/traveller/users?page=1&size=6{&sort}",
"templated": true
},
"search": {
"href": "http://localhost:8080/traveller/users/search"
}
},
"_embedded": {
"users": [
{
"id": 1,
"birthdate": "2015-09-16",
"country": 0,
"email": "stive.jobs@apple.com",
"enabled": true,
"info": null,
"name": "JOBS",
"password": "steve",
"phone": "0033 1 23 45 67 89",
"picture": "1.jpg",
"gender": 0,
"regdate": "2015-09-25T02:54:01.000+0000",
"_links": {
"self": {
"href": "http://localhost:8080/traveller/users/1"
}
}
},
{
"id": 2,
"birthdate": null,
"country": 0,
"email": "bill.gates@microsoft.com",
"enabled": true,
"info": null,
"name": "GATES",
"password": "bill",
"phone": "0033 1 23 45 67 89",
"picture": "2.jpg",
"gender": 0,
"regdate": null,
"_links": {
"self": {
"href": "http://localhost:8080/traveller/users/2"
}
}
},
{
"id": 3,
"birthdate": null,
"country": 0,
"email": "mark.zuckerberg@facebook.com",
"enabled": true,
"info": null,
"name": "ZUCKERBERG",
"password": "zuckerberg",
"phone": "0033 1 23 45 67 89",
"picture": "3.jpg",
"gender": 0,
"regdate": null,
"_links": {
"self": {
"href": "http://localhost:8080/traveller/users/3"
}
}
},
{
"id": 4,
"birthdate": null,
"country": 0,
"email": "tim.cook@apple.com",
"enabled": true,
"info": null,
"name": "COOK",
"password": "cook",
"phone": "0033 1 23 45 67 89",
"picture": "4.jpg",
"gender": 0,
"regdate": null,
"_links": {
"self": {
"href": "http://localhost:8080/traveller/users/4"
}
}
},
{
"id": 5,
"birthdate": null,
"country": 0,
"email": "larry.page@gmail.com",
"enabled": true,
"info": null,
"name": "Page",
"password": "page",
"phone": "0033 1 23 45 67 89",
"picture": "5.jpg",
"gender": 0,
"regdate": null,
"_links": {
"self": {
"href": "http://localhost:8080/traveller/users/5"
}
}
},
{
"id": 6,
"birthdate": null,
"country": 0,
"email": "sergey.brin@gmail.com",
"enabled": true,
"info": null,
"name": "Brin",
"password": "brin",
"phone": "0033 1 23 45 67 89",
"picture": "6.jpg",
"gender": 0,
"regdate": null,
"_links": {
"self": {
"href": "http://localhost:8080/traveller/users/6"
}
}
}
]
},
"page": {
"size": 6,
"totalElements": 25,
"totalPages": 5,
"number": 0
}
}

4.2获取单个资源

General
Remote Address:[::1]:8080
Request URL:http://localhost:8080/traveller/users/1
Request Method:GET
Status Code:200 OK Response Headers
Content-Type:application/json;charset=UTF-8
Date:Wed, 30 Sep 2015 17:35:23 GMT
Server:Apache-Coyote/1.1
Transfer-Encoding:chunked
{
"id": 1,
"birthdate": "2015-09-16",
"country": 0,
"email": "stive.jobs@apple.com",
"enabled": true,
"info": null,
"name": "JOBS",
"password": "steve",
"phone": "0033 1 23 45 67 89",
"picture": "1.jpg",
"gender": 0,
"regdate": "2015-09-25T02:54:01.000+0000",
"_links": {
"self": {
"href": "http://localhost:8080/traveller/users/1"
}
}
}

4.2保存单个资源

General
Remote Address:[::1]:8080
Request URL:http://localhost:8080/traveller/users
Request Method:POST
Status Code:201 Created Response Headers
Content-Length:0
Date:Wed, 30 Sep 2015 17:40:00 GMT
Location:http://localhost:8080/traveller/users/30
Server:Apache-Coyote/1.1 无其他返回值

RestFul && HATEOAS && Spring-Data-Rest介绍的更多相关文章

  1. springboot:spring data jpa介绍

    转载自:https://www.cnblogs.com/ityouknow/p/5891443.html 在上篇文章springboot(二):web综合开发中简单介绍了一下spring data j ...

  2. spring boot(五)Spring data jpa介绍

    在上篇文章springboot(二):web综合开发中简单介绍了一下spring data jpa的基础性使用,这篇文章将更加全面的介绍spring data jpa 常见用法以及注意事项 使用spr ...

  3. Spring Data JPA介绍与简单案例

    一.Spring Data JPA介绍 可以理解为JPA规范的再次封装抽象,底层还是使用了Hibernate的JPA技术实现,引用JPQL(Java Persistence Query Languag ...

  4. Spring Data JPA 介绍

    Spring-data-jpa的基本介绍:JPA诞生的缘由是为了整合第三方ORM框架,建立一种标准的方式,百度百科说是JDK为了实现ORM的天下归一,目前也是在按照这个方向发展,但是还没能完全实现.在 ...

  5. spring data jpa介绍

    首先了解JPA是什么? JPA(JavaPersistence API)是Sun官方提出的Java持久化规范.它为Java开发人员提供了一种对象/关联映射工具来管理Java应用中的关系数据.他的出现主 ...

  6. 初探 spring data(一)--- spring data 概述

    由于自己一个项目要用多到Sql与NoSql两种截然不同的数据结构,但在编程上我希望统一接口API,让不同类型的数据库能在相同的编程接口模式下运作.于是找了一个spring的官网,发现一个spring ...

  7. 通过Spring Data Neo4J操作您的图形数据库

    在前面的一篇文章<图形数据库Neo4J简介>中,我们已经对其内部所使用的各种机制进行了简单地介绍.而在我们尝试对Neo4J进行大版本升级时,我发现网络上并没有任何成型的样例代码以及简介,而 ...

  8. Springboot 系列(十)使用 Spring data jpa 访问数据库

    前言 Springboot data jpa 和 Spring jdbc 同属于 Spring开源组织,在 Spring jdbc 之后又开发了持久层框架,很明显 Spring data jpa 相对 ...

  9. 【ORM框架】Spring Data JPA(一)-- 入门

    本文参考:spring Data JPA入门   [原创]纯干货,Spring-data-jpa详解,全方位介绍  Spring Data JPA系列教程--入门 一.Spring Data JPA介 ...

  10. 展开被 SpringBoot 玩的日子 《 五 》 spring data jpa 的使用

    在上篇文章< 展开被 SpringBoot 玩的日子 < 二 >WEB >中简单介绍了一下spring data jpa的基础性使用,这篇文章将更加全面的介绍spring da ...

随机推荐

  1. Android 实现书籍翻页效果----升级篇

    自从之前发布了<Android 实现书籍翻页效果----完结篇 >之后,收到了很多朋友给我留言,前段时间由于事情较多,博客写得太匆忙很多细节地方没有描述清楚.所以不少人对其中的地方有不少不 ...

  2. Blueprint编译过程

    Blueprint 编译概述 一.术语 Blueprint,像C++语言一下的,在游戏中使用前须要编译.当你在BP编辑器中,点击编译button时候.BP资源開始把属性和图例过程转换为一个类对象处理. ...

  3. The internals of Python string interning

    JUNE 28TH, 2014Tweet This article describes how Python string interning works in CPython 2.7.7. A fe ...

  4. apue.h

    [root@localhost unix_env_advance_prog]# cat apue.h #ifndef _APUE_H #define _APUE_H #define _XOPEN_SO ...

  5. PAT 1019

    1019. General Palindromic Number (20) A number that will be the same when it is written forwards or ...

  6. Getting Started with Zend Framework MVC Applications

    Getting Started with Zend Framework MVC Applications This tutorial is intended to give an introducti ...

  7. 中兴电信光纤猫F612管理员密码获取方法

    1.telnet 192.168.1.1 账号:root 密码:Zte521 2.输入命令: sendcmd 1 DB p DevAuthInfo 得到管理员账号密码如下: <DM name=& ...

  8. Android进阶笔记13:RoboBinding(实现了数据绑定 Presentation Model(MVVM) 模式的Android开源框架)

    1.RoboBinding RoboBinding是一个实现了数据绑定 Presentation Model(MVVM) 模式的Android开源框架.从简单的角度看,他移除了如addXXListen ...

  9. Linux文件与目录管理之ls的使用

    来源:鸟哥的私房菜 查看文件与目录 ls ls [-aAdfFhilnrRSt] 目录名 ls [--color={never,auto,always}] ls [--full-time] 目录名 选 ...

  10. angularjs页面传参

    例如:路由配置如下: $stateProvider.state('admin.userList', { url: '/listUser?type&role',         //参数必须先在 ...