发送一个正确的json对象
发送kafka消息也是一个json传,也没问题
等到消费kafka时,也能接收到一个json字符串但是会多一个 " 引号, 就会导致json转对象失败所以需要先去除 开通和结尾的 " 引号
去除后的json 就是一个正常的json,才能转换对象
处理 " 引号的代码逻辑
@KafkaListener(topics = {"brok.............1"},containerFactory = "kafkaListenerContainerFactory")
// @Transactional(rollbackOn = Exception.class)public void receive(String message) throws JsonProcessingException, ParseException, InterruptedException {ObjectMapper objectMapper=new ObjectMapper();objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);try {message = message.replace("\\", "");String startStr = message.substring(0,1);// 去除开头 " 引号if( "\"".equals( startStr )){message = message.substring(1);}// 去除结尾 " 引号String lastStr = message.substring(message.length()-1,message.length());if( "\"".equals( lastStr )){message = message.substring(0, message.length() - 1);}System.out.println( message );MessageBean messageBean = JSON.parseObject(message, MessageBean.class);consumerService.consumer(messageBean);}catch (Exception e){e.printStackTrace();}}