objectmapper objectmapper = new objectmapper() ;
user user = user.newbuilder().setname("yanbin").setaddress("chicago").build();
system.out.println(objectmapper.writevalueasstring(user));
收到异常(关键信息)
caused by: org.apache.avro.avroruntimeexception: not a map: {"type":"record","name":"user","namespace":"cc.unmi.data","fields":[{"name":"name","type":"string"},{"name":"address","type":["string","null"]}]}
at org.apache.avro.schema.getvaluetype(schema.java:294)
at com.fasterxml.jackson.databind.ser.beanpropertywriter.serializeasfield(beanpropertywriter.java:664)
at com.fasterxml.jackson.databind.ser.std.beanserializerbase.serializefields(beanserializerbase.java:689)
从上面的错误可以定位到 jackson 的试图序列化 user 对象的
public org.apache.avro.schema getschema() { return schema$; }
objectmapper objectmapper = new objectmapper() ;
objectmapper.addmixin(schema.class, ignoreavroschemafield.class);
user user = user.newbuilder().setname("yanbin").setaddress("chicago").build();
system.out.println(objectmapper.writevalueasstring(user));
objectmapper objectmapper = new objectmapper() ;
simplemodule simplemodule = new simplemodule("simplemodule", version.unknownversion());
simplemodule.addserializer(schema.class, new avroschemaserializer());
objectmapper.registermodule(simplemodule);
user user = user.newbuilder().setname("yanbin").setaddress("chicago").build();
system.out.println(objectmapper.writevalueasstring(user));