JsonParseException:意外的字符(“ i”(代码105)):期望双引号

时间:2018-11-20 06:36:48

标签: java serialization rabbitmq spring-cloud-stream

我正在尝试集成Spring Cloud流,并以RabbitMQ作为代理跨服务发布自定义Java对象。我正在发布的对象看起来像:

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

  // setters and getters
}

这只是一个包装对象,原始对象放在messageBody中。我放入messageBody中的对象看起来像:

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

请注意,AppMessageEnvelopeJob都位于不同的模型项目中,该项目作为Maven依赖项导入到发布者和订阅者Spring Boot项目中,因此模型是完全相同的一样。

在生产者中,我将该对象发布为:

@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问题,但没有解决。我想念什么?

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

这可能是一个有趣的错误,请将您的 json 和过去复制到在线 json formatter。 如果验证错误,请手动重写json或对其进行编码。

错误原因:从各种来源复制时的字符编码问题