java JSON解析异常:意外字符('i '(代码105)):应为双引号

iq0todco  于 2023-02-11  发布在  Java
关注(0)|答案(3)|浏览(524)

我正在尝试集成Spring Cloud流,并使用RabbitMQ作为代理跨服务发布自定义Java对象。

public class AppMessageEnvelope implements Serializable {
  ...
  private Object messageBody;
  private Date sentAt = new Date();
  ...

  // setters and getters
}

这只是一个 Package 器对象,原始对象放在messageBody中,我放在messageBody中的对象看起来像这样:

public class Job {
    ...
    private String message;
    private Map<MyEnum, String> myMap;
    ...
}

请注意,AppMessageEnvelopeJob位于不同的model项目中,该项目作为Maven依赖项导入到发布者和订阅者Sping Boot 项目中,因此模型完全相同。
在Producer中,我将对象发布为:

@EnableBinding(Source.class)
public class JobDistributor {

    private final Source jobQueue;

    @Autowired
    public JobDistributor(Source jobQueue) {
        this.jobQueue = jobQueue;
    }

    public AppMessageEnvelope publishJob(AppMessageEnvelope message) {
        LOG.info("Sending message: {}.", message);
        jobQueue.output().send(MessageBuilder.withPayload(message).build());
        return message;
    }
}

在消费者中,我得到的消息是:

@Component
@EnableBinding(Sink.class)
public class JobConsumer {

    private final JobManager jobManager;
    private final ObjectMapper objectMapper;

    @Autowired
    public JobConsumer(
            JobManager jobManager, ObjectMapper objectMapper) {
        this.jobManager = jobManager;
        this.objectMapper = objectMapper;
    }

    @StreamListener(target = Sink.INPUT)
    public void processData(AppMessageEnvelope messageEnvelope) {
        LOG.info("Envelope received: {}.", messageEnvelope);

        try {
            TypeReference<Job> mapType = new TypeReference<Job>() {};
            Job job = objectMapper.readValue(messageEnvelope.getMessageBody().toString(), mapType);
            jobManager.processRequest(job);
        } catch (Exception ex) {
            LOG.error("Couldn't convert to correct object for processing: {}.", ex);
        }
    }
}

我尝试使用TypeReference将内部对象转换为正确的对象,但我得到一个错误:

JobConsumer - Couldn't convert to correct object for processing: {}.
    com.fasterxml.jackson.core.JsonParseException: Unexpected character ('i' (code 105)): was expecting double-quote to start field name
    at [Source: (StringReader); line: 1, column: 3]

在转换邮件之前,我会记录它:

JobConsumer - Envelope received: AppMessageEnvelope{..., messageBody={id=5bf3a7302dbe9c7cf9927c60, jobId=8c0bfcb0b21248e694b5cd52337a1f9e, submittedAt=2018-11-20T06:18:24+0000, lastUpdatedOn=null, message=null, ..., fileContentMap={FILE_BYTES=JVBERi0xLjUKJb/3ov}}, sentAt=Tue Nov 20 11:48:24 IST 2018}

我尝试将ObjectMapper配置为:

@Autowired
private ObjectMapper objectMapper() {
    JsonFactory factory = new JsonFactory();
    factory.enable(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES);
    return new ObjectMapper(factory);
}

我也尝试过使用以下命令启用取消引用字段:

objectMapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);

我尝试了this blog提供的解决方案和一些类似的SO问题,但没有解决。我错过了什么?

rm5edbpk

rm5edbpk1#

使用下面的JSON转换将JSON转换为模型/实体

ObjectMapper objectMapper = new ObjectMapper();
Job job = objectMapper.convertValue(messageEnvelope.getMessageBody().toString(), Job.class);
6kkfgxo0

6kkfgxo02#

这可能是有趣的错误,请复制您的json和过去到在线json formatter。如果验证错误,请重写json手动或编码它。
错误原因:从各种来源复制时的字符编码问题

ldfqzlk8

ldfqzlk83#

这里的问题:作业job =对象Map器.读取值(消息信封.获取消息正文().到字符串(),Map类型);就像这样:作业job =对象Map器.读取值(对象Map器.写入值为字符串(消息信封.获取消息正文()),Map类型);

相关问题