带有下划线的Spring数据休息排序字段 [英] Spring data rest sorting fields with underscores
问题描述
我们在连接到 postgres 数据库的 PagingAndSortingRepository
之上使用了一个非常简单的 @RepositoryRestResource
设置.我们还配置了 spring.jackson.property-naming-strategy=SNAKE_CASE
以返回漂亮的 json.在我们开始分类之前,一切都很好.正如我们所发现的 - 排序要求我们提供实际的类字段名称(我们当然在骆驼情况下有):
We are using a very simple setup of @RepositoryRestResource
on top of a PagingAndSortingRepository
connected to a postgres database. Also we have configured spring.jackson.property-naming-strategy=SNAKE_CASE
to return pretty json. It was all fine and dandy until we started sorting. As we have discovered - sorting requires us to provide the actual class field names (which we of course have in camel case):
get("/thing?sort=dateCreated,desc")
当我们尝试对 javascript 友好时
And when we try to do javascript friendly
get("/thing?sort=date_created,desc")
它失败了,因为 jpa 试图用下划线分割参数.
it fails miserably because jpa tries to split the parameter by the underscore.
是否有一种简单的方法可以使路径参数的格式与我们在返回的 json 中的格式相同?
Is there a simple way to have the path params the same format as we have them in the json that we are returning?
推荐答案
这里有一个 bug - DATAREST-883.它被修复并发布.但是,由于回归(DATAREST-909),这已经在非常下一个版本.我在 Github 上问过他们 他们是否有计划再次拥有这个,因为这在过去也曾让我感到痛苦.我们将看看他们对此有何评论.
There is a bug for this - DATAREST-883. It was fixed and released. But then, due to regressions (DATAREST-909) this has been dropped in the very next release. I asked them on Github if they plan to have this again as this has bitten me in the past as well. We'll see what they have to say about this.
现在您可以:
- 就这样吧
- 使用骆驼案例属性名称
- 解决这个问题(例如,使用 Alan Haye 的回答)-恕我直言,这似乎很脆弱,但可能会在短期.
- leave it be
- go with the camel case property names
- work around this (e.g. go with Alan Haye's answer) - this seems fragile IMHO, but probably will do in a short term.
我测试过的最近 spring-boot 版本中该功能的状态:
The status of the feature in the recent spring-boot versions I've tested with:
- 1.4.0(spring-data-rest 2.5.2):尚未实现
- 1.4.1(spring-data-rest 2.5.3):有效 -> 代码
- 1.4.2(spring-data-rest 2.5.5):删除
- 1.4.0 (spring-data-rest 2.5.2): not yet implemented
- 1.4.1 (spring-data-rest 2.5.3): works -> code
- 1.4.2 (spring-data-rest 2.5.5): dropped
这篇关于带有下划线的Spring数据休息排序字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!