【发布时间】:2023-01-01 22:49:39
【问题描述】:
这是我的控制器:
@RestController
public class TransactionController {
@Autowired
TransactionService transactionService;
@PostMapping("/add-transaction")
public Transaction addTransaction(@RequestBody CustomerTransactions customerTransactions) {
System.out.println("Check in controller");
System.out.println(customerTransactions);
return transactionService.addTransaction(customerTransactions);
}
}
但 customerTransactions 为空。我通过以下方式在 Postman 中发送 JSON 请求:
{
"Customer":{
"custName": "Bikash Bhattarai",
"email": "bksbhattarai@gmail.com",
"phone": 9818122018
},
"Transaction":{
"custId": "446bdfd3-3d09-43e7-83df-1883f967d296",
"transdate": "03-04-2023",
"transamount": 2000
}
}
这是控制台输出:
Check in controller
CustomerTransactions [customer=null, transaction=null]
here we go
CustomerTransactions [customer=null, transaction=null]
null
2023-01-01T20:07:08.514+05:45 ERROR 14976 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed: java.lang.NullPointerException: Cannot invoke "com.rewardsystem.AugustStore.entities.Customer.getCustId()" because "customer" is null] with root cause
【问题讨论】:
-
提供
CustomerTransactions类的结构。从@RequestBody 获取null意味着无法完成从 JSON 到您的 POJO 类的对象映射,因为 JSON 的结构和您的 POJO 类CustomerTransactions不匹配。 -
public class CustomerTransactions { private Customer 客户;私人交易交易; public CustomerTransactions() { super(); // TODO 自动生成的构造函数存根 }
-
@BibekBhattarai 请edit 帖子并将此代码添加到帖子中。它在 cmets 中不可读。