总的来说,我对 Spring Data Rest 和 Spring 还很陌生。 我将 Spring Data Rest 与 Spring Data Jpa 和 Mysql 数据库结合使用。
我找不到如何更改在 URL 中用作 Id 的实体属性。 这基本上是我想要实现的目标:
我有以下实体:Plant(id,code,name,...)
其实我是通过
获取植物资源的GET /plants/:id
我想使用以下方式访问此资源项:
GET /plants/:code
Plant.code
属性不是实体的官方 id
但它是唯一的并且对 API 的最终用户更有意义。
我已通过文档和 Google/SO 进行了搜索,但未找到任何相关答案。
如果答案很明显,请原谅
最佳答案
您可以像这样添加一个搜索端点,它会返回一个匹配的资源:
GET /plants/search/findByCode?code=:code
您可以通过将其添加到您的存储库界面来完成此操作:
Plant findByCode(@Param("code") String code);
关于Spring Data Rest - 更改项目资源 url 中使用的实体属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32268402/