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

问题描述 投票:0回答:1

我对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";
    } ```
java spring-boot spring-cloud-stream jackson2
1个回答
0
投票

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

public String processTransaction(
        ...
        return "transaction treated successfully";
    }
    else return "transaction failed";
© www.soinside.com 2019 - 2024. All rights reserved.