带有休息的Apache AVRO [英] Apache AVRO with Rest

查看:80
本文介绍了带有休息的Apache AVRO的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在评估将Apache AVRO用于我的Jersey REST服务.我正在将Springboot与Jersey REST一起使用.

I am evaluating using Apache AVRO for my Jersey REST services. I am using Springboot with Jersey REST.

当前,我接受JSON作为输入,并使用Jackson对象映射器将其转换为Java Pojos.

Currently I am accepting JSON as input which are converted to Java Pojos using the Jackson object mapper.

我曾在不同的地方看过,但找不到任何使用带有Jersey端点的Apache AVRO的示例.

I have looked in different places but I cannot find any example that is using Apache AVRO with a Jersey end point.

我找到了这个Github存储库( https://github.com/FasterXML/jackson -dataformats-binary/)具有Apache AVRO插件.

I have found this Github repository (https://github.com/FasterXML/jackson-dataformats-binary/) which has Apache AVRO plugin.

我仍然找不到如何整合这一点的好例子.有没有人将Apache AVRO与Jersey一起使用?如果可以,请问有什么可以使用的示例吗?

I still cannot find any good example as how to integrate this. Has anyone used Apache AVRO with Jersey? If yes, is there any example I can use?

推荐答案

开始时,需要发生两件事:

To start , two things need to happen:

  1. 您需要开发自定义ObjectMapper 遵循Avro模式格式
  2. 您需要将自定义ObjectMapper 提供给泽西岛.

应该看起来像这样:

@Provider
public class AvroMapperProvider implements ContextResolver<ObjectMapper> {

    final AvroMapper avroMapper = new AvroMapper();

    @Override
    public ObjectMapper getContext(Class<?> type) {
        return avroMapper;
    }
}

配置您的应用程序以将Jackson用作消息处理程序:

Configure your application to use Jackson as the message handler:

public class MyApplication extends ResourceConfig {
    public MyApplication() {
         super(JacksonFeature.class,AvroMapperProvider.class);
    }
}


或者,您可以实现自定义 MessageBodyReader


Alternatively, you can implement a custom MessageBodyReader and MessageBodyWriter that allows you to directly process the payloads on the way in and out:

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的响应选择此阅读器.

Here, we generate an avro schema from a hypothetical Person class. The JAX-RS runtime will select this reader based on the response from isReadable.

然后您可以将MessageBodyWorkers 组件注入您的服务实现类:

You can then inject the MessageBodyWorkers component into your service implementation class:

@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);
    }
 }

要回答您的最新评论:设置哑剧类型

To answer your last comment: Setting the mime type on your handler to the recommended avro/binary ought to do it.

这篇关于带有休息的Apache AVRO的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