【发布时间】:2018-05-28 07:46:02
【问题描述】:
我有一个使用 Spring-data-rest 生成我的 REST 接口的 Spring Boot 项目,我正在尝试允许对嵌套资源进行分页。我遵循了这个workaround 并坚持实施 findBy 查询。
我有以下设备实体:
@Entity
@Table(name = "devices")
public class Device extends ResourceSupport {
...
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_id")
private User user;
}
我需要使用 userId 来查询它:
@RequestMapping(path = "/users/{id}/devices", method = RequestMethod.GET)
public ModelAndView getUserDevices(@PathVariable Integer id) {
return new ModelAndView("forward:/devices/search/findByUserId?userId=" + id);
}
所以我在我的 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);
}
但我在尝试向此端点发送请求时遇到以下异常:
Unable to locate Attribute with the the given name [id] on this ManagedType [com.example.User]
有没有人建议我做错了什么?
编辑: 谢谢你的回复,我把方法改成了
Page<Device> findByUser_UserId(@Param("user") User user, Pageable pageable);
我的用户实体包含一个userId 字段。
但是,现在我遇到了以下异常:
{
"cause": {
"cause": {
"cause": null,
"message": "Cannot resolve URI 1. Is it local or remote? Only local URIs are resolvable."
},
"message": "Failed to convert from type [java.net.URI] to type [@org.springframework.data.repository.query.Param com.example.User] for value '1'; nested exception is java.lang.IllegalArgumentException: Cannot resolve URI 1. Is it local or remote? Only local URIs are resolvable."
},
"message": "Failed to convert 1 into me.originalsin.user.model.User!"
}
看来我用错了查询?
【问题讨论】:
标签: java spring-boot spring-data-rest