500 Internal Server错误中的弹簧启动发布API

发布于 2025-02-10 02:57:17 字数 2004 浏览 0 评论 0 原文

我正在尝试在Spring Boot应用程序中创建帖子API,但它不起作用,我在代码中找不到问题,

这是 service

  @Transactional
    public Invoice saveInvoice(Invoice invoice) {
        Invoice newInvoice = invoiceRepository.save(invoice);
        return newInvoice;
    }

这是控制器

@PostMapping("/save")
public ResponseEntity<Invoice> addInvoice(@RequestBody InvoiceDTO invoice) {
    try {
        Invoice newInvoice = invoiceService
                .saveInvoice(new Invoice(invoice.getSerialNumber(), invoice.getStatus(), invoice.getCreatedDate()));
        return new ResponseEntity<>(newInvoice, HttpStatus.CREATED);
    } catch (Exception e) {
        return new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR);
    }
}

这是模型

@Entity
@Table(name = "invoice")
public class Invoice {

    @Id
    @GeneratedValue(strategy= GenerationType.IDENTITY)
    private int id;
    @Column(name = "serial_number")
    private long serialNumber;
    @Column(name = "status")
    private String status;
    @Column(name = "created_date")
    private Timestamp createdDate;
    @Column(name = "is_deleted")
    private boolean isDeleted;

    @ManyToOne
    @JoinColumn(name = "customer_id")
    private Customer customer;

    @ManyToOne
    @JoinColumn(name = "employee_id")
    private Employee employee;

    @OneToMany
    private Set<InvoiceHistory> invoiceHistories;

    @ManyToMany
    private Set<Item> items;

    public Invoice(long serialNumber, String status, Timestamp createdDate) {
        this.serialNumber = serialNumber;
        this.status = status;
        this.createdDate = createdDate;
        this.isDeleted = false;
    }
}

但是,当我在Postman中奔跑时,它的返回500内部服务器错误

更新错误消息:

,消息=无法调用 “ com.example.invoices.repository.iinvoicerepository.save(object)” 因为“ this.invoicerepository”为空,路径=/发票/保存}]

问题在哪里?

I'm trying to create a post API in a spring boot app but it's not working and i can't find the issue in the code

This is service:

  @Transactional
    public Invoice saveInvoice(Invoice invoice) {
        Invoice newInvoice = invoiceRepository.save(invoice);
        return newInvoice;
    }

This is the controller:

@PostMapping("/save")
public ResponseEntity<Invoice> addInvoice(@RequestBody InvoiceDTO invoice) {
    try {
        Invoice newInvoice = invoiceService
                .saveInvoice(new Invoice(invoice.getSerialNumber(), invoice.getStatus(), invoice.getCreatedDate()));
        return new ResponseEntity<>(newInvoice, HttpStatus.CREATED);
    } catch (Exception e) {
        return new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR);
    }
}

and this is the model :

@Entity
@Table(name = "invoice")
public class Invoice {

    @Id
    @GeneratedValue(strategy= GenerationType.IDENTITY)
    private int id;
    @Column(name = "serial_number")
    private long serialNumber;
    @Column(name = "status")
    private String status;
    @Column(name = "created_date")
    private Timestamp createdDate;
    @Column(name = "is_deleted")
    private boolean isDeleted;

    @ManyToOne
    @JoinColumn(name = "customer_id")
    private Customer customer;

    @ManyToOne
    @JoinColumn(name = "employee_id")
    private Employee employee;

    @OneToMany
    private Set<InvoiceHistory> invoiceHistories;

    @ManyToMany
    private Set<Item> items;

    public Invoice(long serialNumber, String status, Timestamp createdDate) {
        this.serialNumber = serialNumber;
        this.status = status;
        this.createdDate = createdDate;
        this.isDeleted = false;
    }
}

But when I am running in in postman its returning 500 Internal Server Error

Update error message :

, message=Cannot invoke
"com.example.invoices.repository.IInvoiceRepository.save(Object)"
because "this.invoiceRepository" is null, path=/invoice/save}]

where's the problem ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

戏剧牡丹亭 2025-02-17 02:57:17

对于您的服务类,您如何声明和创建 InvoIcerePository ?如果是这样的话:

private InvoiceRepository invoiceRepository;

那么您需要添加 @Autowired

@Autowired
private InvoiceRepository invoiceRepository;

如果您确实拥有 @Autowired ,请确保使用 @service /code>:

@Service
public class InvoiceService {...}

如果有的话,请确保您没有在控制器中使用 “ new” 创建 Invoiceservice 。而不是:

private InvoiceService invoiceService = new InvoiceService();

“ new” Ing 它在构造函数中,
使用 @Autowire

@Autowire
private InvoiceService invoiceService;

For your service class, how are you declaring and creating invoiceRepository? If it's like this:

private InvoiceRepository invoiceRepository;

Then you need to add an @Autowired:

@Autowired
private InvoiceRepository invoiceRepository;

If you do have it @Autowired, make sure your service class is annotated with @Service:

@Service
public class InvoiceService {...}

If you have that, make sure you aren't creating InvoiceService with "new" in your controller. Instead of:

private InvoiceService invoiceService = new InvoiceService();

or "new"ing it in a constructor,
use @Autowire

@Autowire
private InvoiceService invoiceService;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文