将 Google proto 缓冲区与 Jersey/JAX-RS 相结合 [英] Combining Google proto buffers with Jersey/JAX-RS

查看:19
本文介绍了将 Google proto 缓冲区与 Jersey/JAX-RS 相结合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我有一个 RESTful Web 服务,其端点通过 Jersey/JAX-RS 公开:

Currently I have a RESTful web service with endpoints that are exposed via Jersey/JAX-RS:

@Path("/widgets")
public class WidgetResource {
    @GET
    List<Widget> getAllWidgets() {
        // gets Widgets somehow
    }

    @POST
    Widget save(Widget w) {
        // Save widget and return it
    }
}

我使用 Jackson 将我的 POJO 序列化/反序列化为 JSON,我的服务以 application/json 的形式响应并发送回我的 POJO.

I use Jackson for serializing/deserializing my POJOs into JSON, and my service both responds to and sends back my POJOs as application/json.

我现在正在寻找可能使用 Google 协议缓冲区(或等效技术)来帮助压缩/优化客户端和服务之间的通信,因为 JSON/文本非常庞大/浪费.

I am now looking to possibly use Google protocol buffers (or an equivalent technology) to help compress/optimize the communication between client and service, as JSON/text is pretty bulky/wasteful.

实际上,我有一个由微服务"架构组成的大型后端;数十个相互通信的 REST 服务;这就是为什么我希望优化所有这些消息之间来回发送的消息.

In reality, I have a large backend that consists of a "microservice" architecture; dozens of REST services communicating with each other; this is why I'm looking to optimize the the messages sent backk and forth between all of them.

所以我问:是否仍然可以让 Jersey/JAX-RS 为我的服务端点提供服务,但要清除 Jackson/JSON 的东西并用 Google 协议缓冲区替换它?如果是这样,这段代码可能是什么样的?

So I ask: is it possible to still have Jersey/JAX-RS serve up my service endpoints, but to gut out the Jackson/JSON stuff and replace it with Google protocol buffers? If so, what might this code look like?

推荐答案

JAX-RS 使用 MessageBodyReaderMessageBodyWriter 对不同媒体类型进行序列化/反序列化.您可以在 JAX-RS 阅读更多内容实体提供者.您可以编写自己的来处理 protobuf 对象的序列化/反序列化.然后只需通过发现显式或隐式向应用程序注册提供程序.

JAX-RS uses implementations of MessageBodyReader and MessageBodyWriter to serialize/deserialize to and from differen media types. You can read more at JAX-RS Entity Providers. You can write your own to handle the serializion/derialization of your protobuf objects. Then just register the provider(s) with the application, either explicitly or implicitly through discovery.

widgets.proto

package widget;

option java_package = "protobuf.example";
option java_outer_classname = "WidgetsProtoc";

message Widget {
    required string id = 1;
    required string name = 2;
}

message WidgetList {
    repeated Widget widget = 1;
}

编译完成后,我将得到一个 WidgetsProtoc 类,其中包含静态内部 WidgetWidgetList 类.

When this is compiled, I will be left with a WidgetsProtoc class with static inner Widget and WidgetList classes.

WidgetResource

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Response;
import protobuf.example.WidgetsProtoc.Widget;
import protobuf.example.WidgetsProtoc.WidgetList;

@Path("/widgets")
public class WidgetResource {
    
    @GET
    @Produces("application/protobuf")
    public Response getAllWidgets() {
        Widget widget1 = 
                Widget.newBuilder().setId("1").setName("widget 1").build();
        Widget widget2 = 
                Widget.newBuilder().setId("2").setName("widget 2").build();
        WidgetList list = WidgetList.newBuilder()
                .addWidget(widget1).addWidget(widget2).build();
        return Response.ok(list).build();
    }
    
