gpt4 book ai didi

spring - JsonMappingException : Can not construct instance of

转载 作者:行者123 更新时间:2023-12-01 04:43:51 25 4
gpt4 key购买 nike

我有一个实体,其中两列引用其他表中的同一列。基本上,交易取决于帐户:创建新交易时,将资金从一个帐户发送到另一个帐户。

帐户:

@Entity
@Table(name = "accounts")
public class Account implements java.io.Serializable {

private static final long serialVersionUID = 2612578813518671670L;

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "idaccount", unique = true, nullable = false)
private Long idaccount;

@Column(name = "name", length = 50)
private String name;

@NotNull
@ManyToOne
@JoinColumn(name = "iduser")
private User user;

...

交易:
@Entity
@Table(name = "transactions")
public class Transaction {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "idtransaction", unique = true, nullable = false)
private Long idtransaction;

private BigDecimal amount;

@NotNull
@ManyToOne
@JoinColumn(name = "SOURCE_ACCOUNT")
private Account sourceAccount;

@NotNull
@ManyToOne
@JoinColumn(name = "TARGET_ACCOUNT")
private Account targetAccount;

...

事务 Controller
@CrossOrigin
@RestController
@RequestMapping("/transaction")
public class TransactionController {

@Autowired
TransactionService transactionService;

@RequestMapping(method = RequestMethod.POST)
public ResponseEntity<Transaction> addTransaction(@RequestBody Transaction Transaction) {
transactionService.save(Transaction);

return new ResponseEntity<Transaction>(Transaction, HttpStatus.CREATED);
}

...

如果我尝试发布以创建交易(当然我已经创建了帐户):
{
"amount": 111,
"sourceAccount": 1,
"targetAccount": 2
}

我得到:
Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: Could not read document: Can not construct instance of com.mycompany.basicbank.model.Account: no int/Int-argument constructor/factory method to deserialize from Number value (1)
at [Source: java.io.PushbackInputStream@63447acf; line: 3, column: 18] (through reference chain: com.mycompany.basicbank.model.Transaction["sourceAccount"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of com.mycompany.basicbank.model.Account: no int/Int-argument constructor/factory method to deserialize from Number value (1)
at [Source: java.io.PushbackInputStream@63447acf; line: 3, column: 18] (through reference chain: com.mycompany.basicbank.model.Transaction["sourceAccount"])

所以我的问题是:我应该检查什么才能修复“无法构造 com.livingit.basicbank.model.Account 的实例:没有从 Number 反序列化的 int/Int-argument 构造函数/工厂方法”?

最佳答案

问题是您发送的 json 与 Transaction 不完全匹配类(class)。因此你会看到错误。
但是您要实现的是一个有效的场景并且可以完成。

一些选项。

  • 创建一个与 json 匹配的新类(不是事务)。喜欢
    class TransactionClient {
    BigDecimal amount,
    Long sourceAccount,
    Long targetAccount
    }

  • 在后端( Controller 或一些服务中),您可以使用此 sourceAccount 和 targetAccount 从数据库中获取帐户,并使用此对象创建一个事务对象并保存。
  • 从前端调用后端获取这些源和目标帐户的 Accounts(json),然后像这样使用 json 调用您的事务端点
    {
    "amount": 111,
    "sourceAccount": {
    "idaccount" :123123,
    ..... // All the Non Null fields
    },
    "targetAccount": {
    "idaccount" :45554,
    ..... // All the Non Null fields
    },
    }
  • 关于spring - JsonMappingException : Can not construct instance of,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48607503/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com