无法解析我的Json对象,该对象在Spring Cloud流中通过绑定器接收

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了无法解析我的Json对象,该对象在Spring Cloud流中通过绑定器接收相关的知识,希望对你有一定的参考价值。

我对Json转换器有问题,当我尝试收听该对象并将其转换为Java对象时,正在使用springcloud云流在Rabbitmq中发送一个Json对象。

这里是我收到物品的地方:


import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.Ebanking.transactionservice.entities.Transaction;
import org.Ebanking.transactionservice.repositories.TransactionRepository;
import org.Ebanking.transactionservice.services.TransactionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.annotation.StreamListener;
import org.springframework.cloud.stream.messaging.Sink;

@EnableBinding(Sink.class)
public class TransactionServiceImpl implements TransactionService 

    @Autowired
    TransactionRepository transactionRepository;


    @Override
    @StreamListener(target = Sink.INPUT)
    public void listen(String payload) throws JsonProcessingException 
        ObjectMapper mapper = new ObjectMapper();
        Transaction transaction = mapper.readValue(payload,Transaction.class);
        transactionRepository.save(transaction);
        System.out.println("Transaction registered Successfully");
    

这里是我发送对象的地方:

    @StreamListener(target = Processor.INPUT)
    @SendTo(Processor.OUTPUT)
    public String processTransaction(Transaction transaction) throws JsonProcessingException 
        ObjectMapper mapper = new ObjectMapper();
        Long idDebi = transaction.getAccountId();
        Long idCred = transaction.getAccountIdDestination();
        Double amount = transaction.getAmount();
        String str = mapper.writeValueAsString(transaction);
        if (debitAccount(idDebi, amount).equals(true) && creditAccount(idCred, amount).equals(true)) 
            processor.output().send(MessageBuilder.withPayload(str).setHeader("treatedTran","treatment").build());
            return "transaction treated successfully";
        
        else return "transaction failed";
     ```
答案

您不发送Transaction对象;您只是在发送字符串

public String processTransaction(
        ...
        return "transaction treated successfully";
    
    else return "transaction failed";

以上是关于无法解析我的Json对象,该对象在Spring Cloud流中通过绑定器接收的主要内容,如果未能解决你的问题,请参考以下文章

将JSON对象反序列化为嵌套的C#对象

Spring Boot 无法为对象返回 JSON,但不能为对象列表返回 JSON

如何解析不可预测的 JSON 对象?

django rest api:JSON 解析错误 - 无法解码 JSON 对象

为 HttpClient Post 方法准备 Json 对象

解析JSON对象的AsyncTask#3错误。 String无法转换为JSONObject