Spring Cloud REST Repositories

2024-01-11 15:06 更新

使用Spring Boot運(yùn)行時(shí),只需將此依賴(lài)項(xiàng)添加到pom文件即可將存儲(chǔ)庫(kù)公開(kāi)為REST服務(wù):

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

如果您希望配置參數(shù)(例如path),則可以使用@RepositoryRestResource批注:

@RepositoryRestResource(collectionResourceRel = "trades", path = "trades")
public interface TradeRepository extends SpannerRepository<Trade, String[]> {
}

例如,您可以使用curl http://<server>:<port>/trades檢索存儲(chǔ)庫(kù)中的所有Trade對(duì)象,也可以通過(guò)curl http://<server>:<port>/trades/<trader_id>,<trade_id>檢索任何特定交易。

在這種情況下,主鍵組件idtrader_id之間的分隔符在默認(rèn)情況下是逗號(hào),但是可以通過(guò)擴(kuò)展SpannerKeyIdConverter類(lèi)將其配置為在鍵值中找不到的任何字符串:

@Component
class MySpecialIdConverter extends SpannerKeyIdConverter {

    @Override
    protected String getUrlIdSeparator() {
        return ":";
    }
}

您也可以使用curl -XPOST -H"Content-Type: application/json" -d@test.json http://<server>:<port>/trades/進(jìn)行交易,其中文件test.json包含Trade對(duì)象的JSON表示形式。

以上內(nèi)容是否對(duì)您有幫助:
在線(xiàn)筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)