我正在尝试向Kafka发送消息,并使用Spring云流从Kafka消费相同的消息。我正在使用Postman向我的制作人的其他控制器发送JSON字符串{“acctNo”:“32432”,“tn”:“3234”}。我得到了JSON Parserexception:
com.fasterxml.jackson.core.JsonParseException: Unrecognized token 'ÿ': was expecting ('true', 'false' or 'null')
at [Source: [B@776e03af; line: 1, column: 4]
at com.fasterxml.jackson.core.JsonParser._constructError(JsonParser.java:1581) ~[jackson-core-2.6.5.jar:2.6.5]
at com.fasterxml.jackson.core.base.ParserMinimalBase._reportError(ParserMinimalBase.java:533) ~[jackson-core-2.6.5.jar:2.6.5]
at com.fasterxml.jackson.core.json.UTF8StreamJsonParser._reportInvalidToken(UTF8StreamJsonParser.java:3451) ~[jackson-core-2.6.5.jar:2.6.5]
at com.fasterxml.jackson.core.json.UTF8StreamJsonParser._handleUnexpectedValue(UTF8StreamJsonParser.java:2610) ~[jackson-core-2.6.5.jar:2.6.5]
at com.fasterxml.jackson.core.json.UTF8StreamJsonParser
我的代码是这样的:
生产者应用程序yaml
spring:
cloud:
stream:
bindings:
activationMsgQueue:
destination: test
contentType: application/json
制片人休息控制器
@RestController
@RequestMapping("/ActivationQueueService")
public class ActivationQueueController {
private static final Logger LOGGER = LoggerFactory
.getLogger(ActivationQueueController.class);
@Autowired
SpringCloudStreamClient producer;
@InitBinder
protected void initBinder(WebDataBinder binder) {
binder.setValidator(new ActivationDataInfoValidator());
}
@RequestMapping(method = RequestMethod.POST, value = "/sendMessage", headers = "Accept=application/json", produces = "application/json")
public void sendMessage(@RequestBody @Valid ActivationDataInfo message)
throws JsonProcessingException {
LOGGER.debug("Activation Data Request Recieved : " + message.toString());
if (message != null) {
ObjectMapper mapper = new ObjectMapper();
producer.sendMessagetoKafka(message);
LOGGER.info("Activation Data Request sent to Kafka : " + message);
}
}
}
制片人代码
@Service
@EnableBinding(MessageChannels.class)
public class SpringCloudStreamClient {
private static final Logger LOGGER = LoggerFactory
.getLogger(SpringCloudStreamClient.class);
@Autowired MessageChannels msgChannel;
public Object sendMessagetoKafka(ActivationDataInfo msg){
LOGGER.info("Sending Message : " + msg);
msgChannel.save().send(MessageBuilder.withPayload(msg).build());
return new String("Success");
}
}
消费者应用程序Yaml
spring:
cloud:
stream:
bindings:
input:
content-type: application/x-java-object;type=com.comcast.activation.message.vo.ActivationDataInfo
destination: test
group: prac
consumer:
headerMode: raw
enableDlq: true
resetOffsets: true
startOffset: latest
消费者代码
@EnableBinding(Sink.class)
public class LogSink {
private static Logger logger = LoggerFactory.getLogger(LogSink.class);
@ServiceActivator(inputChannel = Sink.INPUT)
public void loggerSink( ActivationDataInfo payload) throws Exception {
logger.info("Received: " + payload.getAcctNo());
}
}
域类
public class ActivationDataInfo {
private String acctNo;
private String tn;
public String getAcctNo() {
return acctNo;
}
public void setAcctNo(String acctNo) {
this.acctNo = acctNo;
}
public String getTn() {
return tn;
}
public void setTn(String tn) {
this.tn = tn;
}
造成这种情况的原因是什么?请帮忙。
答案 0 :(得分:1)
尝试从客户端删除headerMode: raw
,因为生产者也不使用它。