我正在使用 Spring Data JPA 和 Spring Data REST 开发基于组件的 CRUD 应用程序。我有几个组件。例如,系统组件具有User
模型和UserRepository
. 组件由包名称区分。喜欢com.example.app.<component_name>
所以为了让我的 REST API 看起来更简洁,我需要实现如下的 API URL。
host:8080/<component_name>/<model_collection_name>
例如
host:8080/system/users
我在我的存储库中执行了以下操作
@RepositoryRestResource(collectionResourceRel = "users", path = "system/users")
public interface UserRepository extends PagingAndSortingRepository<User, Long> {
...
}
这会生成以下内容,当我转到 http://localhost:8080
{
"_links": {
"users": {
"href": "http://localhost:8080/system/users{?page,size,sort}",
"templated": true
},
...
但是当我去 http://localhost:8080/system/users
它给出了一个错误
5 月 22 日星期五 17:56:37 IST 2015 出现意外错误(类型 = 未找到,状态 = 404)。没有可用的消息
注意:如果我将路径映射到system-users
然后它工作正常,但是当我/
在路径中使用 a时,例如system/users
,它会中断并给出错误。
tl;博士
不支持。
细节
一般来说:不要专注于设计 URL。根据定义,URI 的结构对于 REST 客户端必须无关紧要:
(来自 Roy Fielding - Architectural Styles and the Design of Network-based Software Architectures - Section 6.2.4)
如果您遵循该标准,您就可以选择关系名称,为您公开的资源设计表示等。
也就是说,目前不支持您要查找的内容。如果您认为值得添加此功能(我认为不值得,因为上面给出的论点),请随时在我们的错误跟踪器中打开一张票。
由于 Spring MVC 映射的工作方式,特别是奇异路径段和非奇异路径段的映射组合可能会导致一些歧义,因此实现起来可能非常棘手。所有这些都使得这个请求不太可能得到跟进。