如何自定义 Spring Data REST 以使用存储库资源的多段路径? [英] How to customize Spring Data REST to use a multi-segment path for a repository resource?

查看:25
本文介绍了如何自定义 Spring Data REST 以使用存储库资源的多段路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Spring Data JPA 和 Spring Data REST 开发基于组件的 CRUD 应用程序.我有几个组件.例如,system 组件具有 User 模型和 UserRepository.组件由包名称区分.像 com.example.app.

I'm developing a component based CRUD application using Spring Data JPA and Spring Data REST. I have several components. For example system component has the User model and the UserRepository. Components are differenciated by the package names. like com.example.app.<component_name>

为了让我的 REST API 看起来更简洁,我需要实现如下 API URL.

So to make my REST API look cleaner, I need to implement the API URL as below.

host:8080//

例如

host:8080/system/users

我在我的仓库中做了以下事情

I did the following in my repository

@RepositoryRestResource(collectionResourceRel = "users", path = "system/users")
public interface UserRepository extends PagingAndSortingRepository<User, Long> {
   ...
}

这会生成以下内容,当我转到 http://localhost:8080

this generates the following, when I goto 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 出现意外错误(type=Not已找到,状态=404).没有可用的消息

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 之类的路径,它会中断并给出错误.

NOTE : If I mapped the path to system-users then it works fine, but when I use a / in the path such as system/users, it breaks and gives the error.

推荐答案

tl;dr

不支持.

一般来说:不要专注于设计 URL.根据定义,URI 的结构必须与 REST 客户端无关:

Generally speaking: don't focus on designing URLs. The structure of a URI must not matter to a REST client by definition:

服务器或客户端软件在任何时候都不需要知道或理解 URI 的含义——它们只是作为一个渠道,资源的创建者(人类命名机构)可以通过该渠道将表示与语义相关联由 URI 标识.

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 - Architectural Styles and基于网络的软件架构设计 - 第 6.2.4 节)

(from Roy Fielding - Architectural Styles and the Design of Network-based Software Architectures - Section 6.2.4)

如果您遵循该标准,您就可以选择关系名称、为您公开的资源设计表示等.

If you follow that criteria, you take care of choosing relation names, design the representations for the resources you expose etc.

也就是说,目前不支持您要查找的内容.如果您认为值得添加此功能(我认为不值得,因为上面给出的论点),请随时在我们的 中打开一张票错误跟踪器.

That said, what you're looking for is currently not supported. If you think it's worthwhile adding this feature (I think it's not, due to the argument given above), feel free to open a ticket in our bug tracker.

由于 Spring MVC 映射的工作方式,实现起来可能非常棘手,尤其是奇异路径段和非奇异路径段的映射组合可能会导致一些歧义.所有这些都使得该请求不太可能得到跟进.

It's probably quite tricky to implement due to the way Spring MVC mapping works and especially the combination of mapping of singular path segments and non-singular ones could cause some ambiguities. All of that makes it rather unlikely this request will get followed up on.

这篇关于如何自定义 Spring Data REST 以使用存储库资源的多段路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