欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

怎么在SpringBoot中使用HATEOAS方法-創(chuàng)新互聯(lián)

怎么在SpringBoot中使用HATEOAS方法?相信很多沒有經(jīng)驗的人對此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個問題。

成都創(chuàng)新互聯(lián)公司基于分布式IDC數(shù)據(jù)中心構(gòu)建的平臺為眾多戶提供服務(wù)器托管 四川大帶寬租用 成都機柜租用 成都服務(wù)器租用。

REST風格簡介

介紹HATEOAS之前先簡單介紹一下REST,REST 是 Representational state transfer 的縮寫,翻譯過來的意思是表達性狀態(tài)轉(zhuǎn)換。REST是一種架構(gòu)的風格

Richardson Maturity Model

Richardson 提出了REST一種 成熟度模型,我們稱之為Richardson Maturity Model,這種模式將REST按照成熟度劃分為4個等級

  • Level0:使用HTTP作為WEB服務(wù)的傳輸方式,以REST樣式公開SOAP Web服務(wù)

  • Level1:使用適當?shù)腢RI(使用名詞)公開資源,這種方式提出了資源的概念

  • Level2:資源使用正確的URI + HTTP方法,比如更新用戶就用put方式,查詢用get方式

  • Level3:使用HATEOAS(作為應用程序狀態(tài)引擎的超媒體),在資源的表達中包含了鏈接信息,客戶端可以在鏈接信息中發(fā)現(xiàn)可以執(zhí)行的操作

HATEOAS是什么?

HATEOAS代表“超媒體是應用程序狀態(tài)的引擎”

從前言我們已經(jīng)可以清楚知道,使用HATEOAS約束是REST風格中成熟度最高的,也是官方推薦的一種方式,沒使用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();
  }

怎么在SpringBoot中使用HATEOAS方法

看完上述內(nèi)容,你們掌握怎么在SpringBoot中使用HATEOAS方法的方法了嗎?如果還想學到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)網(wǎng)站建設(shè)公司行業(yè)資訊頻道,感謝各位的閱讀!

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)建站chinadenli.net,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機、免備案服務(wù)器”等云主機租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。

文章標題:怎么在SpringBoot中使用HATEOAS方法-創(chuàng)新互聯(lián)
轉(zhuǎn)載注明:http://chinadenli.net/article20/geojo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站策劃外貿(mào)網(wǎng)站建設(shè)全網(wǎng)營銷推廣動態(tài)網(wǎng)站ChatGPT移動網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

h5響應式網(wǎng)站建設(shè)