@RepositoryRestResource 和 @RepositoryRestController 浅析
admin
2024-02-17 04:26:11
0次

@RepositoryRestResource 和 @RepositoryRestController 浅析

Spring Data REST 暴露了一个集合资源,该资源以导出的存储库所处理的域类的非大写复数命名。
资源的名称和路径都可以通过在版本库接口上使用@RepositoryRestResource来定制。
也就是说,@RepositoryRestResource注解是可选的,用于定制REST端点。

例如,有一个存储库Station,如果不使用@RepositoryRestResource注解,

public interface StationRepository extends MongoRepository {}

Spring Data REST 暴露的api端点是/stations,
如果使用@RepositoryRestResource注解自定义,

@RepositoryRestResource(collectionResourceRel = "station", path = "station")
public interface StationRepository extends MongoRepository {}

Spring Data REST 暴露的api端点是我们自定义的/station。

@RepositoryRestController注解可以帮助我们客户化Spring Data REST 暴露的api端点的功能。
例如,api端点/station/{id}默认返回这个Station的真实数据,

curl -X GET http://localhost:8181/station/2008
{"name" : "Little West St & 1 Pl","region_id" : "71","lon" : -74.01677685,"lat" : 40.70569254,"eightd_has_key_dispenser" : false,"legacy_id" : "2008","rental_methods" : [ "KEY", "CREDITCARD" ],"external_id" : "66dc90d5-0aca-11e7-82f6-3863bb44ef7c","capacity" : 35,"short_name" : "5001.08","electric_bike_surcharge_waiver" : false,"station_type" : "classic","eightd_station_services" : [ ],"has_kiosk" : true,"rental_uris" : {"android" : "https://bkn.lft.to/lastmile_qr_scan","ios" : "https://bkn.lft.to/lastmile_qr_scan"},"_links" : {"self" : {"href" : "http://localhost:8181/station/2008"},"station" : {"href" : "http://localhost:8181/station/2008"}}
}

我们想客户化api端点/station/{id}的功能,通过@RepositoryRestController注解实现自定义的功能。需要注意的是,我们想覆盖哪些Spring Data Rest默认的api端点的功能,RequestMapping的方法类型和端点必须和Spring Data Rest默认暴露的方法类型和端点一致。

@RepositoryRestController
//@RestController
public class StationController {@ResourceStationRepository stationRepository;@RequestMapping(method = RequestMethod.GET, value = "/station/{id}")public @ResponseBody HttpEntity getStation(@PathVariable String id) {Station station = Station.builder().id(id).build();return ResponseEntity.ok(station);}
}

实现后的效果,

curl -X GET http://localhost:8181/station/2008
{"id" : "2008","name" : null,"region_id" : null,"lon" : 0.0,"lat" : 0.0,"eightd_has_key_dispenser" : false,"legacy_id" : null,"rental_methods" : null,"external_id" : null,"capacity" : 0,"short_name" : null,"electric_bike_surcharge_waiver" : false,"station_type" : null,"eightd_station_services" : null,"has_kiosk" : false,"rental_uris" : null
}

完结!

相关内容

热门资讯

用镜头定格限时美景!东台黄海森... 当深秋的萧瑟与初冬的凛冽交织,黄海森林公园的水杉之约如期而至。近日,黄海森林公园万亩水杉林迎来最佳观...
一树金黄!千年银杏迎来最佳观赏... 眼下,位于河南省南阳市桐柏县淮源镇的一棵千年银杏树迎来最佳观赏期。满目金黄,美不胜收,金黄色的银杏树...
零落一身秋——上海秋色正好 穿行在上海的街巷与公园,城市的秋慢慢铺展开来。金黄银杏、绯红枫叶,在高楼与林荫之间悄悄上色,风掠过枝...
在寺沟想起柳青 本文转自:人民日报白 描   初冬时节,陕北落了第一场雪。雪下得舒缓温润,静静铺洒在广袤的黄土高原上...
红墙银杏“绝美机位”!南京鼓楼... 现代快报讯(记者 卢河燕/文 赵杰/摄)12月上旬,南京鼓楼公园迎来秋冬限定的诗意盛景。朱红古墙之下...
美丽北京|念坛公园芦荻随风,冬... 北京大兴念坛公园的湖畔,成片芦荻迎来最佳观赏期。阳光下,蓬松洁白的花穗随风摇曳,临水铺展,为冬日湖面...
这处风景名胜,也是北京重要水利...   新华社北京12月4日电(记者田晨旭)近日,水利部公布第一批重要水利遗产名单,北京什刹海及北海与都...
红枫似火映南京 暖心宜居“房帮... 12月的南京栖霞山,2026年“江山入戏”红枫系列活动正在此地开展,美艳如火的红枫、各式各样的精彩活...
天人境里“生生之道”   喻军 一广西胜迹殊繁,然而耳目所接,纷纭万状,终难尽述其详。倘只取桂林、阳朔各一景或一物加以概括,印象最深...
寻文脉、润童心,孙子文化园秋冬... 齐鲁晚报·齐鲁壹点记者 乔显佳时下旅游市场进入冬季淡季,各大景区客流普遍回落。刚刚在山东省东营市广饶...