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

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

问题描述

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

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 /< component_name> /< model_collection_name>

例如

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
Found,status = 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



不支持。

tl;dr

Not supported.

一般来说:不要专注于设计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 - 建筑风格和
基于网络的软件架构设计 - 第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天全站免登陆