覆盖 RestController 方法后,我得到:org.springframework.dao.InvalidDataAccessApiUsageException: The given id must not be null
After overriding RestController method I get: org.springframework.dao.InvalidDataAccessApiUsageException: The given id must not be null
我想将 spring-hateoas 添加到我的投资组合项目中并使其完整 restful。这意味着我需要覆盖 AbstractResource class 给出的方法(这是我的@RestController 的基础 class)来根据我的需要编辑它,当我这样做时我得到 org.springframework.dao.InvalidDataAccessApiUsageException:给定的 ID 不能为空!
我的项目如下所示:
- 服务包
public interface AbstractService<ENTITY extends AbstractEntity> {
ENTITY update(ENTITY entity);
EntityModel<ENTITY> getById(Long id);
ENTITY save(ENTITY entity);
void delete(Long id);
Collection<ENTITY> getAll();
}
public abstract class AbstractServiceImpl<ENTITY extends AbstractEntity> implements AbstractService<ENTITY> {
protected abstract JpaRepository<ENTITY, Long> getRepository();
@Override
public Collection<ENTITY> getAll() {
return getRepository().findAll();
}
@Override
public EntityModel<ENTITY> getById(Long id) {
return EntityModel.of(getRepository().getOne(id));
}
}
@Service
public class UserServiceImpl extends AbstractServiceImpl<User> implements UserService {
private final UserRepository userRepository;
@Override
protected JpaRepository<User, Long> getRepository() {
return userRepository;
}
}
- 资源包
public abstract class AbstractResource<ENTITY extends AbstractEntity>{
public abstract AbstractService<ENTITY> getService();
@GetMapping(value = "/{id}", produces = {"application/hal+json"})
public EntityModel<ENTITY> getById(@PathVariable("id") Long id) {
return getService().getById(id);
}
@GetMapping
public Collection<ENTITY> getAll() {
return getService().getAll();
}
}
当我这样离开时 - 一切正常 - 我得到了预期的回复
@RestController
@RequestMapping("/api/users")
public class UserResource extends AbstractResource<User> {
@Autowired
private final UserService userService;
@Override
public AbstractService<User> getService() {
return userService;
}
}
当我尝试覆盖此方法时发生错误
@RestController
@RequestMapping("/api/users")
public class UserResource extends AbstractResource<User> {
@Autowired
private final UserService userService;
@Override
public AbstractService<User> getService() {
return userService;
}
@Override
public EntityModel<User> getById(Long id) {
return userService.getById(id);
or
return super.getById(id);
}
}
UserResource
覆盖AbstractResource.getById(Long)
后,虽然继承了注解@GetMapping(value = "/{id}", produces = {"application/hal+json"})
,但没有继承注解@PathVariable("id")
。将注释 @PathVariable("id")
放在 UserResource.getById(Long)
内应该可以解决您的问题。
强烈建议在Github中放置一个可运行的示例项目(包括请求URL和测试数据)以重现您遇到的错误。否则很难查明原因和验证。
我想将 spring-hateoas 添加到我的投资组合项目中并使其完整 restful。这意味着我需要覆盖 AbstractResource class 给出的方法(这是我的@RestController 的基础 class)来根据我的需要编辑它,当我这样做时我得到 org.springframework.dao.InvalidDataAccessApiUsageException:给定的 ID 不能为空!
我的项目如下所示:
- 服务包
public interface AbstractService<ENTITY extends AbstractEntity> {
ENTITY update(ENTITY entity);
EntityModel<ENTITY> getById(Long id);
ENTITY save(ENTITY entity);
void delete(Long id);
Collection<ENTITY> getAll();
}
public abstract class AbstractServiceImpl<ENTITY extends AbstractEntity> implements AbstractService<ENTITY> {
protected abstract JpaRepository<ENTITY, Long> getRepository();
@Override
public Collection<ENTITY> getAll() {
return getRepository().findAll();
}
@Override
public EntityModel<ENTITY> getById(Long id) {
return EntityModel.of(getRepository().getOne(id));
}
}
@Service
public class UserServiceImpl extends AbstractServiceImpl<User> implements UserService {
private final UserRepository userRepository;
@Override
protected JpaRepository<User, Long> getRepository() {
return userRepository;
}
}
- 资源包
public abstract class AbstractResource<ENTITY extends AbstractEntity>{
public abstract AbstractService<ENTITY> getService();
@GetMapping(value = "/{id}", produces = {"application/hal+json"})
public EntityModel<ENTITY> getById(@PathVariable("id") Long id) {
return getService().getById(id);
}
@GetMapping
public Collection<ENTITY> getAll() {
return getService().getAll();
}
}
当我这样离开时 - 一切正常 - 我得到了预期的回复
@RestController
@RequestMapping("/api/users")
public class UserResource extends AbstractResource<User> {
@Autowired
private final UserService userService;
@Override
public AbstractService<User> getService() {
return userService;
}
}
当我尝试覆盖此方法时发生错误
@RestController
@RequestMapping("/api/users")
public class UserResource extends AbstractResource<User> {
@Autowired
private final UserService userService;
@Override
public AbstractService<User> getService() {
return userService;
}
@Override
public EntityModel<User> getById(Long id) {
return userService.getById(id);
or
return super.getById(id);
}
}
UserResource
覆盖AbstractResource.getById(Long)
后,虽然继承了注解@GetMapping(value = "/{id}", produces = {"application/hal+json"})
,但没有继承注解@PathVariable("id")
。将注释 @PathVariable("id")
放在 UserResource.getById(Long)
内应该可以解决您的问题。
强烈建议在Github中放置一个可运行的示例项目(包括请求URL和测试数据)以重现您遇到的错误。否则很难查明原因和验证。