SpringBoot HATEOAS用法简介
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用法简介的更多相关文章
- SpringBoot系列之@Conditional注解用法简介
SpringBoot系列之@Conditional注解用法简介 引用Spring官方文档的说法介绍一下@Conditional注解:Spring5.0.15版本@Conditional注解官方文档 @ ...
- SpringBoot系列之@PropertySource用法简介
SpringBoot系列之@PropertySource用法简介 继上篇博客:SpringBoot系列之@Value和@ConfigurationProperties用法对比之后,本博客继续介绍一下@ ...
- SpringBoot系列之外部配置用法简介
SpringBoot系列之外部配置用法简介 引用Springboot官方文档的说法,官方文档总共列举了如下用法: 1.Devtools global settings properties on yo ...
- SpringBoot常用配置简介
SpringBoot常用配置简介 1. SpringBoot中几个常用的配置的简单介绍 一个简单的Spring.factories # Bootstrap components org.springf ...
- IOS NSInvocation用法简介
IOS NSInvocation用法简介 2012-10-25 19:59 来源:博客园 作者:csj007523 字号:T|T [摘要]在 iOS中可以直接调用某个对象的消息方式有两种,其中一种就是 ...
- JodaTime用法简介
JodaTime用法简介 Java的Date和Calendar用起来简直就是灾难,跟C#的DateTime差距太明显了,幸好有JodaTime 本文简单罗列JodaTime的用法 package co ...
- Apache自带压力测试工具ab用法简介
ab命令原理 ab命令会创建很多的并发访问线程,模拟多个访问者同时对某一URL进行访问.它的测试目标是基于URL的,因此,既可以用来测试Apache的负载压力,也可以测试nginx.lighthttp ...
- Postman用法简介
转自:http://blog.csdn.net/flowerspring/article/details/52774399 Postman用法简介 转载 2016年10月10日 09:04:10 10 ...
- MSSQL Sql加密函数 hashbytes 用法简介
转自:http://www.maomao365.com/?p=4732 一.mssql sql hashbytes 函数简介 hashbytes函数功能为:返回一个字符,通过 MD2.MD4.MD5. ...
随机推荐
- linux常见报错
零.目录 一. 文件和目录类 File exist 文件已经存在 No such file or directory 没有这个文件或目录(这个东西不存在) command not found 命令找不 ...
- C#面试题目整理(一)
1.您在什么情况下会用到虚方法?它与接口有什么不同?当子类需要重新定义父类的一个方法时,父类的方法需要定义为虚方法:在定义接口的时候不能又方法体,但是虚方法可以有方法体,实现时,子类可以不实现父类的虚 ...
- hadoop之hdfs命令详解
本篇主要对hadoop命令和hdfs命令进行阐述,yarn命令会在之后的文章中体现 hadoop fs命令可以用于其他文件系统,不止是hdfs文件系统内,也就是说该命令的使用范围更广可以用于HDFS. ...
- git使用和操作
git提交日志的规范 为了更规范的开发,特别是团队协同开发,对于代码托管工具的提交上都会有要求的. 作为开发者,我们一定要注重提交日志的规范性,我们要对自己写的代码负责.提交日志规范很多,最近看到了一 ...
- 基于SpringBoot实现AOP+jdk/CGlib动态代理详解
动态代理是一种设计模式.在Spring中,有俩种方式可以实现动态代理--JDK动态代理和CGLIB动态代理. JDK动态代理 首先定义一个人的接口: public interface Person { ...
- Qt 做一个类似微信滑动聊天界面的demo
版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://www.cnblogs.com/lihuidashen/p/115889 ...
- webstorm中关闭烦人Eslint语法检查
打开许久没打开的webstrom,以前关闭的配置不知道怎么又乱了,react项目到处报错,真是没法忍. 关闭eslint位置:File-->Setting-->Languages& ...
- 7.Sentinel源码分析—Sentinel是怎么和控制台通信的?
这里会介绍: Sentinel会使用多线程的方式实现一个类Reactor的IO模型 Sentinel会使用心跳检测来观察控制台是否正常 Sentinel源码解析系列: 1.Sentinel源码分析-F ...
- 夯实Java基础系列10:深入理解Java中的异常体系
目录 为什么要使用异常 异常基本定义 异常体系 初识异常 异常和错误 异常的处理方式 "不负责任"的throws 纠结的finally throw : JRE也使用的关键字 异常调 ...
- 【柠檬班】史上最简单的Jmeter跨线程组取参数值的两种办法(不写代码)【原创】
如果你工作中已经在用jmeter做接口测试,或性能测试了,你可能会遇到一个麻烦,哪就是jmeter的变量值不能跨线程组传递. 看,官方就已经给出了解释.这个不是jmeter的缺陷,这是jmeter ...