REST风格简介

介绍HATEOAS之前先简单介绍一下REST,REST 是 Representational state transfer 的缩写,翻译过来的意思是表达性状态转换。REST是一种架构的风格

Richardson Maturity Model

Richardson 提出了REST一种 成熟度模型,我们称之为Richardson Maturity Model,这种模式将REST按照成熟度划分为4个等级

  • Level0:使用HTTP作为WEB服务的传输方式,以REST样式公开SOAP Web服务
  • Level1:使用适当的URI(使用名词)公开资源,这种方式提出了资源的概念
  • Level2:资源使用正确的URI + HTTP方法,比如更新用户就用put方式,查询用get方式
  • Level3:使用HATEOAS(作为应用程序状态引擎的超媒体),在资源的表达中包含了链接信息,客户端可以在链接信息中发现可以执行的操作

HATEOAS是什么?

HATEOAS代表“超媒体是应用程序状态的引擎”

从前言我们已经可以清楚知道,使用HATEOAS约束是REST风格中成熟度最高的,也是官方推荐的一种方式,没使用HATEOAS的项目,服务端和客户端是耦合的,客户端只能通过相关文档来知道服务端做了什么修改,使用HATEOAS约束的REST服务,服务端修改接口信息后,客户端可以通过服务器提供的资源的表达来智能地发现可以执行的操作,客户端不需要做啥修改,因为资源信息是会动态改变的

在Spring的官网,已经有提供这个项目的相关文档,链接:https://spring.io/projects/spring-hateoas

SpringBoot HATEOAS

SpringBoot中也有集成HATEOAS,本博客介绍一下如何使用

工具准备:

  • JDK8.0
  • Maven 3.0+构建工具
  • Eclipse或者IntelliJ IDEA
  • git&gitlab

Maven相关配置

在pom.xml加上hateoas配置

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-hateoas</artifactId>
</dependency>

因为是要写个web简单curd例子,其它需要的也加上

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-hateoas</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.25</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.40</version>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>

实体类实现ResourceSupport

Model类实现hateoas提供的ResourceSuppor

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import org.springframework.hateoas.ResourceSupport; import javax.persistence.*;
import java.io.Serializable;
@Entity
@Table(name="sys_user")
public class SysUserInfo extends ResourceSupport implements Serializable{ @Id
@GeneratedValue
private Long userId;
@Column(unique=true,length=20,nullable=false)
private String username;
@Column(length=2,nullable=true)
private String sex;
@Column(length=10,nullable=true)
private String password; public SysUserInfo(){ } @JsonCreator
public SysUserInfo(@JsonProperty("userId")Long userId,@JsonProperty("username")String username,
@JsonProperty("sex")String sex,@JsonProperty("password")String password){
this.userId = userId;
this.username = username;
this.sex = sex;
this.password = password;
}
}
....

接口调用,基于HATEOAS模式

@GetMapping("/findBySysUserId/{userId}")
public SysUserInfo findBySysUserId(@PathVariable("userId") long userId) {
if (LOG.isInfoEnabled()) {
LOG.info("请求参数userId : {}" , userId);
}
Optional<SysUserInfo> sysUserInfo = Optional.ofNullable(sysUserRepository.findByUserId(userId));
if (!sysUserInfo.isPresent()) {
throw new NotFoundException("查询不到用户信息! userId:"+userId);
}
//Resource<SysUserInfo> resource = new Resource<SysUserInfo>(sysUserInfo.get());
ControllerLinkBuilder linkBuilder = linkTo(methodOn(this.getClass()).findBySysUserId(userId));
sysUserInfo.get().add(linkBuilder.withRel("findBySysUserId"));
return sysUserInfo.get();
}

实例代码:github链接下载

