必需的 T - 在使用 JPA 的 REST 控制器上提供无效

发布于 2025-01-19 05:24:40 字数 1186 浏览 0 评论 0原文

我正在尝试实现 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:

enter image description here

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

謸气贵蔟 2025-01-26 05:24:40

正如评论中提到的Dawood所述,您正在尝试使用返回void的方法填充响应主体。我相信最佳实践是执行删除方法,然后返回200:

@DeleteMapping("/delete/{id}")
public ResponseEntity deleteKiwi(@PathVariable("id") Long id) {
    birdService.delete(id);
    return new ResponseEntity<>(HttpStatus.OK);
}

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:

@DeleteMapping("/delete/{id}")
public ResponseEntity deleteKiwi(@PathVariable("id") Long id) {
    birdService.delete(id);
    return new ResponseEntity<>(HttpStatus.OK);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文