SpringBoot HATEOAS用法簡介(入門)
REST風(fēng)格簡介
介紹HATEOAS之前先簡單介紹一下REST,REST 是 Representational state transfer 的縮寫,翻譯過來的意思是表達性狀態(tài)轉(zhuǎn)換。REST是一種架構(gòu)的風(fēng)格
Richardson Maturity Model
Richardson 提出了REST一種 成熟度模型,我們稱之為Richardson Maturity Model,這種模式將REST按照成熟度劃分為4個等級
- Level0:使用HTTP作為WEB服務(wù)的傳輸方式,以REST樣式公開SOAP Web服務(wù)
- Level1:使用適當(dāng)?shù)腢RI(使用名詞)公開資源,這種方式提出了資源的概念
- Level2:資源使用正確的URI + HTTP方法,比如更新用戶就用put方式,查詢用get方式
- Level3:使用HATEOAS(作為應(yīng)用程序狀態(tài)引擎的超媒體),在資源的表達中包含了鏈接信息,客戶端可以在鏈接信息中發(fā)現(xiàn)可以執(zhí)行的操作
HATEOAS是什么?
HATEOAS代表“超媒體是應(yīng)用程序狀態(tài)的引擎”
從前言我們已經(jīng)可以清楚知道,使用HATEOAS約束是REST風(fēng)格中成熟度最高的,也是官方推薦的一種方式,沒使用HATEOAS的項目,服務(wù)端和客戶端是耦合的,客戶端只能通過相關(guān)文檔來知道服務(wù)端做了什么修改,使用HATEOAS約束的REST服務(wù),服務(wù)端修改接口信息后,客戶端可以通過服務(wù)器提供的資源的表達來智能地發(fā)現(xiàn)可以執(zhí)行的操作,客戶端不需要做啥修改,因為資源信息是會動態(tài)改變的
在Spring的官網(wǎng),已經(jīng)有提供這個項目的相關(guān)文檔,鏈接:https://spring.io/projects/spring-hateoas
SpringBoot HATEOAS
SpringBoot中也有集成HATEOAS,本博客介紹一下如何使用
工具準備:
- JDK8.0
- Maven 3.0+構(gòu)建工具
- Eclipse或者IntelliJ IDEA
- git&gitlab
Maven相關(guān)配置
在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>
實體類實現(xiàn)ResourceSupport
Model類實現(xiàn)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;
}
}
....
接口調(diào)用,基于HATEOAS模式
@GetMapping("/findBySysUserId/{userId}")
public SysUserInfo findBySysUserId(@PathVariable("userId") long userId) {
if (LOG.isInfoEnabled()) {
LOG.info("請求參數(shù)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鏈接下載
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java使用Runnable接口創(chuàng)建線程的示例代碼
在Java中,多線程編程是實現(xiàn)并發(fā)操作的重要手段之一,通過多線程,程序可以同時執(zhí)行多個任務(wù),從而提高應(yīng)用程序的效率和響應(yīng)速度,Java提供了多種創(chuàng)建線程的方式,其中實現(xiàn)Runnable接口是最常見且推薦的方式之一,本文將詳細介紹如何使用Runnable接口創(chuàng)建線程2025-02-02

