SpringBoot HATEOAS用法簡(jiǎn)介(入門(mén))
REST風(fēng)格簡(jiǎn)介
介紹HATEOAS之前先簡(jiǎn)單介紹一下REST,REST 是 Representational state transfer 的縮寫(xiě),翻譯過(guò)來(lái)的意思是表達(dá)性狀態(tài)轉(zhuǎn)換。REST是一種架構(gòu)的風(fēng)格
Richardson Maturity Model
Richardson 提出了REST一種 成熟度模型,我們稱之為Richardson Maturity Model,這種模式將REST按照成熟度劃分為4個(gè)等級(jí)
- Level0:使用HTTP作為WEB服務(wù)的傳輸方式,以REST樣式公開(kāi)SOAP Web服務(wù)
- Level1:使用適當(dāng)?shù)腢RI(使用名詞)公開(kāi)資源,這種方式提出了資源的概念
- Level2:資源使用正確的URI + HTTP方法,比如更新用戶就用put方式,查詢用get方式
- Level3:使用HATEOAS(作為應(yīng)用程序狀態(tài)引擎的超媒體),在資源的表達(dá)中包含了鏈接信息,客戶端可以在鏈接信息中發(fā)現(xiàn)可以執(zhí)行的操作
HATEOAS是什么?
HATEOAS代表“超媒體是應(yīng)用程序狀態(tài)的引擎”
從前言我們已經(jīng)可以清楚知道,使用HATEOAS約束是REST風(fēng)格中成熟度最高的,也是官方推薦的一種方式,沒(méi)使用HATEOAS的項(xiàng)目,服務(wù)端和客戶端是耦合的,客戶端只能通過(guò)相關(guān)文檔來(lái)知道服務(wù)端做了什么修改,使用HATEOAS約束的REST服務(wù),服務(wù)端修改接口信息后,客戶端可以通過(guò)服務(wù)器提供的資源的表達(dá)來(lái)智能地發(fā)現(xiàn)可以執(zhí)行的操作,客戶端不需要做啥修改,因?yàn)橘Y源信息是會(huì)動(dòng)態(tài)改變的
在Spring的官網(wǎng),已經(jīng)有提供這個(gè)項(xiàng)目的相關(guān)文檔,鏈接:https://spring.io/projects/spring-hateoas
SpringBoot HATEOAS
SpringBoot中也有集成HATEOAS,本博客介紹一下如何使用
工具準(zhǔn)備:
- 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>
因?yàn)槭且獙?xiě)個(gè)web簡(jiǎn)單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>
實(shí)體類(lèi)實(shí)現(xiàn)ResourceSupport
Model類(lèi)實(shí)現(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("請(qǐng)求參數(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(); }
實(shí)例代碼:github鏈接下載
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
IntelliJ IDEA 如何徹底刪除項(xiàng)目的步驟
本篇文章主要介紹了IntelliJ IDEA 如何徹底刪除項(xiàng)目的步驟,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-11-11springMVC配置環(huán)境實(shí)現(xiàn)文件上傳和下載
這篇文章主要為大家詳細(xì)介紹了springMVC配置環(huán)境實(shí)現(xiàn)文件上傳和下載的相關(guān)資料,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-05-05spring boot中使用@Async實(shí)現(xiàn)異步調(diào)用任務(wù)
本篇文章主要介紹了spring boot中使用@Async實(shí)現(xiàn)異步調(diào)用任務(wù),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-02-02Springboot實(shí)現(xiàn)過(guò)濾器的兩種方式
今天通過(guò)本文給大家分享Springboot實(shí)現(xiàn)過(guò)濾器的兩種方式,第一種是spring容器注冊(cè)filter,第二種方式是通過(guò)@WebFilter 注解來(lái)配置,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友參考下吧2023-10-10Java多線程中的CountDownLatch詳細(xì)解讀
這篇文章主要介紹了Java多線程中的CountDownLatch詳細(xì)解讀,一個(gè)同步輔助類(lèi),在完成一組正在其他線程中執(zhí)行的操作之前,它允許一個(gè)或多個(gè)線程一直等待,用給定的計(jì)數(shù) 初始化 CountDownLatch,需要的朋友可以參考下2023-11-11詳解Java ES多節(jié)點(diǎn)任務(wù)的高效分發(fā)與收集實(shí)現(xiàn)
ElasticSearch 是一個(gè)高可用開(kāi)源全文檢索和分析組件。提供存儲(chǔ)服務(wù),搜索服務(wù),大數(shù)據(jù)準(zhǔn)實(shí)時(shí)分析等。一般用于提供一些提供復(fù)雜搜索的應(yīng)用2021-06-06Java實(shí)現(xiàn)BP神經(jīng)網(wǎng)絡(luò)MNIST手寫(xiě)數(shù)字識(shí)別的示例詳解
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)BP神經(jīng)網(wǎng)絡(luò)MNIST手寫(xiě)數(shù)字識(shí)別的相關(guān)方法,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起了解一下2023-01-01