要开始,有两两件事需要发生:
- 您需要develop a custom
ObjectMapper
的Avro的模式格式
- 您需要supply that custom
ObjectMapper
泽西岛的时尚之后。
这应该是这个样子:
@Provider
public class AvroMapperProvider implements ContextResolver<ObjectMapper> {
final AvroMapper avroMapper = new AvroMapper();
@Override
public ObjectMapper getContext(Class<?> type) {
return avroMapper;
}
}
Configure your application使用杰克逊的消息处理程序:
public class MyApplication extends ResourceConfig {
public MyApplication() {
super(JacksonFeature.class,AvroMapperProvider.class);
}
}
另外,可以实现自定义MessageBodyReader
和MessageBodyWriter
,可以让你直接在进出的路上处理有效载荷:
public class AvroMessageReader implements MessageBodyReader<Person> {
AvroSchema schema;
final AvroMapper avroMapper = new AvroMapper();
public AvroMessageReader(){
schema = avroMapper.schemaFor(Person.class); //generates an Avro schema from the POJO class.
}
@Override
public boolean isReadable(Class<?> type, Type type1, Annotation[] antns, MediaType mt) {
return type == Person.class; //determines that this reader can handle the Person class.
}
@Override
public Person readFrom(Class<Person> type, Type type1, Annotation[] antns, MediaType mt, MultivaluedMap<String, String> mm, InputStream in) throws IOException, WebApplicationException {
return avroMapper.reader(schema).readValue(in);
}
}
在这里,我们从假设的Person类生成avro模式。 JAX-RS运行时将根据isReadable
的响应选择此阅读器。
You can then inject the MessageBodyWorkers
组件到您的服务实现类:
@Path("app")
public static class BodyReaderTest{
@Context
private MessageBodyWorkers workers;
@POST
@Produces("avro/binary")
@Consumes("avro/binary")
public String processMessage() {
workers.getMessageBodyReader(Person.class, Person.class, new Annotation[]{}, MediaType.APPLICATION_JSON_TYPE);
}
}
要回答你的最后的评论:在你的处理器Setting the mime type到建议的Avro /二进制应该做到这一点。
一些有用的链接:https://github.com/keedio/avro-schema-repo/tree/master/client/src/main/java/org/apache/avro/repo/client https://开头pastebin.com/DNS8xntG –
您看过测试文件夹:https://github.com/FasterXML/jackson-dataformats-binary/tree/master/avro/src/test/java/com/fasterxml/jackson/ dataformat/avro? – bosco