avro runtime exception not a map when return in Json format -
i have avro schema ukrecord, contain list of cmrecord(also avro schemaed):
{ "namespace": "com.uhdyi.hi.avro", "type": "record", "name": "ukrecord", "fields": [ { "name": "coupon", "type": [ "null", "string" ], "default": null }, { "name": "cm", "type": [ "null", { "type": "array", "items": { "type": "record", "name": "cmrecord", "fields": [ { "name": "id", "type": "string", "default": "" }, { "name": "name", "type": "string", "default": "" } ] } } ], "default": null } ] } in java code, create ukrecord has fields populated correctly, need return object using json based api, complained:
org.apache.avro.avroruntimeexception: not map: {"type":"record","name":"cmrecord","namespace":"com.uhdyi.hi.avro","fields":[{"name":"id","type":"string","default":""},{"name":"name","type":"string","default":""}]} the java code write object json :
objectwriter writer = objectmapper.writer(); if (obj != null) { response.setheader(httpheaders.names.content_type, "application/json; charset=utf-8"); byte[] bytes = writer.writevalueasbytes(obj); <-- failed here ... } obj is:
{"coupon": "c12345", "cm": [{"id": "1", "name": "name1"}, {"id": "2", "name": "name2"}]} why error? please help!
because using unions, avro uncertain how interpret json providing. here's how can change json avro knows it's not null
{ "coupon": { "string": "c12345" }, "cm": { "array": [ { "id": "1", "name": "name1" }, { "id": "2", "name": "name2" } ] } } i know, it's annoying how avro chose handle nulls.
Comments
Post a Comment