必需的 T - 在使用 JPA 的 REST 控制器上提供无效
我正在尝试实现 REST 控制器来完成删除操作并在 REST 控制器上收到错误。
我的 BirdService
public interface BirdService {
Bird create(Bird bird);
Collection<Bird> list();
Bird get(Long id);
Bird update(Bird bird);
void delete(Long id);
}
By BirdServiceImpl
public class BirdServiceImpl implements BirdService {
private final BirdRepository birdRepository;
// Other CRUDS deleted
@Override
public void delete(Long id) {
log.info("About to delete bird : {}", id);
Bird bird = new Bird();
if (birdRepository.existsById(id)) {
birdRepository.deleteById(id);
}
}
}
和 REST 控制器:
@DeleteMapping("/delete/{id}")
public ResponseEntity deleteKiwi(@PathVariable("id") Long id) {
return ResponseEntity.ok()
.header("Custom-Header", "foo")
.body(birdService.delete(id));
}
但是我的 IDE 警告我这一点:
我理解这意味着某处存在错误类型,但 void 是派生 JPA 删除方法的响应类型 -那么为什么我得到这个了吗?我该如何解决它?
I am trying to implement a REST controller to complete a delete operation and getting a error on the REST controller.
My BirdService
public interface BirdService {
Bird create(Bird bird);
Collection<Bird> list();
Bird get(Long id);
Bird update(Bird bird);
void delete(Long id);
}
By BirdServiceImpl
public class BirdServiceImpl implements BirdService {
private final BirdRepository birdRepository;
// Other CRUDS deleted
@Override
public void delete(Long id) {
log.info("About to delete bird : {}", id);
Bird bird = new Bird();
if (birdRepository.existsById(id)) {
birdRepository.deleteById(id);
}
}
}
And the REST controller:
@DeleteMapping("/delete/{id}")
public ResponseEntity deleteKiwi(@PathVariable("id") Long id) {
return ResponseEntity.ok()
.header("Custom-Header", "foo")
.body(birdService.delete(id));
}
My IDE however is warning me about this:
I understand it means there is a wrong type somewhere, but void is the response type for the derived JPA delete method - so why am I getting this and how do I fix it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如评论中提到的Dawood所述,您正在尝试使用返回
void
的方法填充响应主体。我相信最佳实践是执行删除方法,然后返回200:As the Dawood mentioned in the comments, you're trying to populate a response body with a method that returns
void
. I believe best practice is to execute the delete method, and return 200: