我正在使用 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
时
出现错误
Fri May 22 17:56:37 IST 2015 There was an unexpected error (type=Not Found, status=404). No message available
注意:如果我将路径映射到 system-users
那么它工作正常,但是当我使用 /
时在诸如 system/users
的路径中,它会中断并给出错误。
最佳答案
tl;博士
不支持。
详细信息
一般来说:不要专注于设计 URL。根据定义,URI 的结构对于 REST 客户端来说一定不重要:
At no time whatsoever do the server or client software need to know or understand the meaning of a URI -- they merely act as a conduit through which the creator of a resource (a human naming authority) can associate representations with the semantics identified by the URI.
(来自 Roy Fielding - 建筑风格和 基于网络的软件架构设计- Section 6.2.4 )
如果您遵循该标准,您将负责选择关系名称、设计您公开的资源的表示等。
也就是说,目前不支持您正在寻找的内容。如果您认为值得添加此功能(由于上面给出的论点,我认为不值得),请随时在我们的 bug tracker 中开票。 .
由于 Spring MVC 映射的工作方式,尤其是奇异路径段和非奇异路径段的映射的组合可能会导致一些歧义,因此实现起来可能相当棘手。所有这些使得该请求不太可能得到跟进。
关于java - 如何自定义 Spring Data REST 以对存储库资源使用多段路径?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41306263/