gpt4 book ai didi

java - 无法解析我在 Spring Cloud Stream 中通过绑定(bind)器接收到的 Json 对象

转载 作者:行者123 更新时间:2023-12-01 17:02:42 24 4
gpt4 key购买 nike

我的 Json 转换器有问题,当我尝试监听该对象并将其转换为 java 对象时,我正在使用 spring 云流在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";

关于java - 无法解析我在 Spring Cloud Stream 中通过绑定(bind)器接收到的 Json 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61491380/

24 4 0