    @POST
    @Consumes("application/protobuf")
    public Response postAWidget(Widget widget) {
        StringBuilder builder = new StringBuilder("Saving Widget 
");
        builder.append("ID: ").append(widget.getId()).append("
");
        builder.append("Name: ").append(widget.getName()).append("
");
        return Response.created(null).entity(builder.toString()).build();
    }
}

您会注意到 application/protobuf" 媒体类型的使用.这不是标准媒体类型,但有一个草案在工作中.Guava 库也定义了此媒体类型为 MediaType.PROTOBUF,它转换为 application/protobuf",所以我选择坚持使用 that.

You'll notice the use of the "application/protobuf" media type. This isn't a standard media type, but there is a draft in the working. Also the Guava library has define this media type as MediaType.PROTOBUF, which translates to "application/protobuf", so I chose to stick with that.

MessageBodyReaderMessageBodyWriter 都定义在一个类中.您可以选择单独进行.没有区别.

MessageBodyReader and MessageBodyWriter all defined in one class. You can choose to do it separately. Makes no difference.

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import javax.ws.rs.BadRequestException;
import javax.ws.rs.Consumes;
import javax.ws.rs.Produces;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.ext.MessageBodyReader;
import javax.ws.rs.ext.MessageBodyWriter;
import javax.ws.rs.ext.Provider;
import protobuf.example.WidgetsProtoc.Widget;
import protobuf.example.WidgetsProtoc.WidgetList;

@Provider
@Produces("application/protobuf")
@Consumes("application/protobuf")
public class WidgetProtocMessageBodyProvider 
                   implements MessageBodyReader, MessageBodyWriter {

    @Override
    public boolean isReadable(Class type, Type type1, 
            Annotation[] antns, MediaType mt) {
        return Widget.class.isAssignableFrom(type) 
                || WidgetList.class.isAssignableFrom(type);
    }

    @Override
    public Object readFrom(Class type, Type type1, Annotation[] antns, 
            MediaType mt, MultivaluedMap mm, InputStream in) 
            throws IOException, WebApplicationException {
        if (Widget.class.isAssignableFrom(type)) {
            return Widget.parseFrom(in);
        } else if (WidgetList.class.isAssignableFrom(type)) {
            return WidgetList.parseFrom(in);
        } else {
            throw new BadRequestException("Can't Deserailize");
        }
    }

    @Override
    public boolean isWriteable(Class type, Type type1, 
            Annotation[] antns, MediaType mt) {
        return Widget.class.isAssignableFrom(type) 
                || WidgetList.class.isAssignableFrom(type);
    }

    @Override
    public long getSize(Object t, Class type, Type type1, 
            Annotation[] antns, MediaType mt) {  return -1; }

    @Override
    public void writeTo(Object t, Class type, Type type1, 
            Annotation[] antns, MediaType mt, 
            MultivaluedMap mm, OutputStream out) 
            throws IOException, WebApplicationException {
        if (t instanceof Widget) {
            Widget widget = (Widget)t;
            widget.writeTo(out);
        } else if (t instanceof WidgetList) {
            WidgetList list = (WidgetList)t;
            list.writeTo(out);
        }
    }  
}

TestCase(确保提供者在服务器和客户端都注册了)

TestCase (Make sure the provider is registered both with the server and client)

@Test
public void testGetIt() {
    Client client = ClientBuilder.newClient();
    WebTarget target = client.target("http://localhost:8080/api");
    // Get all list
    WidgetList list = target.path("/widgets").request().get(WidgetList.class);
    System.out.println("===== Response from GET =====");
    for (Widget widget: list.getWidgetList()) {
        System.out.println("id: " + widget.getId() 
                         + ", name: " + widget.getName());
    }
    
    // Post one 
    Widget widget = Widget.newBuilder().setId("10")
                          .setName("widget 10").build();
    Response responseFromPost = target.path("widgets").request()
            .post(Entity.entity(widget, "application/protobuf"));
    System.out.println("===== Response from POST =====");
    System.out.println(responseFromPost.readEntity(String.class));
    responseFromPost.close();
}

结果:

===== Response from GET =====
id: 1, name: widget 1
id: 2, name: widget 2
===== Response from POST =====
Saving Widget 
ID: 10
Name: widget 10

这篇关于将 Google proto 缓冲区与 Jersey/JAX-RS 相结合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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