java - 如何自定义 Spring Data REST 以对存储库资源使用多段路径?

标签 java spring spring-mvc spring-boot spring-data-rest

我正在使用 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/

相关文章:

java - Spring:当收到来自 Postman 的请求时,BasicHttp 无法与 jdbcAuthentication 一起使用

java - 在带有 tomcat 8 的 spring mvc web 应用程序中尝试使用 quartz 2.2 调度程序调用服务类方法时出现空指针异常

java - 带有 Jetty 的 Spring MVC - app-servlet.xml 文件中应该包含什么?

用于嵌套 ZIP 文件处理的 Java 实用程序库

java - Spring Bean/存储库的 ID

spring - 结合 Spring、Quartz 调度和 Hazelcast

java - 如何在Spring MVC中创建属性文件的List对象?

java - 如何在继续当前线程之前等待事件发生?

java - 使用 android 的 XML "onClick"属性将值传递给方法

java - 如何将“&”符号放入 Word 2007 .docx XML 文件中?