反序列化 BankAccount 对象时条纹 InvalidDefinitionException - Java

问题描述

简而言之,我有一个 Stripe bank account object,我正试图返回给客户端/前端。

HashMap<String,Object> response = new HashMap<String,Object>();
HashMap<String,Object> payload = new HashMap<String,Object> retrieveParams = new HashMap<>();

List<String> expandList = new ArrayList<>();

expandList.add("sources");
retrieveParams.put("expand",expandList);

customer = Customer.retrieve("cus_JHnmRG1q0aI3wt",retrieveParams,null);
         
BankAccount bankaccount = (BankAccount) customer.getSources().retrieve(customer.getMetadata().get("source"));
                 

payload.put("payment",bankaccount);   /*** Creates Error***/


response.put("payload",payload);         

return new ResponseEntity<>(response,HttpStatus.ACCEPTED);         

如何将这个 bankaccount 对象发送回前端,可能是 Hashmap 或其他 JSON 格式的对象。

我不断收到以下错误

Type deFinition error: [simple type,class com.stripe.net.StripeResponse]; nested exception is
com.fasterxml.jackson.databind.exc.InvalidDeFinitionException: No serializer found for class 
com.stripe.net.StripeResponse and no properties discovered to create BeanSerializer (to avoid exception,disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: java.util.HashMap["payload"]->
java.util.HashMap["payment"]->com.stripe.model.BankAccount["lastResponse"])]

解决方法

我建议改用 ID,但应该可以从任何条纹对象 (source) 中获取 JSON 对象,然后您可以根据需要对其进行序列化/解析。

,

我可以通过像这样附加 toJson() 来解决这个问题:

payload.put("payment",bankaccount.toJson());

这样做的缺点是前端需要额外的 JSON.parse()