我有一个名为User的@Entity.它有一组变更集如下:
@OneToMany(fetch=FetchType.LAZY, cascade=CascadeType.ALL, mappedBy="user")
private Set<Changeset> changesets = new HashSet<Changeset>();
Run Code Online (Sandbox Code Playgroud)
我有一个UserRepository:
@Repository
@RestResource(path = "users", rel = "users")
public interface UserRepository extends JpaRepository<User, Long>{ }
Run Code Online (Sandbox Code Playgroud)
还有一个ChangesetRepository:
@Repository
@RestResource(path = "changesets", rel = "changesets")
public interface ChangesetRepository extends JpaRepository<Changeset, Long> { }
Run Code Online (Sandbox Code Playgroud)
调用GET http://localhost:8080/changesets/
或http://localhost:8080/users/
产生分页响应.
如果我打开GET,http://localhost:8080/users/1/changesets
那么我将所有结果都放在一个数组中,并且不会发生分页.
有没有办法向Spring Data Rest表明我想通过其父用户访问它时以可分页的方式返回changesets集合?变更集集将快速增长,我宁愿不在单个页面中返回大量结果.
编辑:
正如Willie Wheeler所建议的那样,我将其添加到我的ChangesetRepository中以使其可搜索:
@RestResource(path = "byUser", rel = "byUser")
public Page<Changeset> findByUser(@Param("id") User user, Pageable p);
Run Code Online (Sandbox Code Playgroud)
我将关系保留为双向,但也可以通过使用更改集来隐藏用户更改集的链接@RestResource(exported=false)
.
旁注:似乎将关系设置为exported = false会隐藏链接,但实际上不会删除映射./ users/1/changesets未公布,但仍然有效.
spring hibernate spring-data spring-data-jpa spring-data-rest
我使用Spring Data Rest 2.1.1 Release和默认配置.考虑以下资源:
GET /communities/MyCommunity
{
"creationDate": "2014-07-16T06:22:37.153+0000",
"name": "GroupeSEB",
"_links": {
"self": {
"href": "http://localhost:8080/api/communities/GroupeSEB"
},
"posts": {
"href": "http://localhost:8080/api/communities/GroupeSEB/posts"
}
}
}
Run Code Online (Sandbox Code Playgroud)
当我得到"帖子"子资源时:
GET /communities/MyCommunity/posts
{
"_embedded": {
"posts": [
{
"creationDate": "2014-07-09T13:09:14.535+0000",
"id": "53bd3efae4b012818368c549",
"_links": {
"self": {
"href": "http://localhost:8080/api/posts/53bd3efae4b012818368c549"
}
}
}
]
}
}
Run Code Online (Sandbox Code Playgroud)
没有启用分页.由于我的父资源可以聚合大量帖子(其子资源),我如何为每个子资源启用分页?
我有一个使用Spring-data-rest生成REST接口的Spring Boot项目,并且我试图允许对嵌套资源进行分页。我遵循了此变通办法,并被困于实现findBy查询。
我具有以下设备实体:
@Entity
@Table(name = "devices")
public class Device extends ResourceSupport {
...
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_id")
private User user;
}
Run Code Online (Sandbox Code Playgroud)
我需要使用userId查询它:
@RequestMapping(path = "/users/{id}/devices", method = RequestMethod.GET)
public ModelAndView getUserDevices(@PathVariable Integer id) {
return new ModelAndView("forward:/devices/search/findByUserId?userId=" + id);
}
Run Code Online (Sandbox Code Playgroud)
因此,我在DeviceRepository中创建了以下方法:
@Repository
public interface DeviceRepository extends JpaRepository<Device, Long> {
@PreAuthorize("hasRole('ROLE_ADMIN') OR @securityCheck.check(#user, authentication)")
Page<Device> findByUserId(@Param("user") User user, Pageable pageable);
}
Run Code Online (Sandbox Code Playgroud)
但是在尝试向此端点发送请求时出现以下异常:
Unable to locate Attribute with the the given name [id] on this ManagedType [com.example.User] …
Run Code Online (Sandbox Code Playgroud) 我可以通过以下调用从Spring Data REST中检索一本书:GET/book/{id}
现在,如果我知道两本书的ID,我想立刻检索它们?电话应该是什么?我试过以下但是它给我的书不同于指定的书:
GET /book?ids=id1,id2
Run Code Online (Sandbox Code Playgroud)