Skip to content

Classes to assist serializing and deserializing protobufs with JAX-RS.

Notifications You must be signed in to change notification settings

ollierob/jaxrs-protobuf

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

43 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

jaxrs-protobuf

Classes to assist serializing and deserializing protocol buffers with JAX-RS.

Proto helpers

To serialize a Java type into a protobuf, simply have it implement either the WritesProto or BuildsProto interface:

interface MyType extends BuildsProto<MyTypeProto> { 

  @Override
  default MyTypeProto toProto() { ... }

}

If using subclasses that serialize differently, you might want to consider overriding a method that returns a mutable MessageBuilder:

interface MyType extends BuildsProto<MyTypeProto> { 

  String id();

  @Override
  default MyTypeProto toProto() {
    return this.toProtoBuilder().build();
  }
  
  default MyTypeProto.Builder toProtoBuilder() { 
    return MyTypeProto.Builder.newBuilder().setId(this.id());
  }

}

class MyClass implements MyType {

  @Override
  public MyTypeProto.Builder toProtoBuilder() {
      return MyType.super.toProtoBuilder().setType("myclass");
  }
  
}

JAX-RS serialization

Read or write types that implement the above interfaces, and annotate with the protobuf media type:

interface MyResource {

  @GET
  @Produces(ProtobufMediaType.APPLICATION_PROTOBUF)
  MyType get(@QueryParam("id") String id);
  
  @POST
  @Consumes(ProtobufMediaType.APPLICATION_PROTOBUF)
  Response post(MyType object);

}

Note that you may need to install the providers that do this manually. How you do this depends on your JAX-RS implementation - for example, if using resteasy-guice then you simply bind the providers.

About

Classes to assist serializing and deserializing protobufs with JAX-RS.

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages