500 Internal Server错误中的弹簧启动发布API
我正在尝试在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”为空,路径=/发票/保存}]
问题在哪里?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于您的服务类,您如何声明和创建
InvoIcerePository
?如果是这样的话:那么您需要添加
@Autowired
:如果您确实拥有
@Autowired
,请确保使用@service
/code>:如果有的话,请确保您没有在控制器中使用
“ new”
创建Invoiceservice
。而不是:或
“ new” Ing
它在构造函数中,使用
@Autowire
For your service class, how are you declaring and creating
invoiceRepository
? If it's like this:Then you need to add an
@Autowired
:If you do have it
@Autowired
, make sure your service class is annotated with@Service
:If you have that, make sure you aren't creating
InvoiceService
with"new"
in your controller. Instead of:or
"new"ing
it in a constructor,use
@Autowire