SpringBoot HATEOAS用法简介的更多相关文章

  1. SpringBoot系列之@Conditional注解用法简介

    SpringBoot系列之@Conditional注解用法简介 引用Spring官方文档的说法介绍一下@Conditional注解:Spring5.0.15版本@Conditional注解官方文档 @ ...

  2. SpringBoot系列之@PropertySource用法简介

    SpringBoot系列之@PropertySource用法简介 继上篇博客:SpringBoot系列之@Value和@ConfigurationProperties用法对比之后,本博客继续介绍一下@ ...

  3. SpringBoot系列之外部配置用法简介

    SpringBoot系列之外部配置用法简介 引用Springboot官方文档的说法,官方文档总共列举了如下用法: 1.Devtools global settings properties on yo ...

  4. SpringBoot常用配置简介

    SpringBoot常用配置简介 1. SpringBoot中几个常用的配置的简单介绍 一个简单的Spring.factories # Bootstrap components org.springf ...

  5. IOS NSInvocation用法简介

    IOS NSInvocation用法简介 2012-10-25 19:59 来源:博客园 作者:csj007523 字号:T|T [摘要]在 iOS中可以直接调用某个对象的消息方式有两种,其中一种就是 ...

  6. JodaTime用法简介

    JodaTime用法简介 Java的Date和Calendar用起来简直就是灾难,跟C#的DateTime差距太明显了,幸好有JodaTime 本文简单罗列JodaTime的用法 package co ...

  7. Apache自带压力测试工具ab用法简介

    ab命令原理 ab命令会创建很多的并发访问线程,模拟多个访问者同时对某一URL进行访问.它的测试目标是基于URL的,因此,既可以用来测试Apache的负载压力,也可以测试nginx.lighthttp ...

  8. Postman用法简介

    转自:http://blog.csdn.net/flowerspring/article/details/52774399 Postman用法简介 转载 2016年10月10日 09:04:10 10 ...

  9. MSSQL Sql加密函数 hashbytes 用法简介

    转自:http://www.maomao365.com/?p=4732 一.mssql sql hashbytes 函数简介 hashbytes函数功能为:返回一个字符,通过 MD2.MD4.MD5. ...

随机推荐

  1. Nginx 日志文件 access_log详解及日志分割

    Module ngx_http_log_module nginx 日志相关指令主要有两条, 一条是log_format,用来设置日志格式,另外一条是access_log,用来指定日志文件的存放路径.格 ...

  2. linux安装couchbase

    一.卸载 查看已安装的版本 rpm -qa|grep couchbase 卸载已安装的版本 rpm -e xxxx 二.安装 安装couchbase rpm -i xxxx.rpm 浏览器中访问809 ...

  3. Spark比MR快是因为在内存中计算?错!

    MapReduce 就像一台又慢又稳的老爷车,虽然距离 MapReduce 面市到现在已经过去了十几年的时间,但它始终没有被淘汰,任由大数据技术日新月异.蓬蓬勃勃.花里胡哨地发展,这个生态圈始终有它的 ...

  4. go语言 链表练习

    package main import "fmt" //定义节点 type Node struct { Data int Next *Node } /* * 返回第一个节点 * h ...

  5. Linux初识之VMWare中Centos7的安装

    Windows平台下VMWare 14安装Centos 7 一.虚拟机硬件配置 1.选择创建新的虚拟机: 2.选择自定义(高级)进行自定义配置,单击下一步: 3.选择虚拟机硬件兼容性为默认,单击下一步 ...

  6. 导出 mysql 数据到 redis

    决定你要导入到 redis 的数据类型 假设我的表 t_user 的结构为 列名 注释 类型 name 名称 varchar idcard 身份证号 varchar phone 手机号 varchar ...

  7. PHPstorm出现的端口号错误问题(502)

    咔咔咔-听好 PhpStorm的默认端口是63342,但是在浏览器会提示502错误. 同时Phpstorm右下角会报错:Please ensure that configured PHP Interp ...

  8. .Net Core中间件和过滤器实现错误日志记录

    1.中间件的概念 ASP.NET Core的处理流程是一个管道,中间件是组装到应用程序管道中用来处理请求和响应的组件. 每个中间件可以: 选择是否将请求传递给管道中的下一个组件. 可以在调用管道中的下 ...

  9. Navicat使用常见的两个问题及解决方法,提高开发效率

    Navicat使用常见问题 在我们日常开发过程中,一般不会直接使用命令行来操作 MYSQL 数据库,而会选择一些图形化界面去帮助我们来进行此类操作,常用的有:SQLyog(Logo也是小海豚),Nav ...

  10. 你不知道的 IDEA Debug调试小技巧

    一.多线程调试断点 Intellij IDEA 的debug断点调试是有一个模式的选择的,就像下面这张图,平时我们都使用的是默认的 ALL(在Eclipse中默认是线程模式) ,这种模式我们只能将一个 ...