无法解析在 spring 云流中使用绑定器接收到的我的 Json 对象
can't parse my Json object received with binders in spring cloud stream
我在使用 Json 转换器时遇到问题,我正在使用 spring 云流在 rabbitmq 中发送一个 Json 对象,当我尝试收听该对象并将其转换为一个 java 对象。
这里是我收到物品的地方:
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 云流在 rabbitmq 中发送一个 Json 对象,当我尝试收听该对象并将其转换为一个 java 对象。
这里是我收到物品的地方:
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";