我有一个使用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;
}
我需要使用用户ID作为查询条件:
@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!"
}
所以看来我错误地使用了查询?